serde: use EncodeJSON and DecodeJSON for Avro serialization#2351
Merged
Conversation
weeco
approved these changes
Mar 30, 2026
Replace json.Marshal with Schema.EncodeJSON for Avro-to-JSON
serialization. EncodeJSON is schema-aware and correctly handles:
- bytes/fixed fields as \uXXXX strings (json.Marshal base64-encodes)
- NaN as "NaN" and ±Infinity as "Infinity"/"-Infinity" strings
(json.Marshal errors on these values)
- time.Duration as the original millis/micros integer
(json.Marshal writes nanoseconds)
Replace json.Unmarshal with Schema.DecodeJSON for JSON-to-Avro
deserialization. DecodeJSON handles Avro JSON union wrappers
({"string": "hello"}) which json.Unmarshal passes through as
opaque maps, causing encoding failures for union fields.
Replace avro.NewSchemaCache() with zero-value avro.SchemaCache
(NewSchemaCache was removed; zero value is now ready to use).
Note: EncodeJSON produces bare union values by default. If the
previous json.Marshal output was consumed by external systems
expecting a specific format, the bytes and float special value
representations will differ.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Contributor
Author
|
Flaky test failure, proven by #2352, merging. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Replace json.Marshal with Schema.EncodeJSON for Avro-to-JSON serialization. EncodeJSON is schema-aware and correctly handles:
Replace json.Unmarshal with Schema.DecodeJSON for JSON-to-Avro deserialization. DecodeJSON handles Avro JSON union wrappers ({"string": "hello"}) which json.Unmarshal passes through as opaque maps, causing encoding failures for union fields.
Replace avro.NewSchemaCache() with zero-value avro.SchemaCache (NewSchemaCache was removed; zero value is now ready to use).
Note: EncodeJSON produces bare union values by default. If the previous json.Marshal output was consumed by external systems expecting a specific format, the bytes and float special value representations will differ.