Skip to content

Commit

Permalink
make ImmutableLinkedHashMap serializable
Browse files Browse the repository at this point in the history
  • Loading branch information
ramazanyich committed Oct 31, 2023
1 parent 8eb784b commit 604bb63
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}
}

0 comments on commit 604bb63

Please sign in to comment.