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

Support unambiguous JSON (from/to) #283

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

Emptyless
Copy link

@Emptyless Emptyless commented Mar 15, 2024

NewCodecForUnambiguousJSON provides full serialization/deserialization for json that is unambiguous in terms of what the field will contain. This means that avro Union types containing only a single concrete type e.g. ["null", "string"] no longer have to specify their type. Unlike NewCodecForStandardJSONFull, ambiguous types ["int", "string"] do still
need to specify their type as map. See the following examples:

["null", "string"] => "some string" || null
["int", "string"] => {"int": 1} || {"string": "some string"}
["null", "int", "string"] => null || {"int": 1} || {"string": "some string"}

this is especially useful when using json.Marshal, json.Unmarshal with structs containing
optional types:

type Person struct {
   Name *string `json:"name,omitempty"`
}

or using json.Marshal,json.Unmarshal with structs containing a union:

type Message struct {
   Direction DirectionUnion `json:DirectionUnion"
}

type DirectionUnion struct { // only one of the fields can be non-nil
   Request *string `json:"request,omitempty"`
   Response *string `json:"response,omitempty"`
}

I have added example tests for (un)ambiguous nil, primitive and records native-to-textual and vice-versa

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant