Skip to content

Deserializing arbitrary precision numbers #896

@01mf02

Description

@01mf02

Hello! I'm seeking to improve the performance of a program that reads JSON data as serde_json::Value and immediately converts it to a custom type.

In my program jaq, I create a Value type that should hold JSON values. I then parse JSON to serde_json::Value and convert them to my custom Value type. So far, so good.

Recently, I found that converting from serde_json::Value to my own Value type takes quite some time, namely roughly 10% of my total program runtime.

To avoid creating serde_json::Values and then converting them to my own Values, I have tried to implement Deserialize for my custom Value type; see playground.
That allows me to bypass serde_json::Value, except for one point: numbers.
In jaq, I use the arbitrary_precision feature of serde_json. With that, a floating-point number "1.0" becomes:

Map({"$serde_json::private::Number": String("1.0")})

(This is not visible if you run the example from the playground, because the playground does not set the arbitrary_precision feature.)

In serde_json, such number-representing maps are converted to a number using the private types KeyClassifier / KeyClass; however, these types are inaccessible to me from my program, so I cannot use them. Hardcoding the string "$serde_json::private::Number" in my program seems quite dangerous, so that's not a route I want to take.

Is there some way to deserialize JSON values, including arbitrary precision numbers, from serde_json without going through serde_json::Value?

In other words, is it possible to reimplement serde_json::Value outside of the serde_json crate (without duplicating the parsing of serde_json)?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions