mirror of
https://github.com/json-c/json-c.git
synced 2026-03-29 01:49:06 +08:00
Make default double serializer ignore userdata again
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.
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
31
tests/test_double_serializer.c
Normal file
31
tests/test_double_serializer.c
Normal file
@@ -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);
|
||||
}
|
||||
8
tests/test_double_serializer.expected
Normal file
8
tests/test_double_serializer.expected
Normal file
@@ -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
|
||||
12
tests/test_double_serializer.test
Executable file
12
tests/test_double_serializer.test
Executable file
@@ -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 $?
|
||||
Reference in New Issue
Block a user