Uh oh!
There was an error while loading. Please reload this page.
Change include of json.h in json-builder.h. Change %g to %f because Python.json doesn't like scientific notation - #6
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.
cabeaulac
commented
Oct 8, 2014
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. |
LB--
commented
Oct 9, 2014
cabeaulac
commented
Oct 9, 2014
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?
|
jamesamcl
commented
Oct 9, 2014
Could make it an option? |
cabeaulac
commented
Oct 9, 2014
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 notifications@github.com
|
LB--
commented
Oct 9, 2014
Probably by adding an option to json-builder.h#L113-L145 #definejson_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.
cabeaulac
commented
Oct 9, 2014
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. |
There was a problem hiding this comment.
I think these lines are 1 space too shallow - James uses 3-space indents.
There was a problem hiding this comment.
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-- notifications@github.com 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.
Compare the original (deleted) lines 669 and 670 to your new 681 and 682
cabeaulac
commented
Oct 10, 2014
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; |
jamesamcl
commented
Oct 10, 2014
I think this would be more concise as a ternary if on the format anyway? ---- Chad Beaulac wrote ----
|
LB--
commented
Oct 10, 2014
I don't know how strict @udp is on formatting, I just know he prefers 3 spaces per indent. |
cabeaulac
commented
Oct 10, 2014
Ternary operator sounds good. Sent from my iPhone
|
cabeaulac
commented
Oct 10, 2014
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. |
cabeaulac
commented
Oct 10, 2014
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 |
cabeaulac
commented
Oct 10, 2014
In |
jamesamcl
commented
Oct 11, 2014
It was added in 4885ca3. It's to allow room for serialize adding |
cabeaulac
commented
Oct 11, 2014
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 notifications@github.com
|
LB--
commented
Oct 11, 2014
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". |
cabeaulac
commented
Oct 13, 2014
How about this?
|


Changed include to use quotes instead of angle brackets.
This allows the include to be found in the local include path.