mirror of
https://github.com/json-c/json-c.git
synced 2026-03-29 18:09:07 +08:00
Add a json_set_serializer() function to allow the string output of a json_object to be customized.
This commit is contained in:
@@ -42,6 +42,10 @@ TESTS+= test_printbuf.test
|
||||
check_PROGRAMS+=test_printbuf
|
||||
test_printbuf_LDADD = $(LIBJSON_LA)
|
||||
|
||||
TESTS+= test_set_serializer.test
|
||||
check_PROGRAMS += test_set_serializer
|
||||
test_set_serializer_LDADD = $(LIBJSON_LA)
|
||||
|
||||
EXTRA_DIST=
|
||||
EXTRA_DIST += $(TESTS)
|
||||
|
||||
|
||||
71
tests/test_set_serializer.c
Normal file
71
tests/test_set_serializer.c
Normal file
@@ -0,0 +1,71 @@
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "json.h"
|
||||
#include "printbuf.h"
|
||||
|
||||
struct myinfo {
|
||||
int value;
|
||||
};
|
||||
|
||||
static int freeit_was_called = 0;
|
||||
static void freeit(json_object *jso, void *userdata)
|
||||
{
|
||||
struct myinfo *info = userdata;
|
||||
printf("freeit, value=%d\n", info->value);
|
||||
// Don't actually free anything here, the userdata is stack allocated.
|
||||
freeit_was_called = 1;
|
||||
}
|
||||
static int custom_serializer(struct json_object *o,
|
||||
struct printbuf *pb,
|
||||
int level,
|
||||
int flags)
|
||||
{
|
||||
sprintbuf(pb, "Custom Output");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
json_object *my_object;
|
||||
|
||||
MC_SET_DEBUG(1);
|
||||
|
||||
printf("Test setting, then resetting a custom serializer:\n");
|
||||
my_object = json_object_new_object();
|
||||
json_object_object_add(my_object, "abc", json_object_new_int(12));
|
||||
json_object_object_add(my_object, "foo", json_object_new_string("bar"));
|
||||
|
||||
printf("my_object.to_string(standard)=%s\n", json_object_to_json_string(my_object));
|
||||
|
||||
struct myinfo userdata = { .value = 123 };
|
||||
json_object_set_serializer(my_object, custom_serializer, &userdata, freeit);
|
||||
|
||||
printf("my_object.to_string(custom serializer)=%s\n", json_object_to_json_string(my_object));
|
||||
|
||||
printf("Next line of output should be from the custom freeit function:\n");
|
||||
freeit_was_called = 0;
|
||||
json_object_set_serializer(my_object, NULL, NULL, NULL);
|
||||
assert(freeit_was_called);
|
||||
|
||||
printf("my_object.to_string(standard)=%s\n", json_object_to_json_string(my_object));
|
||||
|
||||
json_object_put(my_object);
|
||||
|
||||
// ============================================
|
||||
|
||||
my_object = json_object_new_object();
|
||||
printf("Check that the custom serializer isn't free'd until the last json_object_put:\n");
|
||||
json_object_set_serializer(my_object, custom_serializer, &userdata, freeit);
|
||||
json_object_get(my_object);
|
||||
json_object_put(my_object);
|
||||
printf("my_object.to_string(custom serializer)=%s\n", json_object_to_json_string(my_object));
|
||||
printf("Next line of output should be from the custom freeit function:\n");
|
||||
|
||||
freeit_was_called = 0;
|
||||
json_object_put(my_object);
|
||||
assert(freeit_was_called);
|
||||
|
||||
return 0;
|
||||
}
|
||||
9
tests/test_set_serializer.expected
Normal file
9
tests/test_set_serializer.expected
Normal file
@@ -0,0 +1,9 @@
|
||||
my_object.to_string(standard)={ "abc": 12, "foo": "bar" }
|
||||
my_object.to_string(custom serializer)=Custom Output
|
||||
Next line of output should be from the custom freeit function:
|
||||
freeit, value=123
|
||||
my_object.to_string(standard)={ "abc": 12, "foo": "bar" }
|
||||
Check that the custom serializer isn't free'd until the last json_object_put:
|
||||
my_object.to_string(custom serializer)=Custom Output
|
||||
Next line of output should be from the custom freeit function:
|
||||
freeit, value=123
|
||||
12
tests/test_set_serializer.test
Executable file
12
tests/test_set_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_set_serializer
|
||||
exit $?
|
||||
Reference in New Issue
Block a user