aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPetri Lehtinen <petri@digip.org>2014-04-30 12:46:33 +0300
committerPetri Lehtinen <petri@digip.org>2014-04-30 12:46:34 +0300
commit17b5fdd94b1e22ec294f6cb746599aafc021a54b (patch)
tree4eba46be0023bc31f7da9fe3d73f397a2ad5c2d7 /src
parent5b88cc5ded24fd9802358a03794054485fb523f8 (diff)
downloadjansson-17b5fdd94b1e22ec294f6cb746599aafc021a54b.zip
jansson-17b5fdd94b1e22ec294f6cb746599aafc021a54b.tar.gz
jansson-17b5fdd94b1e22ec294f6cb746599aafc021a54b.tar.bz2
Add JSON_REAL_PRECISION
Fixes #178.
Diffstat (limited to 'src')
-rw-r--r--src/dump.c10
-rw-r--r--src/jansson.h15
-rw-r--r--src/jansson_private.h2
-rw-r--r--src/strconv.c7
4 files changed, 21 insertions, 13 deletions
diff --git a/src/dump.c b/src/dump.c
index 7eddd5a..79cb2f2 100644
--- a/src/dump.c
+++ b/src/dump.c
@@ -22,6 +22,9 @@
#define MAX_INTEGER_STR_LENGTH 100
#define MAX_REAL_STR_LENGTH 100
+#define FLAGS_TO_INDENT(f) ((f) & 0x1F)
+#define FLAGS_TO_PRECISION(f) (((f) >> 11) & 0x1F)
+
struct object_key {
size_t serial;
const char *key;
@@ -45,9 +48,9 @@ static const char whitespace[] = " ";
static int dump_indent(size_t flags, int depth, int space, json_dump_callback_t dump, void *data)
{
- if(JSON_INDENT(flags) > 0)
+ if(FLAGS_TO_INDENT(flags) > 0)
{
- int i, ws_count = JSON_INDENT(flags);
+ int i, ws_count = FLAGS_TO_INDENT(flags);
if(dump("\n", 1, data))
return -1;
@@ -208,7 +211,8 @@ static int do_dump(const json_t *json, size_t flags, int depth,
int size;
double value = json_real_value(json);
- size = jsonp_dtostr(buffer, MAX_REAL_STR_LENGTH, value);
+ size = jsonp_dtostr(buffer, MAX_REAL_STR_LENGTH, value,
+ FLAGS_TO_PRECISION(flags));
if(size < 0)
return -1;
diff --git a/src/jansson.h b/src/jansson.h
index 85215f4..bcb2087 100644
--- a/src/jansson.h
+++ b/src/jansson.h
@@ -259,13 +259,14 @@ json_t *json_load_callback(json_load_callback_t callback, void *data, size_t fla
/* encoding */
-#define JSON_INDENT(n) (n & 0x1F)
-#define JSON_COMPACT 0x20
-#define JSON_ENSURE_ASCII 0x40
-#define JSON_SORT_KEYS 0x80
-#define JSON_PRESERVE_ORDER 0x100
-#define JSON_ENCODE_ANY 0x200
-#define JSON_ESCAPE_SLASH 0x400
+#define JSON_INDENT(n) ((n) & 0x1F)
+#define JSON_COMPACT 0x20
+#define JSON_ENSURE_ASCII 0x40
+#define JSON_SORT_KEYS 0x80
+#define JSON_PRESERVE_ORDER 0x100
+#define JSON_ENCODE_ANY 0x200
+#define JSON_ESCAPE_SLASH 0x400
+#define JSON_REAL_PRECISION(n) (((n) & 0x1F) << 11)
typedef int (*json_dump_callback_t)(const char *buffer, size_t size, void *data);
diff --git a/src/jansson_private.h b/src/jansson_private.h
index c6f437c..e100726 100644
--- a/src/jansson_private.h
+++ b/src/jansson_private.h
@@ -81,7 +81,7 @@ void jsonp_error_vset(json_error_t *error, int line, int column,
/* Locale independent string<->double conversions */
int jsonp_strtod(strbuffer_t *strbuffer, double *out);
-int jsonp_dtostr(char *buffer, size_t size, double value);
+int jsonp_dtostr(char *buffer, size_t size, double value, int prec);
/* Wrappers for custom memory functions */
void* jsonp_malloc(size_t size);
diff --git a/src/strconv.c b/src/strconv.c
index 3a70c6f..f6616dc 100644
--- a/src/strconv.c
+++ b/src/strconv.c
@@ -78,13 +78,16 @@ int jsonp_strtod(strbuffer_t *strbuffer, double *out)
return 0;
}
-int jsonp_dtostr(char *buffer, size_t size, double value)
+int jsonp_dtostr(char *buffer, size_t size, double value, int precision)
{
int ret;
char *start, *end;
size_t length;
- ret = snprintf(buffer, size, "%.17g", value);
+ if (precision == 0)
+ precision = 17;
+
+ ret = snprintf(buffer, size, "%.*g", precision, value);
if(ret < 0)
return -1;