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 6, 2023
1 parent 1861252 commit f4e6de1
Show file tree
Hide file tree
Showing 5 changed files with 244 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,11 @@ object JsonSample extends zio.ZIOAppDefault {

override def run: ZIO[Environment with ZIOAppArgs, Any, Any] =
for {
_ <- ZIO.unit
person = Person("Michelle", 32)
personToJsonTransducer = JsonCodec.schemaBasedBinaryCodec[Person](schemaPerson).streamEncoder
_ <- ZIO.unit
person = Person("Michelle", 32)
personToJsonTransducer = JsonCodec
.schemaBasedBinaryCodec[Person](schemaPerson, JsonCodec.Config.default)
.streamEncoder
_ <- ZStream(person)
.via(personToJsonTransducer)
.via(ZPipeline.utf8Decode)
Expand Down Expand Up @@ -192,8 +194,8 @@ object CombiningExample extends ZIOAppDefault {
_ <- ZIO.debug("combining roundtrip")
person = Person("Michelle", 32)

personToJson = JsonCodec.schemaBasedBinaryCodec[Person](schemaPerson).streamEncoder
jsonToPerson = JsonCodec.schemaBasedBinaryCodec[Person](schemaPerson).streamDecoder
personToJson = JsonCodec.schemaBasedBinaryCodec[Person](schemaPerson, JsonCodec.Config.default).streamEncoder
jsonToPerson = JsonCodec.schemaBasedBinaryCodec[Person](schemaPerson, JsonCodec.Config.default).streamDecoder

personToProto = ProtobufCodec.protobufCodec[Person](schemaPerson).streamEncoder
protoToPerson = ProtobufCodec.protobufCodec[Person](schemaPerson).streamDecoder
Expand Down Expand Up @@ -226,10 +228,16 @@ object DictionaryExample extends ZIOAppDefault {
person = Person("Mike", 32)
dictionary = Map("m" -> person)
dictionaryToJson = JsonCodec
.schemaBasedBinaryCodec[scala.collection.immutable.Map[String, Person]](schemaPersonDictionaryFromMacro)
.schemaBasedBinaryCodec[scala.collection.immutable.Map[String, Person]](
schemaPersonDictionaryFromMacro,
JsonCodec.Config.default
)
.streamEncoder
jsonToDictionary = JsonCodec
.schemaBasedBinaryCodec[scala.collection.immutable.Map[String, Person]](schemaPersonDictionaryFromMacro)
.schemaBasedBinaryCodec[scala.collection.immutable.Map[String, Person]](
schemaPersonDictionaryFromMacro,
JsonCodec.Config.default
)
.streamDecoder
newPersonDictionary <- ZStream(dictionary)
.via(dictionaryToJson)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,11 @@ object JsonSample extends zio.ZIOAppDefault {

override def run: ZIO[Environment with ZIOAppArgs, Any, Any] =
for {
_ <- ZIO.unit
person = Person("Michelle", 32)
personToJsonPipeline = JsonCodec.schemaBasedBinaryCodec[Person](Person.schema).streamEncoder
_ <- ZIO.unit
person = Person("Michelle", 32)
personToJsonPipeline = JsonCodec
.schemaBasedBinaryCodec[Person](Person.schema, JsonCodec.Config.default)
.streamEncoder
_ <- ZStream(person)
.via(personToJsonPipeline)
.via(ZPipeline.utf8Decode)
Expand Down Expand Up @@ -201,8 +203,8 @@ object CombiningExample extends zio.ZIOAppDefault {
_ <- ZIO.debug("combining roundtrip")
person = Person("Michelle", 32)

personToJson = JsonCodec.schemaBasedBinaryCodec[Person](Person.schema).streamEncoder
jsonToPerson = JsonCodec.schemaBasedBinaryCodec[Person](Person.schema).streamDecoder
personToJson = JsonCodec.schemaBasedBinaryCodec[Person](Person.schema, JsonCodec.Config.default).streamEncoder
jsonToPerson = JsonCodec.schemaBasedBinaryCodec[Person](Person.schema, JsonCodec.Config.default).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 @@ -73,16 +73,24 @@ object Example3 extends ZIOAppDefault {
_ <- ZIO.debug("input JSON : " + json)

// get objects from JSON
personDTO <- ZIO.fromEither(JsonCodec.schemaBasedBinaryCodec[PersonDTO](PersonDTO.schema).decode(chunks))
person <- ZIO.fromEither(JsonCodec.schemaBasedBinaryCodec[Person](personTransformation).decode(chunks))
_ <- ZIO.debug("PersonDTO : " + personDTO)
_ <- ZIO.debug("Person : " + person)
personDTO <- ZIO.fromEither(
JsonCodec.schemaBasedBinaryCodec[PersonDTO](PersonDTO.schema, JsonCodec.Config.default).decode(chunks)
)
person <- ZIO.fromEither(
JsonCodec.schemaBasedBinaryCodec[Person](personTransformation, JsonCodec.Config.default).decode(chunks)
)
_ <- ZIO.debug("PersonDTO : " + personDTO)
_ <- ZIO.debug("Person : " + person)

// get JSON from Objects
personJson = new String(JsonCodec.schemaBasedBinaryCodec[Person](Person.schema).encode(person).toArray)
personDTOJson = new String(JsonCodec.schemaBasedBinaryCodec[Person](personTransformation).encode(person).toArray)
_ <- ZIO.debug("Person JSON: " + personJson)
_ <- ZIO.debug("PersonDTO JSON: " + personDTOJson)
personJson = new String(
JsonCodec.schemaBasedBinaryCodec[Person](Person.schema, JsonCodec.Config.default).encode(person).toArray
)
personDTOJson = new String(
JsonCodec.schemaBasedBinaryCodec[Person](personTransformation, JsonCodec.Config.default).encode(person).toArray
)
_ <- ZIO.debug("Person JSON: " + personJson)
_ <- ZIO.debug("PersonDTO JSON: " + personDTOJson)

} yield ()
}
Loading

0 comments on commit f4e6de1

Please sign in to comment.