Skip to content

Commit 10cd132

Browse files
authored
Merge pull request #945 from ramazanyich/jsobject_serializable
make ImmutableLinkedHashMap serializable
2 parents 2edce8b + ff052c4 commit 10cd132

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (C) from 2022 The Play Framework Contributors <https://github.com/playframework>, 2011-2021 Lightbend Inc. <https://www.lightbend.com>
3+
*/
4+
5+
package play.api.libs.json
6+
7+
import org.specs2.mutable._
8+
import play.api.libs.json.Json._
9+
10+
import java.io.{ ByteArrayInputStream, ByteArrayOutputStream, ObjectInputStream, ObjectOutputStream }
11+
12+
class JsObjectSerializationSpec extends Specification {
13+
"JsObject" should {
14+
"serialize/deserialize correctly" in {
15+
val originalObj = Json.obj(
16+
"field1" -> 123,
17+
"field2" -> "abc",
18+
"field3" -> JsNull,
19+
"obj" -> Json.obj("field1" -> 234),
20+
"arr" -> JsArray(
21+
Seq(
22+
JsString("abc"),
23+
JsNumber(123),
24+
JsBoolean(true),
25+
JsNull,
26+
Json.obj("field1" -> 345)
27+
)
28+
)
29+
)
30+
31+
val bos = new ByteArrayOutputStream()
32+
val out = new ObjectOutputStream(bos)
33+
out.writeObject(originalObj)
34+
35+
val bis = new ByteArrayInputStream(bos.toByteArray)
36+
val in = new ObjectInputStream(bis)
37+
in.readObject().asInstanceOf[JsObject].must(beEqualTo(originalObj))
38+
}
39+
}
40+
}

play-json/shared/src/main/scala-2.13+/play/api/libs/json/ImmutableLinkedHashMap.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ import scala.collection.mutable
1313
/**
1414
* Wraps a Java LinkedHashMap as a Scala immutable.Map.
1515
*/
16-
private[json] class ImmutableLinkedHashMap[A, +B](underlying: JLinkedHashMap[A, B]) extends AbstractMap[A, B] {
16+
@SerialVersionUID(-2338626292552177485L)
17+
private[json] class ImmutableLinkedHashMap[A, +B](underlying: JLinkedHashMap[A, B])
18+
extends AbstractMap[A, B]
19+
with Serializable {
1720

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

play-json/shared/src/main/scala-2.13-/play/api/libs/json/ImmutableLinkedHashMap.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ import scala.collection.mutable.ArrayBuffer
1717
/**
1818
* Wraps a Java LinkedHashMap as a Scala immutable.Map.
1919
*/
20+
@SerialVersionUID(-2338626292552177485L)
2021
private[json] class ImmutableLinkedHashMap[A, +B](underlying: JLinkedHashMap[A, B])
2122
extends AbstractMap[A, B]
2223
with Map[A, B]
23-
with MapLike[A, B, ImmutableLinkedHashMap[A, B]] {
24+
with MapLike[A, B, ImmutableLinkedHashMap[A, B]] with Serializable {
2425

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

play-json/shared/src/main/scala/play/api/libs/json/JsValue.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ object JsValue {
3333
* Represents a Json null value.
3434
*/
3535
case object JsNull extends JsValue {
36+
@transient
3637
implicit val reads: Reads[JsNull.type] = Reads[JsNull.type] {
3738
case JsNull => JsSuccess(JsNull)
3839
case _ => JsError("error.expected.null")

0 commit comments

Comments
 (0)