# How to Stop json-cpp from Injecting Decimal Points

This topic is 1027 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

##### Share on other sites

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.

##### Share on other sites

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.

Edited by shuma-gorath

##### Share on other sites

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.

##### Share on other sites
You can always convet to ASCII (hex or decimal) and store it as a string... :(

##### Share on other sites

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.

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.

##### Share on other sites

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

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.

##### Share on other sites

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.

1. 1
2. 2
3. 3
Rutin
19
4. 4
JoeJ
14
5. 5

• 14
• 9
• 23
• 9
• 32
• ### Forum Statistics

• Total Topics
632628
• Total Posts
3007515
• ### Who's Online (See full list)

There are no registered users currently online

×