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

Decoding floats and ints wrapped in quotes #49

Open
CSNoyes opened this issue Dec 19, 2017 · 1 comment
Open

Decoding floats and ints wrapped in quotes #49

CSNoyes opened this issue Dec 19, 2017 · 1 comment

Comments

@CSNoyes
Copy link

CSNoyes commented Dec 19, 2017

I'm working with an API that returns floats and ints wrapped in quotes. A typical response is included below. In the examples (and in canonical JSON afaik) ints and floats aren't quote wrapped. I get an "invalid floating point number" error, which I guess is expected given that. Is there a way to add an inner encoder that strips the quotes to get them ready for parsing by the double-conversion lib?

["buy", "1", "3"]

@punchfox
Copy link
Contributor

We were considering adding a codec that would string encode in the way you describe, wrapping a typed codec, i.e., quote(number()). We haven't gotten around doing that yet though. The codec itself would be quite easy to write, first using a string codec to decode the string into a std::string followed by decode by the wrapped codec. This could also be done using the transform() codec, with a bit more manual work.

Neither solution would work in your example, where the quoted values are both numbers and strings. The number() codec will fail to parse "buy" in this case. Instead I'd recommend parsing this array into the following C++ type: std::vector<json::encoded_value>. The encoded_value represents some JSON value, such as "buy" or "3". You can then decode them afterwards using json::decode(...) or json::try_decode(...).

If you know the format of these arrays, for example [<string>, <integer>, <integer>] you can decode the JSON using the tuple() codec. Just make a std::tuple<std::string, int, int> and the default codec should decode it correctly for you.

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

No branches or pull requests

2 participants