Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ways to ignore empty collections while encoding json #605

Merged
merged 2 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading