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

Add option to have more control over the precision with which reals are placed in JSON with dump calls #676

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
7 changes: 5 additions & 2 deletions src/dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,13 @@ static int do_dump(const json_t *json, size_t flags, int depth, hashtable_t *par
case JSON_REAL: {
char buffer[MAX_REAL_STR_LENGTH];
int size;
int precision = json_real_precision(json);
double value = json_real_value(json);

size = jsonp_dtostr(buffer, MAX_REAL_STR_LENGTH, value,
FLAGS_TO_PRECISION(flags));
if (precision == DEFAULT_PRECISION_SYSTEM)
precision = FLAGS_TO_PRECISION(flags);

size = jsonp_dtostr(buffer, MAX_REAL_STR_LENGTH, value, precision);
if (size < 0)
return -1;

Expand Down
6 changes: 6 additions & 0 deletions src/jansson.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ json_t *json_stringn(const char *value, size_t len);
json_t *json_string_nocheck(const char *value);
json_t *json_stringn_nocheck(const char *value, size_t len);
json_t *json_integer(json_int_t value);
json_t *json_real_with_precision(double value, int precision);
json_t *json_real(double value);
json_t *json_true(void);
json_t *json_false(void);
Expand Down Expand Up @@ -308,18 +309,23 @@ static JSON_INLINE int json_array_insert(json_t *array, size_t ind, json_t *valu
return json_array_insert_new(array, ind, json_incref(value));
}

#define DEFAULT_PRECISION_SYSTEM -1

const char *json_string_value(const json_t *string);
size_t json_string_length(const json_t *string);
json_int_t json_integer_value(const json_t *integer);
double json_real_value(const json_t *real);
int json_real_precision(const json_t *real);
double json_number_value(const json_t *json);

int json_string_set(json_t *string, const char *value);
int json_string_setn(json_t *string, const char *value, size_t len);
int json_string_set_nocheck(json_t *string, const char *value);
int json_string_setn_nocheck(json_t *string, const char *value, size_t len);
int json_integer_set(json_t *integer, json_int_t value);
int json_real_set_with_precision(json_t *real, double value, int precision);
int json_real_set(json_t *real, double value);
int json_real_set_precision(json_t *json, int precision);

/* pack, unpack */

Expand Down
3 changes: 2 additions & 1 deletion src/jansson_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ typedef struct {
typedef struct {
json_t json;
double value;
int precision;
} json_real_t;

typedef struct {
Expand All @@ -79,7 +80,7 @@ void jsonp_error_vset(json_error_t *error, int line, int column, size_t position

/* Locale independent string<->double conversions */
int jsonp_strtod(strbuffer_t *strbuffer, double *out);
int jsonp_dtostr(char *buffer, size_t size, double value, int prec);
int jsonp_dtostr(char *buffer, size_t size, double value, int precision);

/* Wrappers for custom memory functions */
void *jsonp_malloc(size_t size) JANSSON_ATTRS((warn_unused_result));
Expand Down
30 changes: 27 additions & 3 deletions src/value.c
Original file line number Diff line number Diff line change
Expand Up @@ -927,8 +927,7 @@ static json_t *json_integer_copy(const json_t *integer) {
}

/*** real ***/

json_t *json_real(double value) {
json_t *json_real_with_precision(double value, int precision) {
json_real_t *real;

if (isnan(value) || isinf(value))
Expand All @@ -940,25 +939,50 @@ json_t *json_real(double value) {
json_init(&real->json, JSON_REAL);

real->value = value;
real->precision = precision;
return &real->json;
}

json_t *json_real(double value) {
return json_real_with_precision(value, DEFAULT_PRECISION_SYSTEM);
}

double json_real_value(const json_t *json) {
if (!json_is_real(json))
return 0;

return json_to_real(json)->value;
}

int json_real_set(json_t *json, double value) {
int json_real_precision(const json_t *json) {
if (!json_is_real(json))
return -1;

return json_to_real(json)->precision;
}

int json_real_set_with_precision(json_t *json, double value, int precision) {
if (!json_is_real(json) || isnan(value) || isinf(value))
return -1;

json_to_real(json)->value = value;
json_to_real(json)->precision = precision;

return 0;
}

int json_real_set(json_t *json, double value) {
return(json_real_set_with_precision(json, value, DEFAULT_PRECISION_SYSTEM));
}

int json_real_set_precision(json_t *json, int precision) {
if (!json_is_real(json))
return -1;

json_to_real(json)->precision = precision;
return(0);
}

static void json_delete_real(json_real_t *real) { jsonp_free(real); }

static int json_real_equal(const json_t *real1, const json_t *real2) {
Expand Down