Skip to content

Commit

Permalink
Improvemetns to abr's comment
Browse files Browse the repository at this point in the history
  • Loading branch information
lciolecki committed Nov 26, 2024
1 parent 1943c73 commit a0ecd85
Showing 1 changed file with 9 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,14 @@ 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.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}")
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 @@ -83,20 +82,12 @@ case class ToJsonEncoder(
case None => Null
}

private def encodeListMap(value: scala.collection.immutable.ListMap[_, _]): Json = {
val mapWithStringKeys = value.view.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[_, _]) = {
val mapWithStringKeys = map.view.map { case (k, v) =>
k.toString -> encode(v)
}.toMap
}

fromFields(mapWithStringKeys)
}
Expand Down

0 comments on commit a0ecd85

Please sign in to comment.