Skip to content

Commit

Permalink
Add missing support of explicitNulls option for JSON encoders of `S…
Browse files Browse the repository at this point in the history
…chema.GenericRecord` values
  • Loading branch information
plokhotnyuk committed Jan 11, 2025
1 parent a59764e commit 01dc907
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ object JsonCodec {
val encoders = nonTransientFields.map(field => schemaEncoder(field.schema.asInstanceOf[Schema[Any]], cfg))
if (nonTransientFields.isEmpty) { (_: ListMap[String, _], _: Option[Int], out: Write) =>
out.write("{}")
} else { (value: ListMap[String, _], indent: Option[Int], out: Write) =>
} else { (a: ListMap[String, _], indent: Option[Int], out: Write) =>
{
out.write('{')
val doPrettyPrint = indent ne None
Expand All @@ -600,21 +600,22 @@ object JsonCodec {
}
var idx = 0
while (idx < nonTransientFields.length) {
val field = nonTransientFields(idx)
val fieldName = field.fieldName
val fieldValue = value(fieldName)
if (!isEmptyOptionalValue(field, fieldValue, cfg)) {
val field = nonTransientFields(idx)
val encoder = encoders(idx)
idx += 1
val name = field.fieldName
val value = a(name)
if (!isEmptyOptionalValue(field, value, cfg) && (!encoder.isNothing(value) || cfg.explicitNulls)) {
if (first) first = false
else {
out.write(',')
if (doPrettyPrint) pad(indent_, out)
}
strEnc.unsafeEncode(fieldName, indent_, out)
strEnc.unsafeEncode(name, indent_, out)
if (doPrettyPrint) out.write(" : ")
else out.write(':')
encoders(idx).unsafeEncode(fieldValue, indent_, out)
encoder.unsafeEncode(value, indent_, out)
}
idx += 1
}
if (doPrettyPrint) pad(indent, out)
out.write('}')
Expand Down Expand Up @@ -1116,7 +1117,7 @@ object JsonCodec {

private[codec] def caseClassEncoder[Z](schema: Schema.Record[Z], cfg: Config, discriminatorTuple: DiscriminatorTuple): ZJsonEncoder[Z] = {
val nonTransientFields = schema.nonTransientFields.map(_.asInstanceOf[Schema.Field[Z, Any]]).toArray
val fieldEncoders = nonTransientFields.map(s => JsonEncoder.schemaEncoder(s.schema, cfg, discriminatorTuple))
val encoders = nonTransientFields.map(s => JsonEncoder.schemaEncoder(s.schema, cfg, discriminatorTuple))
(a: Z, indent: Option[Int], out: Write) => {
out.write('{')
val doPrettyPrint = indent ne None
Expand All @@ -1137,20 +1138,20 @@ object JsonCodec {
}
var idx = 0
while (idx < nonTransientFields.length) {
val schema = nonTransientFields(idx)
val enc = fieldEncoders(idx)
val field = nonTransientFields(idx)
val encoder = encoders(idx)
idx += 1
val value = schema.get(a)
if (!isEmptyOptionalValue(schema, value, cfg) && (!enc.isNothing(value) || cfg.explicitNulls)) {
val value = field.get(a)
if (!isEmptyOptionalValue(field, value, cfg) && (!encoder.isNothing(value) || cfg.explicitNulls)) {
if (first) first = false
else {
out.write(',')
if (doPrettyPrint) pad(indent_, out)
}
strEnc.unsafeEncode(schema.fieldName, indent_, out)
strEnc.unsafeEncode(field.fieldName, indent_, out)
if (doPrettyPrint) out.write(" : ")
else out.write(':')
enc.unsafeEncode(value, indent_, out)
encoder.unsafeEncode(value, indent_, out)
}
}
if (doPrettyPrint) pad(indent, out)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,19 @@ object JsonCodecSpec extends ZIOSpecDefault {
"{}"
)
},
test("record with option fields encoded as null") {
test("record with option fields") {
assertEncodes(
recordWithOptionSchema,
ListMap[String, Any]("foo" -> Some("s"), "bar" -> None),
charSequenceToByteChunk("""{"foo":"s","bar":null}""")
charSequenceToByteChunk("""{"foo":"s"}""")
)
},
test("record with option fields and flag to encode nulls") {
assertEncodes(
recordWithOptionSchema,
ListMap[String, Any]("foo" -> Some("s"), "bar" -> None),
charSequenceToByteChunk("""{"foo":"s","bar":null}"""),
JsonCodec.Config.default.copy(explicitNulls = true)
)
},
test("case class with option fields omitted when empty") {
Expand Down Expand Up @@ -479,7 +487,7 @@ object JsonCodecSpec extends ZIOSpecDefault {
RecordExample.schema,
RecordExample(f1 = "test", f3 = Some("transient"), f20 = None, f21 = Vector.empty, f22 = Nil),
charSequenceToByteChunk(
"""{"$f1":"test","f20":null,"f21":[],"f22":[]}""".stripMargin
"""{"$f1":"test","f21":[],"f22":[]}""".stripMargin
)
)
}
Expand Down

0 comments on commit 01dc907

Please sign in to comment.