Skip to content

Commit

Permalink
Improvemetns
Browse files Browse the repository at this point in the history
  • Loading branch information
lciolecki committed Nov 26, 2024
1 parent 6eb15cb commit 0dc7f3d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,15 @@ case class ToJsonEncoder(
case a: OffsetDateTime => Encoder[OffsetDateTime].apply(a)
case a: UUID => safeString(a.toString)
case a: DisplayJson => a.asJson
case a: scala.collection.Map[_, _] => encodeMap(a.toMap)
case a: java.util.Map[_, _] => encodeMap(a.asScala.toMap)
case a: Iterable[_] => fromValues(a.map(encode))
case a: Enum[_] => safeString(a.toString)
case a: java.util.Collection[_] => fromValues(a.asScala.map(encode))
case a: Array[_] => fromValues(a.map(encode))
case _ if !failOnUnknown => safeString(any.toString)
case a => throw new IllegalArgumentException(s"Invalid type: ${a.getClass}")
case a: scala.collection.immutable.ListMap[_, _] => encodeListMap(a)
case a: scala.collection.Map[_, _] => encodeMap(a.toMap)
case a: java.util.Map[_, _] => encodeMap(a.asScala.toMap)
case a: Iterable[_] => fromValues(a.map(encode))
case a: Enum[_] => safeString(a.toString)
case a: java.util.Collection[_] => fromValues(a.asScala.map(encode))
case a: Array[_] => fromValues(a.map(encode))
case _ if !failOnUnknown => safeString(any.toString)
case a => throw new IllegalArgumentException(s"Invalid type: ${a.getClass}")
}
)

Expand All @@ -86,17 +87,22 @@ case class ToJsonEncoder(
case None => Null
}

private def encodeListMap(value: scala.collection.immutable.ListMap[_, _]): Json = {
val mapWithStringKeys = value.toList.map { case (k, v) =>
k.toString -> encode(v)
}

fromFields(mapWithStringKeys)
}

// TODO: make encoder aware of NU Types to encode things like multiset differently. Right now its handled by calling
// toString on keys.
private def encodeMap(map: Map[_, _]) = {
// TODO: Switch to SeqMap after removing support for Scala 2.12
val mapWithStringKeys = ListMap.from(
map.toList.map { case (k, v) =>
k.toString -> v
}
)
val mapWithStringKeys = map.view.map { case (k, v) =>
k.toString -> encode(v)
}.toMap

fromFields(mapWithStringKeys.mapValuesNow(encode))
fromFields(mapWithStringKeys)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -103,20 +103,26 @@ class ToJsonEncoderSpec extends AnyFunSpec with Matchers {
}

it("should convert map to json and keep order of keys") {
val map = ListMap(
"intNumber" -> 42,
"floatNumber" -> 42.42,
"someTimestamp" -> 1496930555793L,
"someString" -> "hello",
"booleanValue" -> true
val nestedMap = ListMap(
"first" -> ListMap(
"b" -> 2,
"a" -> 1,
),
"second" -> ListMap(
"x" -> 3,
"y" -> 4
)
)

encoder.encode(map) shouldBe Json.obj(
"intNumber" -> fromInt(42),
"floatNumber" -> fromFloatOrNull(42.42f),
"someTimestamp" -> fromLong(1496930555793L),
"someString" -> fromString("hello"),
"booleanValue" -> fromBoolean(true),
encoder.encode(nestedMap) shouldBe Json.obj(
"first" -> Json.obj(
"b" -> fromInt(2),
"a" -> fromInt(1),
),
"second" -> Json.obj(
"x" -> fromInt(3),
"y" -> fromInt(4)
)
)
}

Expand Down

0 comments on commit 0dc7f3d

Please sign in to comment.