|
Packit |
ea8578 |
#include <assert.h>
|
|
Packit |
ea8578 |
#include <stdio.h>
|
|
Packit |
ea8578 |
#include <string.h>
|
|
Packit |
ea8578 |
|
|
Packit |
ea8578 |
#include "json.h"
|
|
Packit |
ea8578 |
#include "printbuf.h"
|
|
Packit |
ea8578 |
|
|
Packit |
ea8578 |
struct myinfo {
|
|
Packit |
ea8578 |
int value;
|
|
Packit |
ea8578 |
};
|
|
Packit |
ea8578 |
|
|
Packit |
ea8578 |
static int freeit_was_called = 0;
|
|
Packit |
ea8578 |
static void freeit(json_object *jso, void *userdata)
|
|
Packit |
ea8578 |
{
|
|
Packit |
ea8578 |
struct myinfo *info = userdata;
|
|
Packit |
ea8578 |
printf("freeit, value=%d\n", info->value);
|
|
Packit |
ea8578 |
// Don't actually free anything here, the userdata is stack allocated.
|
|
Packit |
ea8578 |
freeit_was_called = 1;
|
|
Packit |
ea8578 |
}
|
|
Packit |
ea8578 |
static int custom_serializer(struct json_object *o,
|
|
Packit |
ea8578 |
struct printbuf *pb,
|
|
Packit |
ea8578 |
int level,
|
|
Packit |
ea8578 |
int flags)
|
|
Packit |
ea8578 |
{
|
|
Packit |
ea8578 |
sprintbuf(pb, "Custom Output");
|
|
Packit |
ea8578 |
return 0;
|
|
Packit |
ea8578 |
}
|
|
Packit |
ea8578 |
|
|
Packit |
ea8578 |
int main(int argc, char **argv)
|
|
Packit |
ea8578 |
{
|
|
Packit |
ea8578 |
json_object *my_object;
|
|
Packit |
ea8578 |
|
|
Packit |
ea8578 |
MC_SET_DEBUG(1);
|
|
Packit |
ea8578 |
|
|
Packit |
ea8578 |
printf("Test setting, then resetting a custom serializer:\n");
|
|
Packit |
ea8578 |
my_object = json_object_new_object();
|
|
Packit |
ea8578 |
json_object_object_add(my_object, "abc", json_object_new_int(12));
|
|
Packit |
ea8578 |
json_object_object_add(my_object, "foo", json_object_new_string("bar"));
|
|
Packit |
ea8578 |
|
|
Packit |
ea8578 |
printf("my_object.to_string(standard)=%s\n", json_object_to_json_string(my_object));
|
|
Packit |
ea8578 |
|
|
Packit |
ea8578 |
struct myinfo userdata = { .value = 123 };
|
|
Packit |
ea8578 |
json_object_set_serializer(my_object, custom_serializer, &userdata, freeit);
|
|
Packit |
ea8578 |
|
|
Packit |
ea8578 |
printf("my_object.to_string(custom serializer)=%s\n", json_object_to_json_string(my_object));
|
|
Packit |
ea8578 |
|
|
Packit |
ea8578 |
printf("Next line of output should be from the custom freeit function:\n");
|
|
Packit |
ea8578 |
freeit_was_called = 0;
|
|
Packit |
ea8578 |
json_object_set_serializer(my_object, NULL, NULL, NULL);
|
|
Packit |
ea8578 |
assert(freeit_was_called);
|
|
Packit |
ea8578 |
|
|
Packit |
ea8578 |
printf("my_object.to_string(standard)=%s\n", json_object_to_json_string(my_object));
|
|
Packit |
ea8578 |
|
|
Packit |
ea8578 |
json_object_put(my_object);
|
|
Packit |
ea8578 |
|
|
Packit |
ea8578 |
// ============================================
|
|
Packit |
ea8578 |
|
|
Packit |
ea8578 |
my_object = json_object_new_object();
|
|
Packit |
ea8578 |
printf("Check that the custom serializer isn't free'd until the last json_object_put:\n");
|
|
Packit |
ea8578 |
json_object_set_serializer(my_object, custom_serializer, &userdata, freeit);
|
|
Packit |
ea8578 |
json_object_get(my_object);
|
|
Packit |
ea8578 |
json_object_put(my_object);
|
|
Packit |
ea8578 |
printf("my_object.to_string(custom serializer)=%s\n", json_object_to_json_string(my_object));
|
|
Packit |
ea8578 |
printf("Next line of output should be from the custom freeit function:\n");
|
|
Packit |
ea8578 |
|
|
Packit |
ea8578 |
freeit_was_called = 0;
|
|
Packit |
ea8578 |
json_object_put(my_object);
|
|
Packit |
ea8578 |
assert(freeit_was_called);
|
|
Packit |
ea8578 |
|
|
Packit |
ea8578 |
return 0;
|
|
Packit |
ea8578 |
}
|