Skip to content
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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nbproject
36 changes: 27 additions & 9 deletions json-builder.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,17 @@ const int f_spaces_around_brackets = (1 << 0);
const int f_spaces_after_commas = (1 << 1);
const int f_spaces_after_colons = (1 << 2);
const int f_tabs = (1 << 3);
const int f_no_scientific_notation = (1 << 4);

int get_serialize_flags (json_serialize_opts opts)
{
int flags = 0;

if (opts.opts & json_serialize_opt_no_scientific_notation)
flags |= f_no_scientific_notation;

if (opts.mode == json_serialize_mode_packed)
return 0;
return flags;

if (opts.mode == json_serialize_mode_multiline)
{
Expand All @@ -123,6 +127,7 @@ int get_serialize_flags (json_serialize_opts opts)
if (! (opts.opts & json_serialize_opt_no_space_after_colon))
flags |= f_spaces_after_colons;


return flags;
}

Expand Down Expand Up @@ -543,7 +548,7 @@ size_t json_measure_ex (json_value * value, json_serialize_opts opts)
size_t depth = 0;
size_t indents = 0;
int flags;
int bracket_size, comma_size, colon_size;
int bracket_size, comma_size, colon_size, no_scientific_notation;

flags = get_serialize_flags (opts);

Expand All @@ -552,7 +557,9 @@ size_t json_measure_ex (json_value * value, json_serialize_opts opts)
bracket_size = flags & f_spaces_around_brackets ? 2 : 1;
comma_size = flags & f_spaces_after_commas ? 2 : 1;
colon_size = flags & f_spaces_after_colons ? 2 : 1;
no_scientific_notation = flags & f_no_scientific_notation ? 1 : 0;


while (value)
{
json_int_t integer;
Expand Down Expand Up @@ -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)
total += 2;
Copy link
Member

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.

Copy link
Author

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)
    
  •          total += 2;
    
    I think these lines are 1 space too shallow - James uses 3-space indents.


Reply to this email directly or view it on GitHub.

Copy link
Member

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


break;

Expand Down Expand Up @@ -878,8 +890,14 @@ void json_serialize_ex (json_char * buf, json_value * value, json_serialize_opts
case json_double:

ptr = buf;

buf += sprintf (buf, "%g", value->u.dbl);
if (flags & f_no_scientific_notation)
{
buf += sprintf (buf, "%f", value->u.dbl);
}
else
{
buf += sprintf (buf, "%g", value->u.dbl);
}

if ((dot = strchr (ptr, ',')))
{
Expand Down
3 changes: 2 additions & 1 deletion json-builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
/* Requires json.h from json-parser
* https://github.com/udp/json-parser
*/
#include <json.h>
#include "json.h"

#ifdef __cplusplus
extern "C"
Expand Down Expand Up @@ -121,6 +121,7 @@ json_value * json_null_new ();
#define json_serialize_opt_no_space_after_comma (1 << 3)
#define json_serialize_opt_no_space_after_colon (1 << 4)
#define json_serialize_opt_use_tabs (1 << 5)
#define json_serialize_opt_no_scientific_notation (1 << 6)

typedef struct json_serialize_opts
{
Expand Down