Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Parse strings as other types #373

Closed
joshlf opened this issue Oct 23, 2017 · 3 comments
Closed

Feature Request: Parse strings as other types #373

joshlf opened this issue Oct 23, 2017 · 3 comments

Comments

@joshlf
Copy link

joshlf commented Oct 23, 2017

The GDAX API full channel is a JSON API whose messages include numerical values that are for some reason encoded as strings. In order to parse these messages, I've had to implement manual parsing, but it'd be nice if there were some attribute that I could use to instruct serde_json to attempt to parse string-typed values as other types.

Example message from that API:

{
    "type": "received",
    "time": "2014-11-09T08:19:27.028459Z",
    "product_id": "BTC-USD",
    "sequence": 12,
    "order_id": "dddec984-77a8-460a-b958-66f114b0de9b",
    "funds": "3000.234",
    "side": "buy",
    "order_type": "market"
}
@dtolnay
Copy link
Member

dtolnay commented Oct 23, 2017

The from_str helper from #317 (comment) looks like it would work for any type that implements FromStr. Does that cover all the types in the GDAX API full channel?

@joshlf
Copy link
Author

joshlf commented Oct 24, 2017

Yeah, that's roughly what I've done in my implementation.

However, this gets tricky when you want to nest those fields. For example, if instead of having an f64 field, you want it to be optional, making it an Option<f64> field, then the attribute isn't sufficient. I ended up writing my own type - struct F64(f64) - and then using Option<F64>. It'd be nice if instead I could just do something like:

struct Foo {
    #[serde(deserialize_string)]
    bar: Option<f64>,
}

@dtolnay
Copy link
Member

dtolnay commented Nov 5, 2017

Yes the interaction of deserialize_with and Option still needs to be implemented. Let's follow up in serde-rs/serde#723.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants