Skip to content

Commit

Permalink
use different config value
Browse files Browse the repository at this point in the history
  • Loading branch information
a-morales committed Sep 12, 2024
1 parent ddc29af commit c1a1955
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ import zio.{ Cause, Chunk, ChunkBuilder, ZIO, ZNothing }

object JsonCodec {

final case class Config(ignoreEmptyCollections: Boolean, treatStreamsAsArrays: Boolean = false)
final case class Config(
ignoreEmptyCollections: Boolean,
treatStreamsAsArrays: Boolean = false,
explicitNulls: Boolean = false
)

object Config {
val default: Config = Config(ignoreEmptyCollections = false)
Expand Down Expand Up @@ -1089,7 +1093,7 @@ object JsonCodec {
case e: Throwable => throw new RuntimeException(s"Failed to encode field '${s.name}' in $schema'", e)
}
val value = s.get(a)
if (!isEmptyOptionalValue(s, value, cfg)) {
if (!isEmptyOptionalValue(s, value, cfg) && (!enc.isNothing(value) || cfg.explicitNulls)) {
if (first)
first = false
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import zio.Console._
import zio._
import zio.json.JsonDecoder.JsonError
import zio.json.ast.Json
import zio.json.{ DeriveJsonEncoder, JsonEncoder, jsonExplicitNull }
import zio.json.{ DeriveJsonEncoder, JsonEncoder }
import zio.schema.CaseSet._
import zio.schema._
import zio.schema.annotation._
Expand Down Expand Up @@ -121,39 +121,39 @@ object JsonCodecSpec extends ZIOSpecDefault {
Schema[ListAndMapAndOption],
ListAndMapAndOption(Nil, Map("foo" -> 1), Some("foo")),
"""{"map":{"foo":1},"option":"foo"}""",
JsonCodec.Config(ignoreEmptyCollections = true)
JsonCodec.Config(ignoreEmptyCollections = true, explicitNulls = false)
)
},
test("map empty") {
assertEncodesJson(
Schema[ListAndMapAndOption],
ListAndMapAndOption(List("foo"), Map.empty, Some("foo")),
"""{"list":["foo"],"option":"foo"}""",
JsonCodec.Config(ignoreEmptyCollections = true)
JsonCodec.Config(ignoreEmptyCollections = true, explicitNulls = false)
)
},
test("option empty") {
assertEncodesJson(
Schema[ListAndMapAndOption],
ListAndMapAndOption(List("foo"), Map("foo" -> 1), None),
"""{"list":["foo"],"map":{"foo":1}}""",
JsonCodec.Config(ignoreEmptyCollections = true)
JsonCodec.Config(ignoreEmptyCollections = true, explicitNulls = false)
)
},
test("all empty") {
assertEncodesJson(
Schema[ListAndMapAndOption],
ListAndMapAndOption(Nil, Map.empty, None),
"""{}""",
JsonCodec.Config(ignoreEmptyCollections = true)
JsonCodec.Config(ignoreEmptyCollections = true, explicitNulls = false)
)
},
test("all empty, but don't ignore empty collections") {
assertEncodesJson(
Schema[ListAndMapAndOption],
ListAndMapAndOption(Nil, Map.empty, None),
"""{"list":[],"map":{},"option":null}""",
JsonCodec.Config(ignoreEmptyCollections = false)
JsonCodec.Config(ignoreEmptyCollections = false, explicitNulls = true)
)
}
),
Expand Down Expand Up @@ -277,7 +277,7 @@ object JsonCodecSpec extends ZIOSpecDefault {
assertEncodes(
WithOptionFields.schema,
WithOptionFields(Some("s"), None),
charSequenceToByteChunk("""{"a":"s","b":null}""")
charSequenceToByteChunk("""{"a":"s"}""")
)
}
),
Expand Down Expand Up @@ -335,7 +335,7 @@ object JsonCodecSpec extends ZIOSpecDefault {
assertEncodes(
Subscription.schema,
Subscription.Unlimited(None),
charSequenceToByteChunk("""{"type":"unlimited","until":null}""")
charSequenceToByteChunk("""{"type":"unlimited"}""")
)
},
suite("with no discriminator")(
Expand Down Expand Up @@ -879,7 +879,7 @@ object JsonCodecSpec extends ZIOSpecDefault {
assertDecodes(
WithOptionFields.schema,
WithOptionFields(Some("s"), None),
charSequenceToByteChunk("""{"a":"s","b":null}""")
charSequenceToByteChunk("""{"a":"s"}""")
)
},
test("case class with option fields accept empty json object as value") {
Expand Down Expand Up @@ -1885,7 +1885,6 @@ object JsonCodecSpec extends ZIOSpecDefault {
charSequenceToByteChunk(encoded)
}

@jsonExplicitNull
case class SearchRequest(query: String, pageNumber: Int, resultPerPage: Int, nextPage: Option[String])

object SearchRequest {
Expand Down

0 comments on commit c1a1955

Please sign in to comment.