Skip to content

feat: Add the Avro JSON encoding for writing serde types - #750

Open
janwaleed09 wants to merge 2 commits into
apache:mainfrom
janwaleed09:feat/json_encoding
Open

janwaleed09 wants to merge 2 commits into
apache:mainfrom
janwaleed09:feat/json_encoding

Conversation

@janwaleed09

Copy link
Copy Markdown
Contributor

This is the writing half of #518. Reading needs a decision first, see the question at the end.

It adds a json module with to_writer, to_vec and to_string, plus a JsonWriter that you can build from a GenericDatumWriter when you need schemata or a different human_readable setting:

let json = apache_avro::json::to_string(&schema, &reading)?;
assert_eq!(json, r#"{"sensor":"a","value":{"double":1.5}}"#);

The issue suggests a second serializer that drives serde_json. I went a different way: SchemaAwareSerializer is now generic over a small internal Encoding trait, a bit like Java's split between DatumWriter and Encoder. Union branch selection, logical types, field order and defaults stay in one place, so a type is written to JSON exactly the way it is written to binary.

The first commit only does that refactor and doesn't change the binary output. What JSON needs on top, closing a union branch and the commas between items, is an associated type that is zero-sized for Binary, so the binary serializers don't get any bigger. The second commit adds the JSON encoding and the public API. It's probably easiest to review the two commits separately.

I also tried writing binary first and converting that to JSON. That gives the same output without touching ser_schema, but it was about 1.5x slower, so I didn't go with it.

with_different_schema is gone, because the serializer now has to write something after the inner value. #528 uses it, so whichever of the two lands second will need a small rebase.

The output follows the spec and, where the spec doesn't say, Java: the null branch of a union is a plain null, other branches are {"<full name>": value}, bytes and fixed are code points 0-255, NaN, Infinity and -Infinity are strings, and logical types are written in the form of their underlying type.

How I tested it:

  • 17 new tests, and all existing tests pass; two test helpers only needed the new encoding field
  • compared with avro-tools 1.12.2 for 39 values: jsontofrag turns our JSON back into the same binary, and fragtojson gives the same JSON apart from number and escape formatting
  • ran the CI checks locally on macOS with stable, nightly and 1.89.0, plus the wasm32 build
  • benches/serde.rs shows no change on the binary side beyond noise

There are no new dependencies and nothing breaking; the public API only gets the json module.

@Kriskras99, a question about reading: SchemaAwareDeserializer pulls from its input, but serde_json has no public pull parser. I can see three ways: a small JSON tokenizer in the crate, a dependency that has one (or sonic-rs if #672 lands), or JSON → binary → SchemaAwareDeserializer, which needs no changes in deser_schema but is slower. It also touches the same code as #575. Which would you prefer?

@Kriskras99

Copy link
Copy Markdown
Contributor

I think this is a good approach, but it will take some time to review it completely.

For the deserializer I think I prefer using sonic-rs, as I'm planning to use that as a dependency anyway.

@janwaleed09

Copy link
Copy Markdown
Contributor Author

For the deserializer I think I prefer using sonic-rs, as I'm planning to use that as a dependency anyway.

Thanks i will build the reading side on sonic-rs then as a separate PR once this one is in. Two things I noticed while trying it: the web-assembly job of #672 fails because its lockfile still has ahash 0.8.12, which pulls in getrandom 0.3; cargo update -p ahash fixes that, since getrandom 0.4 is already in the lockfile. And sonic-rs currently parses -0.0 as 0.0 (cloudwego/sonic-rs#248) so the reader will need a small workaround for doubles. Ive also rebased this on main since #751 touched the same line

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