Parsing numbers that have decimal places

Sep 24, 2014 at 8:29 PM
Hi and thanks for a great library that is working very well in my projects.

I have a very particular need to parse decimal numbers. Currently the library uses double as the "base number type" if I can put it that way and casts out of that type for any other numbers which is perfectly legitimate in most cases.

I have the requirement to store very strict decimals that get passed into my API as json numbers. This becomes difficult due to the precision and scale requirements that I have as I cannot tell from the json number value whether a value is actually what was passed in - or whether it is just part of the double data type representing my number.

With this in mind is there any way to get at the actual string value (from my stream) that was passed into the parse method that generated the number value? I would ideally like to get at this without having to home grow a container of some sorts to capture the stream and parse it separately.

The call to serialize() a number value returns the string representation of the double value, I would like to get at the original string value. I cannot unfortunately get my consumers to send numbers as strings.

Any help appreciated.

Thanks
Sep 24, 2014 at 8:59 PM
Just testing to_string(double d) - and it seem to take care of getting back to the string value, for my test cases at least.
Coordinator
Sep 24, 2014 at 9:36 PM
Hi Zack,

We unfortunately do not store the string form of the decimals we parse. If the exact form of the decimal is needed, it would be better (as you've implied) to have it sent as a string.

While it's not a particularly robust solution, you could take the string form of the JSON, do a minor parsing pass to transform all doubles into strings, and then pass that to the JSON parser. In the most informal case, you could look at C++11 regular expressions to do this.

Alternatively, you could try plugging in a 3rd-party JSON parser such as rapidjson (I don't know if RapidJSON specifically has this capability, but I assume one will exist). Casablanca is quite modular, so replacing an individual component should still work quite well.

Sincerely,
roschuma