static int sort_fn (const void *j1, const void *j2)
{
- json_object * const *jso1, * const *jso2;
- int i1, i2;
-
- jso1 = (json_object* const*)j1;
- jso2 = (json_object* const*)j2;
- if (!*jso1 && !*jso2) {
- return 0;
- }
- if (!*jso1) {
- return -1;
- }
- if (!*jso2) {
- return 1;
- }
-
- i1 = json_object_get_int(*jso1);
- i2 = json_object_get_int(*jso2);
-
- return i1 - i2;
+ json_object * const *jso1, * const *jso2;
+ int i1, i2;
+
+ jso1 = (json_object* const*)j1;
+ jso2 = (json_object* const*)j2;
+ if (!*jso1 && !*jso2)
+ return 0;
+ if (!*jso1)
+ return -1;
+ if (!*jso2)
+ return 1;
+
+ i1 = json_object_get_int(*jso1);
+ i2 = json_object_get_int(*jso2);
+
+ return i1 - i2;
}
#ifdef TEST_FORMATTED
int main(int argc, char **argv)
{
- json_object *my_string, *my_int, *my_object, *my_array;
- int i;
+ json_object *my_string, *my_int, *my_object, *my_array;
+ int i;
#ifdef TEST_FORMATTED
int sflags = 0;
#endif
- MC_SET_DEBUG(1);
+ MC_SET_DEBUG(1);
#ifdef TEST_FORMATTED
sflags = parse_flags(argc, argv);
#endif
- my_string = json_object_new_string("\t");
- printf("my_string=%s\n", json_object_get_string(my_string));
- printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));
- json_object_put(my_string);
-
- my_string = json_object_new_string("\\");
- printf("my_string=%s\n", json_object_get_string(my_string));
- printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));
- json_object_put(my_string);
-
- my_string = json_object_new_string("foo");
- printf("my_string=%s\n", json_object_get_string(my_string));
- printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));
-
- my_int = json_object_new_int(9);
- printf("my_int=%d\n", json_object_get_int(my_int));
- printf("my_int.to_string()=%s\n", json_object_to_json_string(my_int));
-
- my_array = json_object_new_array();
- json_object_array_add(my_array, json_object_new_int(1));
- json_object_array_add(my_array, json_object_new_int(2));
- json_object_array_add(my_array, json_object_new_int(3));
- json_object_array_put_idx(my_array, 4, json_object_new_int(5));
- printf("my_array=\n");
- for(i=0; i < json_object_array_length(my_array); i++) {
- json_object *obj = json_object_array_get_idx(my_array, i);
- printf("\t[%d]=%s\n", i, json_object_to_json_string(obj));
- }
- printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array));
-
- json_object_put(my_array);
-
- my_array = json_object_new_array();
- json_object_array_add(my_array, json_object_new_int(3));
- json_object_array_add(my_array, json_object_new_int(1));
- json_object_array_add(my_array, json_object_new_int(2));
- json_object_array_put_idx(my_array, 4, json_object_new_int(0));
- printf("my_array=\n");
- for(i=0; i < json_object_array_length(my_array); i++) {
- json_object *obj = json_object_array_get_idx(my_array, i);
- printf("\t[%d]=%s\n", i, json_object_to_json_string(obj));
- }
- printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array));
- json_object_array_sort(my_array, sort_fn);
- printf("my_array=\n");
- for(i=0; i < json_object_array_length(my_array); i++) {
- json_object *obj = json_object_array_get_idx(my_array, i);
- printf("\t[%d]=%s\n", i, json_object_to_json_string(obj));
- }
- printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array));
-
- 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"));
- json_object_object_add(my_object, "bool0", json_object_new_boolean(0));
- json_object_object_add(my_object, "bool1", json_object_new_boolean(1));
- json_object_object_add(my_object, "baz", json_object_new_string("bang"));
- json_object_object_add(my_object, "baz", json_object_new_string("fark"));
- json_object_object_del(my_object, "baz");
- /*json_object_object_add(my_object, "arr", my_array);*/
- printf("my_object=\n");
- json_object_object_foreach(my_object, key, val) {
- printf("\t%s: %s\n", key, json_object_to_json_string(val));
- }
- printf("my_object.to_string()=%s\n", json_object_to_json_string(my_object));
-
- json_object_put(my_string);
- json_object_put(my_int);
- json_object_put(my_object);
- json_object_put(my_array);
-
- return 0;
+ my_string = json_object_new_string("\t");
+ printf("my_string=%s\n", json_object_get_string(my_string));
+ printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));
+ json_object_put(my_string);
+
+ my_string = json_object_new_string("\\");
+ printf("my_string=%s\n", json_object_get_string(my_string));
+ printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));
+ json_object_put(my_string);
+
+ my_string = json_object_new_string("foo");
+ printf("my_string=%s\n", json_object_get_string(my_string));
+ printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));
+
+ my_int = json_object_new_int(9);
+ printf("my_int=%d\n", json_object_get_int(my_int));
+ printf("my_int.to_string()=%s\n", json_object_to_json_string(my_int));
+
+ my_array = json_object_new_array();
+ json_object_array_add(my_array, json_object_new_int(1));
+ json_object_array_add(my_array, json_object_new_int(2));
+ json_object_array_add(my_array, json_object_new_int(3));
+ json_object_array_put_idx(my_array, 4, json_object_new_int(5));
+ printf("my_array=\n");
+ for(i=0; i < json_object_array_length(my_array); i++)
+ {
+ json_object *obj = json_object_array_get_idx(my_array, i);
+ printf("\t[%d]=%s\n", i, json_object_to_json_string(obj));
+ }
+ printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array));
+
+ json_object_put(my_array);
+
+ my_array = json_object_new_array();
+ json_object_array_add(my_array, json_object_new_int(3));
+ json_object_array_add(my_array, json_object_new_int(1));
+ json_object_array_add(my_array, json_object_new_int(2));
+ json_object_array_put_idx(my_array, 4, json_object_new_int(0));
+ printf("my_array=\n");
+ for(i=0; i < json_object_array_length(my_array); i++)
+ {
+ json_object *obj = json_object_array_get_idx(my_array, i);
+ printf("\t[%d]=%s\n", i, json_object_to_json_string(obj));
+ }
+ printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array));
+ json_object_array_sort(my_array, sort_fn);
+ printf("my_array=\n");
+ for(i=0; i < json_object_array_length(my_array); i++)
+ {
+ json_object *obj = json_object_array_get_idx(my_array, i);
+ printf("\t[%d]=%s\n", i, json_object_to_json_string(obj));
+ }
+ printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array));
+
+ 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"));
+ json_object_object_add(my_object, "bool0", json_object_new_boolean(0));
+ json_object_object_add(my_object, "bool1", json_object_new_boolean(1));
+ json_object_object_add(my_object, "baz", json_object_new_string("bang"));
+ json_object_object_add(my_object, "baz", json_object_new_string("fark"));
+ json_object_object_del(my_object, "baz");
+ /*json_object_object_add(my_object, "arr", my_array);*/
+ printf("my_object=\n");
+ json_object_object_foreach(my_object, key, val)
+ {
+ printf("\t%s: %s\n", key, json_object_to_json_string(val));
+ }
+ printf("my_object.to_string()=%s\n", json_object_to_json_string(my_object));
+
+ json_object_put(my_string);
+ json_object_put(my_int);
+ json_object_put(my_object);
+ json_object_put(my_array);
+
+ return 0;
}
int main(int argc, char **argv)
{
- json_object *new_obj;
+ json_object *new_obj;
#ifdef TEST_FORMATTED
int sflags = 0;
#endif
- MC_SET_DEBUG(1);
+ MC_SET_DEBUG(1);
#ifdef TEST_FORMATTED
sflags = parse_flags(argc, argv);
#endif
- new_obj = json_tokener_parse("/* more difficult test case */ { \"glossary\": { \"title\": \"example glossary\", \"GlossDiv\": { \"title\": \"S\", \"GlossList\": [ { \"ID\": \"SGML\", \"SortAs\": \"SGML\", \"GlossTerm\": \"Standard Generalized Markup Language\", \"Acronym\": \"SGML\", \"Abbrev\": \"ISO 8879:1986\", \"GlossDef\": \"A meta-markup language, used to create markup languages such as DocBook.\", \"GlossSeeAlso\": [\"GML\", \"XML\", \"markup\"] } ] } } }");
- printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
- json_object_put(new_obj);
+ new_obj = json_tokener_parse("/* more difficult test case */ { \"glossary\": { \"title\": \"example glossary\", \"GlossDiv\": { \"title\": \"S\", \"GlossList\": [ { \"ID\": \"SGML\", \"SortAs\": \"SGML\", \"GlossTerm\": \"Standard Generalized Markup Language\", \"Acronym\": \"SGML\", \"Abbrev\": \"ISO 8879:1986\", \"GlossDef\": \"A meta-markup language, used to create markup languages such as DocBook.\", \"GlossSeeAlso\": [\"GML\", \"XML\", \"markup\"] } ] } } }");
+ printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
+ json_object_put(new_obj);
- return 0;
+ return 0;
}
/*
* gcc -o utf8 utf8.c -I/home/y/include -L./.libs -ljson
-*/
+ */
#include <stdio.h>
#include <string.h>
#include "json_object.h"
#include "json_tokener.h"
-void print_hex( const char* s) {
- const char *iter = s;
- unsigned char ch;
- while ((ch = *iter++) != 0) {
- if( ',' != ch)
- printf("%x ", ch);
- else
- printf( ",");
- }
- printf("\n");
+void print_hex( const char* s)
+{
+ const char *iter = s;
+ unsigned char ch;
+ while ((ch = *iter++) != 0)
+ {
+ if( ',' != ch)
+ printf("%x ", ch);
+ else
+ printf( ",");
+ }
+ printf("\n");
}
-int main() {
- const char *input = "\"\\ud840\\udd26,\\ud840\\udd27,\\ud800\\udd26,\\ud800\\udd27\"";
- const char *expected = "\xF0\xA0\x84\xA6,\xF0\xA0\x84\xA7,\xF0\x90\x84\xA6,\xF0\x90\x84\xA7";
- struct json_object *parse_result = json_tokener_parse((char*)input);
- const char *unjson = json_object_get_string(parse_result);
+int main()
+{
+ const char *input = "\"\\ud840\\udd26,\\ud840\\udd27,\\ud800\\udd26,\\ud800\\udd27\"";
+ const char *expected = "\xF0\xA0\x84\xA6,\xF0\xA0\x84\xA7,\xF0\x90\x84\xA6,\xF0\x90\x84\xA7";
+ struct json_object *parse_result = json_tokener_parse((char*)input);
+ const char *unjson = json_object_get_string(parse_result);
- printf("input: %s\n", input);
+ printf("input: %s\n", input);
- int strings_match = !strcmp( expected, unjson);
+ int strings_match = !strcmp( expected, unjson);
int retval = 0;
- if (strings_match) {
- printf("JSON parse result is correct: %s\n", unjson);
- printf("PASS\n");
- } else {
- printf("JSON parse result doesn't match expected string\n");
- printf("expected string bytes: ");
- print_hex( expected);
- printf("parsed string bytes: ");
- print_hex( unjson);
- printf("FAIL\n");
- retval = 1;
- }
+ if (strings_match)
+ {
+ printf("JSON parse result is correct: %s\n", unjson);
+ printf("PASS\n");
+ } else {
+ printf("JSON parse result doesn't match expected string\n");
+ printf("expected string bytes: ");
+ print_hex( expected);
+ printf("parsed string bytes: ");
+ print_hex( unjson);
+ printf("FAIL\n");
+ retval = 1;
+ }
json_object_put(parse_result);
return retval;
}
checktype(new_obj, "big_number");
checktype(new_obj, "a_null");
- json_object_put(new_obj);
+ json_object_put(new_obj);
- return 0;
+ return 0;
}
static void getit(struct json_object *new_obj, const char *field)
#include "json_inttypes.h"
#include "json_object.h"
-int main() {
- // this test has a space after the null character. check that it's still included
- const char *input = " \0 ";
- const char *expected = "\" \\u0000 \"";
- struct json_object *string = json_object_new_string_len(input, 3);
- const char *json = json_object_to_json_string(string);
+int main()
+{
+ // this test has a space after the null character. check that it's still included
+ const char *input = " \0 ";
+ const char *expected = "\" \\u0000 \"";
+ struct json_object *string = json_object_new_string_len(input, 3);
+ const char *json = json_object_to_json_string(string);
- int strings_match = !strcmp( expected, json);
- int retval = 0;
- if (strings_match) {
- printf("JSON write result is correct: %s\n", json);
- printf("PASS\n");
- } else {
- printf("JSON write result doesn't match expected string\n");
- printf("expected string: ");
- printf("%s\n", expected);
- printf("parsed string: ");
- printf("%s\n", json);
- printf("FAIL\n");
- retval=1;
- }
- json_object_put(string);
- return retval;
+ int strings_match = !strcmp( expected, json);
+ int retval = 0;
+ if (strings_match)
+ {
+ printf("JSON write result is correct: %s\n", json);
+ printf("PASS\n");
+ } else {
+ printf("JSON write result doesn't match expected string\n");
+ printf("expected string: ");
+ printf("%s\n", expected);
+ printf("parsed string: ");
+ printf("%s\n", json);
+ printf("FAIL\n");
+ retval=1;
+ }
+ json_object_put(string);
+ return retval;
}
static void test_basic_parse()
{
- json_object *new_obj;
+ json_object *new_obj;
- new_obj = json_tokener_parse("\"\003\"");
- printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
- json_object_put(new_obj);
+ new_obj = json_tokener_parse("\"\003\"");
+ printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
+ json_object_put(new_obj);
- new_obj = json_tokener_parse("/* hello */\"foo\"");
- printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
- json_object_put(new_obj);
+ new_obj = json_tokener_parse("/* hello */\"foo\"");
+ printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
+ json_object_put(new_obj);
- new_obj = json_tokener_parse("// hello\n\"foo\"");
- printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
- json_object_put(new_obj);
+ new_obj = json_tokener_parse("// hello\n\"foo\"");
+ printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
+ json_object_put(new_obj);
- new_obj = json_tokener_parse("\"\\u0041\\u0042\\u0043\"");
- printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
- json_object_put(new_obj);
+ new_obj = json_tokener_parse("\"\\u0041\\u0042\\u0043\"");
+ printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
+ json_object_put(new_obj);
- new_obj = json_tokener_parse("null");
- printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
- json_object_put(new_obj);
+ new_obj = json_tokener_parse("null");
+ printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
+ json_object_put(new_obj);
- new_obj = json_tokener_parse("True");
- printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
- json_object_put(new_obj);
+ new_obj = json_tokener_parse("True");
+ printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
+ json_object_put(new_obj);
- new_obj = json_tokener_parse("12");
- printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
- json_object_put(new_obj);
+ new_obj = json_tokener_parse("12");
+ printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
+ json_object_put(new_obj);
- new_obj = json_tokener_parse("12.3");
- printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
- json_object_put(new_obj);
+ new_obj = json_tokener_parse("12.3");
+ printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
+ json_object_put(new_obj);
- new_obj = json_tokener_parse("[\"\\n\"]");
- printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
- json_object_put(new_obj);
+ new_obj = json_tokener_parse("[\"\\n\"]");
+ printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
+ json_object_put(new_obj);
- new_obj = json_tokener_parse("[\"\\nabc\\n\"]");
- printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
- json_object_put(new_obj);
+ new_obj = json_tokener_parse("[\"\\nabc\\n\"]");
+ printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
+ json_object_put(new_obj);
- new_obj = json_tokener_parse("[null]");
- printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
- json_object_put(new_obj);
+ new_obj = json_tokener_parse("[null]");
+ printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
+ json_object_put(new_obj);
- new_obj = json_tokener_parse("[]");
- printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
- json_object_put(new_obj);
+ new_obj = json_tokener_parse("[]");
+ printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
+ json_object_put(new_obj);
- new_obj = json_tokener_parse("[false]");
- printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
- json_object_put(new_obj);
+ new_obj = json_tokener_parse("[false]");
+ printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
+ json_object_put(new_obj);
- new_obj = json_tokener_parse("[\"abc\",null,\"def\",12]");
- printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
- json_object_put(new_obj);
+ new_obj = json_tokener_parse("[\"abc\",null,\"def\",12]");
+ printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
+ json_object_put(new_obj);
- new_obj = json_tokener_parse("{}");
- printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
- json_object_put(new_obj);
+ new_obj = json_tokener_parse("{}");
+ printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
+ json_object_put(new_obj);
- new_obj = json_tokener_parse("{ \"foo\": \"bar\" }");
- printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
- json_object_put(new_obj);
+ new_obj = json_tokener_parse("{ \"foo\": \"bar\" }");
+ printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
+ json_object_put(new_obj);
- new_obj = json_tokener_parse("{ \"foo\": \"bar\", \"baz\": null, \"bool0\": true }");
- printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
- json_object_put(new_obj);
+ new_obj = json_tokener_parse("{ \"foo\": \"bar\", \"baz\": null, \"bool0\": true }");
+ printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
+ json_object_put(new_obj);
- new_obj = json_tokener_parse("{ \"foo\": [null, \"foo\"] }");
- printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
- json_object_put(new_obj);
+ new_obj = json_tokener_parse("{ \"foo\": [null, \"foo\"] }");
+ printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
+ json_object_put(new_obj);
- new_obj = json_tokener_parse("{ \"abc\": 12, \"foo\": \"bar\", \"bool0\": false, \"bool1\": true, \"arr\": [ 1, 2, 3, null, 5 ] }");
- printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
- json_object_put(new_obj);
+ new_obj = json_tokener_parse("{ \"abc\": 12, \"foo\": \"bar\", \"bool0\": false, \"bool1\": true, \"arr\": [ 1, 2, 3, null, 5 ] }");
+ printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
+ json_object_put(new_obj);
}
static void test_verbose_parse()
new_obj = json_tokener_parse_verbose("foo", &error);
assert (new_obj == NULL);
- /* b/c the string starts with 'f' parsing return a boolean error */
+ /* b/c the string starts with 'f' parsing return a boolean error */
assert (error == json_tokener_error_parse_boolean);
printf("json_tokener_parse_versbose() OK\n");
static void test_basic_printbuf_memset()
{
struct printbuf *pb;
-
+
printf("%s: starting test\n", __func__);
pb = printbuf_new();
sprintbuf(pb, "blue:%d", 1);
memset(data, 'X', *before_resize + 1);
printbuf_memappend_fast(pb, data, *before_resize + 1);
printf("Append to just after resize: %d, [%s]\n", printbuf_length(pb), pb->buf);
-
+
free(data);
printbuf_free(pb);
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;
+ freeit_was_called = 0;
json_object_put(my_object);
assert(freeit_was_called);