Closed dougvj closed 2 years ago
Thank you for your contribution, nice executed.
Indeed, that is not valid in JSON (just for the sake of curiosity, let me say that it would be valid as a JavaScript object).
Let me ask you something. I see that you are adding a trailing 0, for example 1.e+20 => 1.0e+20, if I have undestood correctly.
Wouldn't be an option to remove the .
when it is not followed by a number?
After reading your code comment about the #
flag being the culprit. (https://en.wikipedia.org/wiki/Printf_format_string)
In https://github.com/jllodra/ncdump-json/blob/master/src/dumplib.c#L109 you could define also the variables "json_float_att_fmt", "json_float_attx_fmt", and "json_double_att_fmt", with the same format specification but removing the #
.
Then, in https://github.com/jllodra/ncdump-json/blob/master/src/ncdump.c#L447, you could write if (is_json)
then do the snprintf using the json-specific format, else
do what it was doing before.
What do you think about this approach?
I thought it might be desirable to maintain the float type rather than produce a JSON int type in the case that scientific notation is not produced. However, for our needs this doesn't matter and I would be happy to switch this to dropping the decimal instead if you prefer.
Yes this approach would be fine too.
If you are OK with it (and also if it solves your problem), I think the best approach would be the latter (dropping the #
for json). For sake of code simplicity, and would avoid manipulation of text
OK thanks for the feedback. I pushed this change and edited the title to reflect.
Excellent, thank you for your contribution
Recently we encountered a data set that produced output values on variable attribute floats with trailing decimals, for example:
This not valid JSON and broke parsing.
I was able to recreate the issue with the test data set in the repo, under tests:
Produces this in the output:
~This PR fixes the problem by detecting the case where a
.
is followed by a non digit and adds a single 0.~Now fixed by using the standard variable format specifier which does not have '#'