Skip to content

Commit

Permalink
Ways to ignore empty collections while encoding json
Browse files Browse the repository at this point in the history
  • Loading branch information
987Nabil committed Sep 7, 2023
1 parent c97c3dd commit e92fac1
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand Down Expand Up @@ -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
Expand All @@ -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))
Expand All @@ -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)))
}
Expand All @@ -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)))))
Expand All @@ -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)))
}

Expand All @@ -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")
Expand Down

0 comments on commit e92fac1

Please sign in to comment.