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

fix: handle missing collection fields in JSON decoding #688

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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 @@ -3,6 +3,7 @@ package dev.zio.schema.example.example1
import zio._
import zio.schema.{ DeriveSchema, Schema, TypeId }
import zio.stream.ZPipeline
import zio.schema.codec.JsonCodec

/**
* Example 1 of ZIO-Schema:
Expand Down Expand Up @@ -142,13 +143,14 @@ object JsonSample extends zio.ZIOAppDefault {
import ManualConstruction._
import zio.schema.codec.JsonCodec
import zio.stream.ZStream
implicit val defaultConfig: JsonCodec.Config = JsonCodec.Config.default

override def run: ZIO[Environment with ZIOAppArgs, Any, Any] =
for {
_ <- ZIO.unit
person = Person("Michelle", 32)
personToJsonTransducer = JsonCodec
.schemaBasedBinaryCodec[Person](schemaPerson)
.schemaBasedBinaryCodec[Person](schemaPerson, defaultConfig)
.streamEncoder
_ <- ZStream(person)
.via(personToJsonTransducer)
Expand Down Expand Up @@ -187,15 +189,16 @@ object CombiningExample extends ZIOAppDefault {
import ManualConstruction._
import zio.schema.codec.{ JsonCodec, ProtobufCodec }
import zio.stream.ZStream
implicit val defaultConfig: JsonCodec.Config = JsonCodec.Config.default

override def run: ZIO[Environment with ZIOAppArgs, Any, Any] =
for {
_ <- ZIO.unit
_ <- 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, defaultConfig).streamEncoder
jsonToPerson = JsonCodec.schemaBasedBinaryCodec[Person](schemaPerson, defaultConfig).streamDecoder

personToProto = ProtobufCodec.protobufCodec[Person](schemaPerson).streamEncoder
protoToPerson = ProtobufCodec.protobufCodec[Person](schemaPerson).streamDecoder
Expand All @@ -222,19 +225,23 @@ object DictionaryExample extends ZIOAppDefault {
import MacroConstruction._
import zio.schema.codec.JsonCodec
import zio.stream.ZStream
implicit val defaultConfig: JsonCodec.Config = JsonCodec.Config.default

override def run: ZIO[Environment with ZIOAppArgs, Any, Any] =
for {
_ <- ZIO.unit
person = Person("Mike", 32)
dictionary = Map("m" -> person)
dictionaryToJson = JsonCodec
.schemaBasedBinaryCodec[scala.collection.immutable.Map[String, Person]](
schemaPersonDictionaryFromMacro
schemaPersonDictionaryFromMacro,
defaultConfig
)
.streamEncoder
jsonToDictionary = JsonCodec
.schemaBasedBinaryCodec[scala.collection.immutable.Map[String, Person]](
schemaPersonDictionaryFromMacro
schemaPersonDictionaryFromMacro,
defaultConfig
)
.streamDecoder
newPersonDictionary <- ZStream(dictionary)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,14 @@ import dev.zio.schema.example.example2.Domain._
object JsonSample extends zio.ZIOAppDefault {
import zio.schema.codec.JsonCodec
import zio.stream.ZStream
implicit val defaultConfig: JsonCodec.Config = JsonCodec.Config.default

override def run: ZIO[Environment with ZIOAppArgs, Any, Any] =
for {
_ <- ZIO.unit
person = Person("Michelle", 32)
personToJsonPipeline = JsonCodec
.schemaBasedBinaryCodec[Person](Person.schema)
.schemaBasedBinaryCodec[Person](Person.schema, defaultConfig)
.streamEncoder
_ <- ZStream(person)
.via(personToJsonPipeline)
Expand Down Expand Up @@ -196,15 +197,16 @@ object ProtobufExample extends ZIOAppDefault {
object CombiningExample extends zio.ZIOAppDefault {
import zio.schema.codec.{ JsonCodec, ProtobufCodec }
import zio.stream.ZStream
implicit val defaultConfig: JsonCodec.Config = JsonCodec.Config.default

override def run: ZIO[Environment with ZIOAppArgs, Any, Any] =
for {
_ <- ZIO.unit
_ <- 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, defaultConfig).streamEncoder
jsonToPerson = JsonCodec.schemaBasedBinaryCodec[Person](Person.schema, defaultConfig).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 @@ -57,6 +57,7 @@ private[example3] object Domain {
object Example3 extends ZIOAppDefault {
import dev.zio.schema.example.example3.Domain._
import zio.schema.codec.JsonCodec
implicit val defaultConfig: JsonCodec.Config = JsonCodec.Config.default

val personTransformation: Schema[Person] = PersonDTO.schema.transform[Person](
(dto: PersonDTO) => Person(dto.firstname + " " + dto.lastname, dto.years),
Expand All @@ -74,20 +75,20 @@ object Example3 extends ZIOAppDefault {

// get objects from JSON
personDTO <- ZIO.fromEither(
JsonCodec.schemaBasedBinaryCodec[PersonDTO](PersonDTO.schema).decode(chunks)
JsonCodec.schemaBasedBinaryCodec[PersonDTO](PersonDTO.schema, defaultConfig).decode(chunks)
)
person <- ZIO.fromEither(
JsonCodec.schemaBasedBinaryCodec[Person](personTransformation).decode(chunks)
JsonCodec.schemaBasedBinaryCodec[Person](personTransformation, defaultConfig).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
JsonCodec.schemaBasedBinaryCodec[Person](Person.schema, defaultConfig).encode(person).toArray
)
personDTOJson = new String(
JsonCodec.schemaBasedBinaryCodec[Person](personTransformation).encode(person).toArray
JsonCodec.schemaBasedBinaryCodec[Person](personTransformation, defaultConfig).encode(person).toArray
)
_ <- ZIO.debug("Person JSON: " + personJson)
_ <- ZIO.debug("PersonDTO JSON: " + personDTOJson)
Expand Down
Loading
Loading