Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jamesbond3142/19a857d2cc31e2fd550f8fd129d5802f to your computer and use it in GitHub Desktop.
Save jamesbond3142/19a857d2cc31e2fd550f8fd129d5802f to your computer and use it in GitHub Desktop.
parson: support per-node customisation of serialisation of number
From 43c3b2c9d5388a8da1b433d42dd04b371921c9e2 Mon Sep 17 00:00:00 2001
From: james <james@localhost>
Date: Tue, 21 Feb 2017 04:18:37 +1000
Subject: [PATCH] parson JSON library - add format serialisation of numbers
---
inc/parson.h | 2 ++
src/parson.c | 16 ++++++++++++++--
2 files changed, 16 insertions(+), 2 deletions(-)
diff --git a/inc/parson.h b/inc/parson.h
index e4cd21e..ca74cd5 100644
--- a/inc/parson.h
+++ b/inc/parson.h
@@ -143,6 +143,7 @@ int json_object_dothas_value_of_type(const JSON_Object *object, const char *name
JSON_Status json_object_set_value(JSON_Object *object, const char *name, JSON_Value *value);
JSON_Status json_object_set_string(JSON_Object *object, const char *name, const char *string);
JSON_Status json_object_set_number(JSON_Object *object, const char *name, double number);
+JSON_Status json_object_set_number2(JSON_Object *object, const char *name, double number, const char *FloatFormat);
JSON_Status json_object_set_boolean(JSON_Object *object, const char *name, int boolean);
JSON_Status json_object_set_null(JSON_Object *object, const char *name);
@@ -206,6 +207,7 @@ JSON_Value * json_value_init_object (void);
JSON_Value * json_value_init_array (void);
JSON_Value * json_value_init_string (const char *string); /* copies passed string */
JSON_Value * json_value_init_number (double number);
+JSON_Value * json_value_init_number2(double number, const char *FloatFormat);
JSON_Value * json_value_init_boolean(int boolean);
JSON_Value * json_value_init_null (void);
JSON_Value * json_value_deep_copy (const JSON_Value *value);
diff --git a/src/parson.c b/src/parson.c
index b914ae8..91990e6 100644
--- a/src/parson.c
+++ b/src/parson.c
@@ -68,6 +68,7 @@ struct json_value_t {
JSON_Value *parent;
JSON_Value_Type type;
JSON_Value_Value value;
+ const char *number_format;
};
struct json_object_t {
@@ -890,7 +891,9 @@ static int json_serialize_to_buffer_r(const JSON_Value *value, char *buf, int le
} else if (num == ((double)(unsigned int)num)) {
written = sprintf(num_buf, "%u", (unsigned int)num);
} else {
- written = sprintf(num_buf, DOUBLE_SERIALIZATION_FORMAT, num);
+ const char *fmt = value->number_format;
+ if (!fmt) fmt = DOUBLE_SERIALIZATION_FORMAT;
+ written = sprintf(num_buf, fmt, num);
}
if (written < 0) {
return -1;
@@ -1270,7 +1273,7 @@ JSON_Value * json_value_init_string(const char *string) {
return value;
}
-JSON_Value * json_value_init_number(double number) {
+JSON_Value * json_value_init_number2(double number, const char *FloatFormat) {
JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
if (!new_value) {
return NULL;
@@ -1278,9 +1281,14 @@ JSON_Value * json_value_init_number(double number) {
new_value->parent = NULL;
new_value->type = JSONNumber;
new_value->value.number = number;
+ new_value->number_format = FloatFormat;
return new_value;
}
+JSON_Value * json_value_init_number(double number) {
+ return json_value_init_number2(number, NULL);
+}
+
JSON_Value * json_value_init_boolean(int boolean) {
JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
if (!new_value) {
@@ -1678,6 +1686,10 @@ JSON_Status json_object_set_number(JSON_Object *object, const char *name, double
return json_object_set_value(object, name, json_value_init_number(number));
}
+JSON_Status json_object_set_number2(JSON_Object *object, const char *name, double number, const char *FloatFormat) {
+ return json_object_set_value(object, name, json_value_init_number2(number, FloatFormat));
+}
+
JSON_Status json_object_set_boolean(JSON_Object *object, const char *name, int boolean) {
return json_object_set_value(object, name, json_value_init_boolean(boolean));
}
--
2.7.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment