From e92fac12dd77bb2a31577e2cba47cbc771bd1958 Mon Sep 17 00:00:00 2001 From: Nabil Abdel-Hafeez <7283535+987Nabil@users.noreply.github.com> Date: Thu, 7 Sep 2023 13:35:59 +0200 Subject: [PATCH] Ways to ignore empty collections while encoding json --- .../zio/schema/example/example1/Example1.scala | 12 +++++------- .../zio/schema/example/example2/Example2.scala | 6 +++--- .../zio/schema/example/example3/Example3.scala | 8 ++++---- .../scala/zio/schema/codec/JsonCodec.scala | 2 +- .../zio/schema/codec/JsonCodecSpec.scala | 18 +++++++++--------- 5 files changed, 22 insertions(+), 24 deletions(-) diff --git a/zio-schema-examples/shared/src/main/scala/dev/zio/schema/example/example1/Example1.scala b/zio-schema-examples/shared/src/main/scala/dev/zio/schema/example/example1/Example1.scala index b1736f561..c2df72747 100644 --- a/zio-schema-examples/shared/src/main/scala/dev/zio/schema/example/example1/Example1.scala +++ b/zio-schema-examples/shared/src/main/scala/dev/zio/schema/example/example1/Example1.scala @@ -148,7 +148,7 @@ object JsonSample extends zio.ZIOAppDefault { _ <- ZIO.unit person = Person("Michelle", 32) personToJsonTransducer = JsonCodec - .schemaBasedBinaryCodec[Person](schemaPerson, JsonCodec.Config.default) + .schemaBasedBinaryCodec[Person](schemaPerson) .streamEncoder _ <- ZStream(person) .via(personToJsonTransducer) @@ -194,8 +194,8 @@ object CombiningExample extends ZIOAppDefault { _ <- ZIO.debug("combining roundtrip") person = Person("Michelle", 32) - personToJson = JsonCodec.schemaBasedBinaryCodec[Person](schemaPerson, JsonCodec.Config.default).streamEncoder - jsonToPerson = JsonCodec.schemaBasedBinaryCodec[Person](schemaPerson, JsonCodec.Config.default).streamDecoder + personToJson = JsonCodec.schemaBasedBinaryCodec[Person](schemaPerson).streamEncoder + jsonToPerson = JsonCodec.schemaBasedBinaryCodec[Person](schemaPerson).streamDecoder personToProto = ProtobufCodec.protobufCodec[Person](schemaPerson).streamEncoder protoToPerson = ProtobufCodec.protobufCodec[Person](schemaPerson).streamDecoder @@ -229,14 +229,12 @@ object DictionaryExample extends ZIOAppDefault { dictionary = Map("m" -> person) dictionaryToJson = JsonCodec .schemaBasedBinaryCodec[scala.collection.immutable.Map[String, Person]]( - schemaPersonDictionaryFromMacro, - JsonCodec.Config.default + schemaPersonDictionaryFromMacro ) .streamEncoder jsonToDictionary = JsonCodec .schemaBasedBinaryCodec[scala.collection.immutable.Map[String, Person]]( - schemaPersonDictionaryFromMacro, - JsonCodec.Config.default + schemaPersonDictionaryFromMacro ) .streamDecoder newPersonDictionary <- ZStream(dictionary) diff --git a/zio-schema-examples/shared/src/main/scala/dev/zio/schema/example/example2/Example2.scala b/zio-schema-examples/shared/src/main/scala/dev/zio/schema/example/example2/Example2.scala index 833ee869d..83a664e65 100644 --- a/zio-schema-examples/shared/src/main/scala/dev/zio/schema/example/example2/Example2.scala +++ b/zio-schema-examples/shared/src/main/scala/dev/zio/schema/example/example2/Example2.scala @@ -159,7 +159,7 @@ object JsonSample extends zio.ZIOAppDefault { _ <- ZIO.unit person = Person("Michelle", 32) personToJsonPipeline = JsonCodec - .schemaBasedBinaryCodec[Person](Person.schema, JsonCodec.Config.default) + .schemaBasedBinaryCodec[Person](Person.schema) .streamEncoder _ <- ZStream(person) .via(personToJsonPipeline) @@ -203,8 +203,8 @@ object CombiningExample extends zio.ZIOAppDefault { _ <- ZIO.debug("combining roundtrip") person = Person("Michelle", 32) - personToJson = JsonCodec.schemaBasedBinaryCodec[Person](Person.schema, JsonCodec.Config.default).streamEncoder - jsonToPerson = JsonCodec.schemaBasedBinaryCodec[Person](Person.schema, JsonCodec.Config.default).streamDecoder + personToJson = JsonCodec.schemaBasedBinaryCodec[Person](Person.schema).streamEncoder + jsonToPerson = JsonCodec.schemaBasedBinaryCodec[Person](Person.schema).streamDecoder personToProto = ProtobufCodec.protobufCodec[Person](Person.schema).streamEncoder protoToPerson = ProtobufCodec.protobufCodec[Person](Person.schema).streamDecoder diff --git a/zio-schema-examples/shared/src/main/scala/dev/zio/schema/example/example3/Example3.scala b/zio-schema-examples/shared/src/main/scala/dev/zio/schema/example/example3/Example3.scala index 2c33a3064..164f3f614 100644 --- a/zio-schema-examples/shared/src/main/scala/dev/zio/schema/example/example3/Example3.scala +++ b/zio-schema-examples/shared/src/main/scala/dev/zio/schema/example/example3/Example3.scala @@ -74,20 +74,20 @@ object Example3 extends ZIOAppDefault { // get objects from JSON personDTO <- ZIO.fromEither( - JsonCodec.schemaBasedBinaryCodec[PersonDTO](PersonDTO.schema, JsonCodec.Config.default).decode(chunks) + JsonCodec.schemaBasedBinaryCodec[PersonDTO](PersonDTO.schema).decode(chunks) ) person <- ZIO.fromEither( - JsonCodec.schemaBasedBinaryCodec[Person](personTransformation, JsonCodec.Config.default).decode(chunks) + JsonCodec.schemaBasedBinaryCodec[Person](personTransformation).decode(chunks) ) _ <- ZIO.debug("PersonDTO : " + personDTO) _ <- ZIO.debug("Person : " + person) // get JSON from Objects personJson = new String( - JsonCodec.schemaBasedBinaryCodec[Person](Person.schema, JsonCodec.Config.default).encode(person).toArray + JsonCodec.schemaBasedBinaryCodec[Person](Person.schema).encode(person).toArray ) personDTOJson = new String( - JsonCodec.schemaBasedBinaryCodec[Person](personTransformation, JsonCodec.Config.default).encode(person).toArray + JsonCodec.schemaBasedBinaryCodec[Person](personTransformation).encode(person).toArray ) _ <- ZIO.debug("Person JSON: " + personJson) _ <- ZIO.debug("PersonDTO JSON: " + personDTOJson) diff --git a/zio-schema-json/shared/src/main/scala/zio/schema/codec/JsonCodec.scala b/zio-schema-json/shared/src/main/scala/zio/schema/codec/JsonCodec.scala index 2a3446168..595f1c408 100644 --- a/zio-schema-json/shared/src/main/scala/zio/schema/codec/JsonCodec.scala +++ b/zio-schema-json/shared/src/main/scala/zio/schema/codec/JsonCodec.scala @@ -68,7 +68,7 @@ object JsonCodec { implicit def schemaBasedBinaryCodec[A](implicit schema: Schema[A]): BinaryCodec[A] = schemaBasedBinaryCodec[A](JsonCodec.Config.default) - implicit def schemaBasedBinaryCodec[A](cfg: Config)(implicit schema: Schema[A]): BinaryCodec[A] = + def schemaBasedBinaryCodec[A](cfg: Config)(implicit schema: Schema[A]): BinaryCodec[A] = new BinaryCodec[A] { override def decode(whole: Chunk[Byte]): Either[DecodeError, A] = JsonDecoder.decode( diff --git a/zio-schema-json/shared/src/test/scala-2/zio/schema/codec/JsonCodecSpec.scala b/zio-schema-json/shared/src/test/scala-2/zio/schema/codec/JsonCodecSpec.scala index 868befead..81db36e62 100644 --- a/zio-schema-json/shared/src/test/scala-2/zio/schema/codec/JsonCodecSpec.scala +++ b/zio-schema-json/shared/src/test/scala-2/zio/schema/codec/JsonCodecSpec.scala @@ -1171,8 +1171,8 @@ object JsonCodecSpec extends ZIOSpecDefault { val dyn = DynamicValue.fromSchemaAndValue(schema, value) ZStream .succeed(dyn) - .via(JsonCodec.schemaBasedBinaryCodec(Schema.dynamicValue, JsonCodec.Config.default).streamEncoder) - .via(JsonCodec.schemaBasedBinaryCodec(Schema.dynamicValue, JsonCodec.Config.default).streamDecoder) + .via(JsonCodec.schemaBasedBinaryCodec(Schema.dynamicValue).streamEncoder) + .via(JsonCodec.schemaBasedBinaryCodec(Schema.dynamicValue).streamDecoder) .map(_.toTypedValue(schema)) .runHead .map { result => @@ -1215,7 +1215,7 @@ object JsonCodecSpec extends ZIOSpecDefault { ) = { val stream = ZStream .succeed(value) - .via(JsonCodec.schemaBasedBinaryCodec(schema, cfg).streamEncoder) + .via(JsonCodec.schemaBasedBinaryCodec(cfg)(schema).streamEncoder) .runCollect .tap { chunk => printLine(s"${new String(chunk.toArray)}").when(print).ignore @@ -1231,7 +1231,7 @@ object JsonCodecSpec extends ZIOSpecDefault { ) = { val stream = ZStream .succeed(value) - .via(JsonCodec.schemaBasedBinaryCodec[A](schema, cfg).streamEncoder) + .via(JsonCodec.schemaBasedBinaryCodec[A](cfg)(schema).streamEncoder) .runCollect .map(chunk => new String(chunk.toArray)) assertZIO(stream)(equalTo(json)) @@ -1240,7 +1240,7 @@ object JsonCodecSpec extends ZIOSpecDefault { private def assertEncodesJson[A](schema: Schema[A], value: A)(implicit enc: JsonEncoder[A]) = { val stream = ZStream .succeed(value) - .via(JsonCodec.schemaBasedBinaryCodec[A](schema, JsonCodec.Config.default).streamEncoder) + .via(JsonCodec.schemaBasedBinaryCodec[A](schema).streamEncoder) .runCollect assertZIO(stream)(equalTo(jsonEncoded(value))) } @@ -1253,7 +1253,7 @@ object JsonCodecSpec extends ZIOSpecDefault { ) = { val stream = ZStream .fromChunk(charSequenceToByteChunk(json)) - .via(JsonCodec.schemaBasedBinaryCodec[A](schema, cfg).streamDecoder) + .via(JsonCodec.schemaBasedBinaryCodec[A](cfg)(schema).streamDecoder) .catchAll(ZStream.succeed[DecodeError](_)) .runHead assertZIO(stream)(isSome(equalTo(ReadError(Cause.empty, JsonError.render(errors))))) @@ -1265,7 +1265,7 @@ object JsonCodecSpec extends ZIOSpecDefault { chunk: Chunk[Byte], cfg: JsonCodec.Config = JsonCodec.Config.default ) = { - val result = ZStream.fromChunk(chunk).via(JsonCodec.schemaBasedBinaryCodec[A](schema, cfg).streamDecoder).runCollect + val result = ZStream.fromChunk(chunk).via(JsonCodec.schemaBasedBinaryCodec[A](cfg)(schema).streamDecoder).runCollect assertZIO(result)(equalTo(Chunk(value))) } @@ -1283,13 +1283,13 @@ object JsonCodecSpec extends ZIOSpecDefault { ZStream .succeed(value) .tap(value => printLine(s"Input Value: $value").when(print).ignore) - .via(JsonCodec.schemaBasedBinaryCodec[A1](encodingSchema, cfg).streamEncoder) + .via(JsonCodec.schemaBasedBinaryCodec[A1](cfg)(encodingSchema).streamEncoder) .runCollect .tap(encoded => printLine(s"Encoded: ${new String(encoded.toArray)}").when(print).ignore) .flatMap { encoded => ZStream .fromChunk(encoded) - .via(JsonCodec.schemaBasedBinaryCodec[A2](decodingSchema, cfg).streamDecoder) + .via(JsonCodec.schemaBasedBinaryCodec[A2](cfg)(decodingSchema).streamDecoder) .runCollect .tapError { err => printLineError(s"Decoding failed for input ${new String(encoded.toArray)}\nError Message: $err")