From 604bb639791776139755c0cdd678644ead683b0b Mon Sep 17 00:00:00 2001 From: ramil Date: Tue, 31 Oct 2023 13:25:05 +0100 Subject: [PATCH] make ImmutableLinkedHashMap serializable --- .../libs/json/ImmutableLinkedHashMap.scala | 5 ++++- .../libs/json/ImmutableLinkedHashMap.scala | 3 ++- .../play/api/libs/json/JsObjectSpec.scala | 19 +++++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/play-json/shared/src/main/scala-2.13+/play/api/libs/json/ImmutableLinkedHashMap.scala b/play-json/shared/src/main/scala-2.13+/play/api/libs/json/ImmutableLinkedHashMap.scala index dae99d481..8805cbe85 100644 --- a/play-json/shared/src/main/scala-2.13+/play/api/libs/json/ImmutableLinkedHashMap.scala +++ b/play-json/shared/src/main/scala-2.13+/play/api/libs/json/ImmutableLinkedHashMap.scala @@ -13,8 +13,11 @@ 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] { +private[json] class ImmutableLinkedHashMap[A, +B](underlying: JLinkedHashMap[A, B]) + extends AbstractMap[A, B] + with Serializable { + private val serialVersionUID = -2338626292552177485L override def get(key: A): Option[B] = Option(underlying.get(key)) override def removed(key: A): Map[A, B] = { diff --git a/play-json/shared/src/main/scala-2.13-/play/api/libs/json/ImmutableLinkedHashMap.scala b/play-json/shared/src/main/scala-2.13-/play/api/libs/json/ImmutableLinkedHashMap.scala index 17b316fa9..0c0a18d69 100644 --- a/play-json/shared/src/main/scala-2.13-/play/api/libs/json/ImmutableLinkedHashMap.scala +++ b/play-json/shared/src/main/scala-2.13-/play/api/libs/json/ImmutableLinkedHashMap.scala @@ -20,8 +20,9 @@ import scala.collection.mutable.ArrayBuffer 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 { + private val serialVersionUID = -2338626292552177485L override def get(key: A): Option[B] = Option(underlying.get(key)) override def +[V1 >: B](kv: (A, V1)): Map[A, V1] = { diff --git a/play-json/shared/src/test/scala/play/api/libs/json/JsObjectSpec.scala b/play-json/shared/src/test/scala/play/api/libs/json/JsObjectSpec.scala index e04ce2359..099ef39c9 100644 --- a/play-json/shared/src/test/scala/play/api/libs/json/JsObjectSpec.scala +++ b/play-json/shared/src/test/scala/play/api/libs/json/JsObjectSpec.scala @@ -9,6 +9,8 @@ import play.api.libs.json.Json._ import org.scalatest.matchers.must.Matchers import org.scalatest.wordspec.AnyWordSpec +import java.io.{ ByteArrayInputStream, ByteArrayOutputStream, ObjectInputStream, ObjectOutputStream } + class JsObjectSpec extends AnyWordSpec with Matchers { "JsObject.deepMerge" should { "not fail when the objects are empty" in { @@ -275,5 +277,22 @@ class JsObjectSpec extends AnyWordSpec with Matchers { Json.stringify(originalObj).mustEqual(expected) } + + "serializa/deserialize correctly" in { + val originalObj = Json.obj( + "field1" -> 123, + "field2" -> "abc", + "field3" -> JsNull, + "obj" -> Json.obj("field1" -> 234) + ) + + 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].mustEqual(originalObj) + } } }