]> granicus.if.org Git - json-c/commitdiff
Make default double serializer ignore userdata again
authorMatthias Schiffer <mschiffer@universe-factory.net>
Sun, 29 May 2016 02:54:38 +0000 (04:54 +0200)
committerMatthias Schiffer <mschiffer@universe-factory.net>
Sun, 29 May 2016 09:24:55 +0000 (11:24 +0200)
The user might want to use the userdata for something different, so the
serializer should ignore it by default.

Explicitly setting the serializer to json_object_double_to_json_string will
still make it interpret the userdata as a format string.

.gitignore
json_object.c
tests/Makefile.am
tests/test_double_serializer.c [new file with mode: 0644]
tests/test_double_serializer.expected [new file with mode: 0644]
tests/test_double_serializer.test [new file with mode: 0755]

index 57aa6d1ebe508d44a8fafc8e97914d0b7d7d44cd..6e0bee23a3316469b66ebf713a7d5a7fe87066fd 100644 (file)
@@ -41,6 +41,7 @@
 /tests/test_parse
 /tests/test_cast
 /tests/test_charcase
+/tests/test_double_serializer
 /tests/test_locale
 /tests/test_null
 /tests/test_printbuf
index f0d1251de605223daac0f053b134264aad97783b..f9085403cb662e311ffdcccd1bb812a71d09a2e2 100644 (file)
@@ -54,6 +54,7 @@ static struct json_object* json_object_new(enum json_type o_type);
 
 static json_object_to_json_string_fn json_object_object_to_json_string;
 static json_object_to_json_string_fn json_object_boolean_to_json_string;
+static json_object_to_json_string_fn json_object_double_to_json_string_default;
 static json_object_to_json_string_fn json_object_int_to_json_string;
 static json_object_to_json_string_fn json_object_string_to_json_string;
 static json_object_to_json_string_fn json_object_array_to_json_string;
@@ -261,7 +262,7 @@ void json_object_set_serializer(json_object *jso,
                        jso->_to_json_string = &json_object_boolean_to_json_string;
                        break;
                case json_type_double:
-                       jso->_to_json_string = &json_object_double_to_json_string;
+                       jso->_to_json_string = &json_object_double_to_json_string_default;
                        break;
                case json_type_int:
                        jso->_to_json_string = &json_object_int_to_json_string;
@@ -643,10 +644,11 @@ int64_t json_object_get_int64(const struct json_object *jso)
 
 /* json_object_double */
 
-int json_object_double_to_json_string(struct json_object* jso,
-                                     struct printbuf *pb,
-                                     int level,
-                                     int flags)
+static int json_object_double_to_json_string_format(struct json_object* jso,
+                                                   struct printbuf *pb,
+                                                   int level,
+                                                   int flags,
+                                                   const char *format)
 {
   char buf[128], *p, *q;
   int size;
@@ -663,7 +665,7 @@ int json_object_double_to_json_string(struct json_object* jso,
       size = snprintf(buf, sizeof(buf), "-Infinity");
   else
     size = snprintf(buf, sizeof(buf),
-        jso->_userdata ? (const char*) jso->_userdata : "%.17g", jso->o.c_double);
+        format ? format : "%.17g", jso->o.c_double);
 
   p = strchr(buf, ',');
   if (p) {
@@ -685,12 +687,30 @@ int json_object_double_to_json_string(struct json_object* jso,
   return size;
 }
 
+static int json_object_double_to_json_string_default(struct json_object* jso,
+                                                    struct printbuf *pb,
+                                                    int level,
+                                                    int flags)
+{
+       return json_object_double_to_json_string_format(jso, pb, level, flags,
+                                                       NULL);
+}
+
+int json_object_double_to_json_string(struct json_object* jso,
+                                     struct printbuf *pb,
+                                     int level,
+                                     int flags)
+{
+       return json_object_double_to_json_string_format(jso, pb, level, flags,
+                                                       jso->_userdata);
+}
+
 struct json_object* json_object_new_double(double d)
 {
        struct json_object *jso = json_object_new(json_type_double);
        if (!jso)
                return NULL;
-       jso->_to_json_string = &json_object_double_to_json_string;
+       jso->_to_json_string = &json_object_double_to_json_string_default;
        jso->o.c_double = d;
        return jso;
 }
index 79b196b6a34c137bdd548c21b39d57ee496530ce..7fdb2d9236144bb88b5f9634a8b267f7c0287c45 100644 (file)
@@ -11,6 +11,7 @@ TESTS+= testReplaceExisting.test
 TESTS+= test_parse_int64.test
 TESTS+= test_null.test
 TESTS+= test_cast.test
+TESTS+= test_double_serializer.test
 TESTS+= test_parse.test
 TESTS+= test_locale.test
 TESTS+= test_charcase.test
@@ -22,7 +23,7 @@ check_PROGRAMS=
 check_PROGRAMS += $(TESTS:.test=)
 
 # Note: handled by test1.test
-check_PROGRAMS += test1Formatted 
+check_PROGRAMS += test1Formatted
 test1Formatted_SOURCES = test1.c parse_flags.c
 test1Formatted_CPPFLAGS = -DTEST_FORMATTED
 
diff --git a/tests/test_double_serializer.c b/tests/test_double_serializer.c
new file mode 100644 (file)
index 0000000..4ef754d
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+* Tests if the format string for double serialization is handled correctly
+*/
+
+#include <stdio.h>
+#include "config.h"
+
+#include "json_object.h"
+#include "json_object_private.h"
+
+int main()
+{
+       struct json_object *obj = json_object_new_double(0.5);
+
+       printf("Test default serializer:\n");
+       printf("obj.to_string(standard)=%s\n", json_object_to_json_string(obj));
+
+       printf("Test default serializer with custom userdata:\n");
+       obj->_userdata = "test";
+       printf("obj.to_string(userdata)=%s\n", json_object_to_json_string(obj));
+
+       printf("Test explicit serializer with custom userdata:\n");
+       json_object_set_serializer(obj, json_object_double_to_json_string, "test", NULL);
+       printf("obj.to_string(custom)=%s\n", json_object_to_json_string(obj));
+
+       printf("Test reset serializer:\n");
+       json_object_set_serializer(obj, NULL, NULL, NULL);
+       printf("obj.to_string(reset)=%s\n", json_object_to_json_string(obj));
+
+       json_object_put(obj);
+}
diff --git a/tests/test_double_serializer.expected b/tests/test_double_serializer.expected
new file mode 100644 (file)
index 0000000..da645d7
--- /dev/null
@@ -0,0 +1,8 @@
+Test default serializer:
+obj.to_string(standard)=0.5
+Test default serializer with custom userdata:
+obj.to_string(userdata)=0.5
+Test explicit serializer with custom userdata:
+obj.to_string(custom)=test
+Test reset serializer:
+obj.to_string(reset)=0.5
diff --git a/tests/test_double_serializer.test b/tests/test_double_serializer.test
new file mode 100755 (executable)
index 0000000..d4abac4
--- /dev/null
@@ -0,0 +1,12 @@
+#!/bin/sh
+
+# Common definitions
+if test -z "$srcdir"; then
+    srcdir="${0%/*}"
+    test "$srcdir" = "$0" && srcdir=.
+    test -z "$srcdir" && srcdir=.
+fi
+. "$srcdir/test-defs.sh"
+
+run_output_test test_double_serializer
+exit $?