-
Notifications
You must be signed in to change notification settings - Fork 33
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
Change include of json.h in json-builder.h. Change %g to %f because Python.json doesn't like scientific notation #6
base: master
Are you sure you want to change the base?
Conversation
Changed include to use quotes instead of angle brackets. This allows the include to be found in the local include path.
The Python json module does not like scientific notation when loading a string using the json.loads() method.
I just added a commit to this pull request. Changed %g to %f when using scanf to convert double into a string. %g creates doubles in scientific notation for large values and Python.json.loads(my_json) does not play nicely with scientific notation. |
It does but Python.json isn't parsing it correctly. What shall we do? Change to %f so the Python binding works or maybe there's a way to tell the Python.json.loads function to take scientific notation that I don't know about yet. What do you think?
|
Could make it an option? |
It's changed in my version to %f so the JSON coming from an embedded C Thanks, On Thu, Oct 9, 2014 at 2:16 AM, James McLaughlin [email protected]
|
Probably by adding an option to json-builder.h#L113-L145 #define json_serialize_opt_no_scientific_notation (1 << 6) And then changing the implementation to check for that option. |
json-builder.h Added the json_serialize_opt_no_scientific_notation option. This changes the serialization code to use %f instead of %g when it's set.
I pushed a change to implement the json_serialize_opt_no_scientific_notation. I'm running with it in my code and in my CUnit tests now. It's working good so far. |
total += snprintf (NULL, 0, "%g", value->u.dbl); | ||
} | ||
if (value->u.dbl - floor (value->u.dbl) < 0.001) | ||
total += 2; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these lines are 1 space too shallow - James uses 3-space indents.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't change the 2 space indent code. That was already there.
Sent from my iPhone
On Oct 9, 2014, at 3:05 PM, LB-- [email protected] wrote:
In json-builder.c:
@@ -663,11 +670,16 @@ size_t json_measure_ex (json_value * value, json_serialize_opts opts)
break;case json_double:
- total += snprintf (NULL, 0, "%g", value->u.dbl);
if (value->u.dbl - floor (value->u.dbl) < 0.001)
total += 2;
if (no_scientific_notation)
{
total += snprintf (NULL, 0, "%f", value->u.dbl);
}
else
{
total += snprintf (NULL, 0, "%g", value->u.dbl);
}
if (value->u.dbl - floor (value->u.dbl) < 0.001)
I think these lines are 1 space too shallow - James uses 3-space indents.total += 2;
—
Reply to this email directly or view it on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Compare the original (deleted) lines 669 and 670 to your new 681 and 682
Hmm. I'm not seeing it. Please elaborate. I'm brand new to this codebase and can be easily missing something. if (value->u.dbl - floor (value->u.dbl) < 0.001)
total += 2; New if (value->u.dbl - floor (value->u.dbl) < 0.001)
total += 2; |
I think this would be more concise as a ternary if on the format anyway? ---- Chad Beaulac wrote ----
|
I don't know how strict @udp is on formatting, I just know he prefers 3 spaces per indent. |
Ternary operator sounds good. Sent from my iPhone
|
Ok. It seems like there's definitely something wrong. Some code is returning shorter values for json_measure_ex than the string returned by json_serialize_ex. I'm writing unit tests to address it now. |
My Python unittest is working with my scientific notation. So, I believe the issue I was originally having is due to json_measure and json_serialize returning different length strings. I'll figure it out and post resolution. I'll probably back out the |
In
|
It was added in 4885ca3. It's to allow room for serialize adding |
Oh. Ok. That makes total sense. I'll write some CUnit tests that target On Sat, Oct 11, 2014 at 2:32 AM, James McLaughlin [email protected]
|
I think it would take less effort to just try both http://en.cppreference.com/w/cpp/io/c/fprintf
I don't know what it means by "alternative representation". |
How about this?
|
Changed include to use quotes instead of angle brackets.
This allows the include to be found in the local include path.