Skip to content

Commit

Permalink
encode jsonUnknown field as a Document when inlining its values
Browse files Browse the repository at this point in the history
  • Loading branch information
benoitlouy committed Aug 13, 2024
1 parent ac62ec9 commit 72fd3fe
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions modules/json/src/smithy4s/json/internals/SchemaVisitorJCodec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1411,19 +1411,22 @@ private[smithy4s] class SchemaVisitorJCodec(

private def jsonUnknownFieldEncoder[Z, A](
field: Field[Z, A]
): (Z, JsonWriter) => Unit = { (z: Z, out: JsonWriter) =>
field.foreachUnlessDefault(z) {
case m: Map[_, _] =>
m.foreach { case (label: String, value: Document) =>
writeLabel(label, out)
documentJCodec.encodeValue(value, out)
}
case Some(m: Map[_, _]) =>
m.foreach { case (label: String, value: Document) =>
writeLabel(label, out)
documentJCodec.encodeValue(value, out)
): (Z, JsonWriter) => Unit = {
val docEncoder = Document.Encoder.fromSchema(field.schema)
(z: Z, out: JsonWriter) =>
field.foreachUnlessDefault(z) { a =>
docEncoder.encode(a) match {
case Document.DObject(value) =>
value.foreach { case (label: String, value: Document) =>
writeLabel(label, out)
documentJCodec.encodeValue(value, out)
}
case _ =>
out.encodeError(
s"Failed encoding field ${field.label} because it cannot be converted to a JSON object"
)
}
}
}
}

private type Fields[Z] = Vector[Field[Z, _]]
Expand Down

0 comments on commit 72fd3fe

Please sign in to comment.