How to Stop json-cpp from Injecting Decimal Points

Started by
6 comments, last by shuma-gorath 8 years, 4 months ago

Suppose I read in a JSON file and write out to the same file without changing the JSON at all. Now, when I do this, json-cpp enforces E+ notation on big numbers, and I have found no way to turn it off. In my case, I'm working with timestamps, so it's essential to preserve the numbers in their original form.

Is there any way to turn off this E+ stuff, or is there another C++ JSON library that can do this?

Thanks.

Advertisement

I think you perhaps have two options here.

The first thing you can try is to change the the type to an integer, I would only expect doubles or floats to be treated in this way. (This should remove the e+ notation.) I.e. is it a timestamps in milliseconds since the Epoch?

The second thing would be to look at the API interface for writer a bit closer and possibly the writer.cpp source. I haven't had any direct experience with json-cpp, but out of curiosity looked at the source real quick, seems like it outputs to an ostream object. Not sure if you can supply the ostream or not, but it suggests that you may be able to set the output formatting on the ostream object.

For example, this code:


double testNum = 12323541245634262.0;
std::cout << "Number: " << testNum << std::endl;

will print:

Number: 1.23235e+016

However, this code:


double testNum = 12323541245634262.0;

std::cout << std::fixed << std::showpoint;
std::cout << std::setprecision(4);
std::cout << "Number: " << testNum << std::endl;

will print:

Number: 12323541245634262.0000

If you have access to a writer function or constructor that accepts an ostream object, you can create your file like this:


std::ofstream ofs ("test.json", std::ofstream::out);

ofs << std::fixed << std::showpoint;
ofs << std::setprecision(4);

Hope it helps.

Thanks for the reply.

So, yes, the number is integer timestamp since the Epoch (e.g. 1422982952874000). Looking over things today, it appears that only JsonView was showing the E+ notation. However, json-cpp is still appending decimal points to my timestamps.

The built-in writers for Json-cpp can optionally take stream, though I see this behavior with and without, and following your advice about the precision.

When I get a chance, I'm going to try a few things and update this thread.

UPDATE: I have updated the thread title to reflect the fact the the problem has to do with the trailing decimal points.

The number looks too big to fit in a 32 bit integer, perhaps it gets converted to float because you have more bits there to represent the number?

You may want to check what limits are applied to json.

You can always convet to ASCII (hex or decimal) and store it as a string... :(

The number looks too big to fit in a 32 bit integer, perhaps it gets converted to float because you have more bits there to represent the number?

You may want to check what limits are applied to json.

Thanks for the reply.

Thing is, Firefox bookmarks, when backed up to JSON, use the same timestamp format, and they don't use quotation marks. When I pass in bookmarks to json-cpp, the timestamps come out with a decimal point appended.

You can always convet to ASCII (hex or decimal) and store it as a string... sad.png

Thanks for the reply.

I'm not rightly sure if the consuming program can handle quoted timestamps, but I can find out.

Would anyone happen to know a way to walk the DOM, visiting each value along the way? I have some tree-traversal code lying around that I could re-purpose for this, but I'd much rather use the library's built-in method. I don't remember seeing anything in the documentation about this.

Well, in the end, I have decided not to use JSON for this project. I figure the alternatives will be simpler.

Thanks again to all who replied.

This topic is closed to new replies.

Advertisement