Skip to content

serde: use EncodeJSON and DecodeJSON for Avro serialization#2351

Merged
twmb merged 1 commit intomasterfrom
avro-encodejson
Mar 30, 2026
Merged

serde: use EncodeJSON and DecodeJSON for Avro serialization#2351
twmb merged 1 commit intomasterfrom
avro-encodejson

Conversation

@twmb
Copy link
Copy Markdown
Contributor

@twmb twmb commented 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.

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>
@twmb twmb force-pushed the avro-encodejson branch from 9f22a84 to 14d05a1 Compare March 30, 2026 19:58
@r-vasquez r-vasquez enabled auto-merge March 30, 2026 20:05
@r-vasquez r-vasquez disabled auto-merge March 30, 2026 20:18
@twmb
Copy link
Copy Markdown
Contributor Author

twmb commented Mar 30, 2026

Flaky test failure, proven by #2352, merging.

@twmb twmb enabled auto-merge March 30, 2026 20:26
@twmb twmb disabled auto-merge March 30, 2026 20:26
@twmb twmb enabled auto-merge (squash) March 30, 2026 20:26
@twmb twmb disabled auto-merge March 30, 2026 20:27
@twmb twmb merged commit 9db13eb into master Mar 30, 2026
40 of 44 checks passed
@twmb twmb deleted the avro-encodejson branch March 30, 2026 20:37
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.

2 participants