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 f5885e0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -600,10 +600,12 @@ object JsonCodec {
}
var idx = 0
while (idx < nonTransientFields.length) {
val field = nonTransientFields(idx)
val field = nonTransientFields(idx)
val encoder = encoders(idx)
idx += 1
val fieldName = field.fieldName
val fieldValue = value(fieldName)
if (!isEmptyOptionalValue(field, fieldValue, cfg)) {
if (!isEmptyOptionalValue(field, fieldValue, cfg) && (!encoder.isNothing(fieldValue) || cfg.explicitNulls)) {
if (first) first = false
else {
out.write(',')
Expand All @@ -612,9 +614,8 @@ object JsonCodec {
strEnc.unsafeEncode(fieldName, indent_, out)
if (doPrettyPrint) out.write(" : ")
else out.write(':')
encoders(idx).unsafeEncode(fieldValue, indent_, out)
encoder.unsafeEncode(fieldValue, indent_, out)
}
idx += 1
}
if (doPrettyPrint) pad(indent, out)
out.write('}')
Expand Down Expand Up @@ -1137,11 +1138,11 @@ object JsonCodec {
}
var idx = 0
while (idx < nonTransientFields.length) {
val schema = nonTransientFields(idx)
val enc = fieldEncoders(idx)
val schema = nonTransientFields(idx)
val encoder = fieldEncoders(idx)
idx += 1
val value = schema.get(a)
if (!isEmptyOptionalValue(schema, value, cfg) && (!enc.isNothing(value) || cfg.explicitNulls)) {
if (!isEmptyOptionalValue(schema, value, cfg) && (!encoder.isNothing(value) || cfg.explicitNulls)) {
if (first) first = false
else {
out.write(',')
Expand All @@ -1150,7 +1151,7 @@ object JsonCodec {
strEnc.unsafeEncode(schema.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 f5885e0

Please sign in to comment.