Skip to content

Commit

Permalink
Ways to ignore empty collections while encoding json (#605)
Browse files Browse the repository at this point in the history
* Ways to ignore empty collections while encoding json

* Ways to ignore empty collections while encoding json
  • Loading branch information
987Nabil authored Sep 7, 2023
1 parent a1d38fc commit aa0770e
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 101 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)
.streamEncoder
_ <- ZStream(person)
.via(personToJsonTransducer)
.via(ZPipeline.utf8Decode)
Expand Down Expand Up @@ -226,10 +228,14 @@ 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
)
.streamEncoder
jsonToDictionary = JsonCodec
.schemaBasedBinaryCodec[scala.collection.immutable.Map[String, Person]](schemaPersonDictionaryFromMacro)
.schemaBasedBinaryCodec[scala.collection.immutable.Map[String, Person]](
schemaPersonDictionaryFromMacro
)
.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)
.streamEncoder
_ <- ZStream(person)
.via(personToJsonPipeline)
.via(ZPipeline.utf8Decode)
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).decode(chunks)
)
person <- ZIO.fromEither(
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).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).encode(person).toArray
)
personDTOJson = new String(
JsonCodec.schemaBasedBinaryCodec[Person](personTransformation).encode(person).toArray
)
_ <- ZIO.debug("Person JSON: " + personJson)
_ <- ZIO.debug("PersonDTO JSON: " + personDTOJson)

} yield ()
}
Loading

0 comments on commit aa0770e

Please sign in to comment.