Skip to content

Commit

Permalink
Merge pull request #945 from ramazanyich/jsobject_serializable
Browse files Browse the repository at this point in the history
make ImmutableLinkedHashMap serializable
  • Loading branch information
mkurz authored Nov 4, 2023
2 parents 2edce8b + ff052c4 commit 10cd132
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (C) from 2022 The Play Framework Contributors <https://github.com/playframework>, 2011-2021 Lightbend Inc. <https://www.lightbend.com>
*/

package play.api.libs.json

import org.specs2.mutable._
import play.api.libs.json.Json._

import java.io.{ ByteArrayInputStream, ByteArrayOutputStream, ObjectInputStream, ObjectOutputStream }

class JsObjectSerializationSpec extends Specification {
"JsObject" should {
"serialize/deserialize correctly" in {
val originalObj = Json.obj(
"field1" -> 123,
"field2" -> "abc",
"field3" -> JsNull,
"obj" -> Json.obj("field1" -> 234),
"arr" -> JsArray(
Seq(
JsString("abc"),
JsNumber(123),
JsBoolean(true),
JsNull,
Json.obj("field1" -> 345)
)
)
)

val bos = new ByteArrayOutputStream()
val out = new ObjectOutputStream(bos)
out.writeObject(originalObj)

val bis = new ByteArrayInputStream(bos.toByteArray)
val in = new ObjectInputStream(bis)
in.readObject().asInstanceOf[JsObject].must(beEqualTo(originalObj))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import scala.collection.mutable
/**
* Wraps a Java LinkedHashMap as a Scala immutable.Map.
*/
private[json] class ImmutableLinkedHashMap[A, +B](underlying: JLinkedHashMap[A, B]) extends AbstractMap[A, B] {
@SerialVersionUID(-2338626292552177485L)
private[json] class ImmutableLinkedHashMap[A, +B](underlying: JLinkedHashMap[A, B])
extends AbstractMap[A, B]
with Serializable {

override def get(key: A): Option[B] = Option(underlying.get(key))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ import scala.collection.mutable.ArrayBuffer
/**
* Wraps a Java LinkedHashMap as a Scala immutable.Map.
*/
@SerialVersionUID(-2338626292552177485L)
private[json] class ImmutableLinkedHashMap[A, +B](underlying: JLinkedHashMap[A, B])
extends AbstractMap[A, B]
with Map[A, B]
with MapLike[A, B, ImmutableLinkedHashMap[A, B]] {
with MapLike[A, B, ImmutableLinkedHashMap[A, B]] with Serializable {

override def get(key: A): Option[B] = Option(underlying.get(key))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ object JsValue {
* Represents a Json null value.
*/
case object JsNull extends JsValue {
@transient
implicit val reads: Reads[JsNull.type] = Reads[JsNull.type] {
case JsNull => JsSuccess(JsNull)
case _ => JsError("error.expected.null")
Expand Down

0 comments on commit 10cd132

Please sign in to comment.