apoelstra/strason

Feedback on serde_json arbitrary precision implementation

Closed this issue · 6 comments

I implemented arbitrary precision number parsing for serde_json: serde-rs/json#252.

I would love your feedback on whether you would have still created this library if our implementation had existed at the time. In particular, the new serde_json "arbitrary_precision" feature is equivalent to your stringly typed number parsing and the somewhat older serde_json "preserve_order" feature is equivalent to using Vec<(String, Json)> to store objects, except avoids a linear scan for lookup and does not allow duplicate keys.

I don't think our implementation entirely supersedes this library because ours relies on specialization, so a nightly compiler. I would still be interested to hear your perspective and whether the PR would have addressed your use cases.

Hi @dtolnay. Yes, if your PR had existed and worked on a stable compiler at the time, I believe there would have been no reason for strason to exist.

I don't have any further insight, sorry -- my code that uses strason is used to interface with the Bitcoin Core RPC interface, and I haven't touched it in a very long time.

Thanks! That's helpful.

@dtolnay Very nice work on this, thanks. I've had a look at some of the diffs, and it solves my use case, I believe. The only thing I couldn't quite figure out is how specialisation fits into the picture. Maybe you could give a brief overview of this? (Or perhaps it's just late, and I'm missing something obvious.)

Specialization is how we break parametricity and treat some specific types differently from generic types T. Search for "default fn" in the PR - there are three of them:

  • The JSON deserializer specializes for deserializing Number and Value vs generic other types.
  • The Number serializer specializes for serializing to JSON and Value vs generic other formats.
  • The Number deserializer specializes for deserializing Number and Value vs generic other types.

Ah okay, I think that's all clear now, thanks. (The three-way symmetry between JSON text, Number, and Value makes sense.)