Repository navigation
feat: Add the Avro JSON encoding for writing serde types - #750
janwaleed09 wants to merge 2 commits into
Conversation
|
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 |
54d23e1 to
142cab5
Compare
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 |
This is the writing half of #518. Reading needs a decision first, see the question at the end.
It adds a
jsonmodule withto_writer,to_vecandto_string, plus aJsonWriterthat you can build from aGenericDatumWriterwhen you needschemataor a differenthuman_readablesetting:The issue suggests a second serializer that drives
serde_json. I went a different way:SchemaAwareSerializeris now generic over a small internalEncodingtrait, a bit like Java's split betweenDatumWriterandEncoder. 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_schemais 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
nullbranch of a union is a plainnull, other branches are{"<full name>": value},bytesandfixedare code points 0-255,NaN,Infinityand-Infinityare strings, and logical types are written in the form of their underlying type.How I tested it:
encodingfieldavro-tools1.12.2 for 39 values:jsontofragturns our JSON back into the same binary, andfragtojsongives the same JSON apart from number and escape formattingbenches/serde.rsshows no change on the binary side beyond noiseThere are no new dependencies and nothing breaking; the public API only gets the
jsonmodule.@Kriskras99, a question about reading:
SchemaAwareDeserializerpulls from its input, butserde_jsonhas no public pull parser. I can see three ways: a small JSON tokenizer in the crate, a dependency that has one (orsonic-rsif #672 lands), or JSON → binary →SchemaAwareDeserializer, which needs no changes indeser_schemabut is slower. It also touches the same code as #575. Which would you prefer?