clang-format the files

This commit is contained in:
dota17
2020-03-28 10:25:00 +08:00
parent c117d8a8a8
commit 8b162c4b89
54 changed files with 2860 additions and 2713 deletions

View File

@@ -7,19 +7,20 @@
#include "parse_flags.h"
#if !defined(HAVE_STRCASECMP) && defined(_MSC_VER)
# define strcasecmp _stricmp
#define strcasecmp _stricmp
#elif !defined(HAVE_STRCASECMP)
# error You do not have strcasecmp on your system.
#error You do not have strcasecmp on your system.
#endif /* HAVE_STRNCASECMP */
static struct {
static struct
{
const char *arg;
int flag;
} format_args[] = {
{ "plain", JSON_C_TO_STRING_PLAIN },
{ "spaced", JSON_C_TO_STRING_SPACED },
{ "pretty", JSON_C_TO_STRING_PRETTY },
{ "pretty_tab", JSON_C_TO_STRING_PRETTY_TAB },
{"plain", JSON_C_TO_STRING_PLAIN},
{"spaced", JSON_C_TO_STRING_SPACED},
{"pretty", JSON_C_TO_STRING_PRETTY},
{"pretty_tab", JSON_C_TO_STRING_PRETTY_TAB},
};
#ifndef NELEM
@@ -30,7 +31,7 @@ int parse_flags(int argc, char **argv)
{
int arg_idx;
int sflags = 0;
for (arg_idx = 1; arg_idx < argc ; arg_idx++)
for (arg_idx = 1; arg_idx < argc; arg_idx++)
{
int jj;
for (jj = 0; jj < (int)NELEM(format_args); jj++)

View File

@@ -1,19 +1,19 @@
#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <assert.h>
#include "json.h"
#include "parse_flags.h"
static int sort_fn (const void *j1, const void *j2)
static int sort_fn(const void *j1, const void *j2)
{
json_object * const *jso1, * const *jso2;
json_object *const *jso1, *const *jso2;
int i1, i2;
jso1 = (json_object* const*)j1;
jso2 = (json_object* const*)j2;
jso1 = (json_object *const *)j1;
jso2 = (json_object *const *)j2;
if (!*jso1 && !*jso2)
return 0;
if (!*jso1)
@@ -38,7 +38,8 @@ static const char *to_json_string(json_object *obj, int flags)
copy = strdup(result);
if (copy == NULL)
printf("to_json_string: Allocation failed!\n");
else {
else
{
result = json_object_to_json_string_ext(obj, flags);
if (length != strlen(result))
printf("to_json_string: Length mismatch!\n");
@@ -48,7 +49,7 @@ static const char *to_json_string(json_object *obj, int flags)
}
return result;
}
#define json_object_to_json_string(obj) to_json_string(obj,sflags)
#define json_object_to_json_string(obj) to_json_string(obj, sflags)
#else
/* no special define */
#endif
@@ -84,7 +85,7 @@ void test_array_del_idx()
orig_array_len = json_object_array_length(my_array);
printf("my_array=\n");
for(ii = 0; ii < json_object_array_length(my_array); ii++)
for (ii = 0; ii < json_object_array_length(my_array); ii++)
{
json_object *obj = json_object_array_get_idx(my_array, ii);
printf("\t[%d]=%s\n", (int)ii, json_object_to_json_string(obj));
@@ -94,38 +95,38 @@ void test_array_del_idx()
for (ii = 0; ii < orig_array_len; ii++)
{
rc = json_object_array_del_idx(my_array, 0, 1);
printf("after del_idx(0,1)=%d, my_array.to_string()=%s\n",
rc, json_object_to_json_string(my_array));
printf("after del_idx(0,1)=%d, my_array.to_string()=%s\n", rc,
json_object_to_json_string(my_array));
}
/* One more time, with the empty array: */
rc = json_object_array_del_idx(my_array, 0, 1);
printf("after del_idx(0,1)=%d, my_array.to_string()=%s\n",
rc, json_object_to_json_string(my_array));
printf("after del_idx(0,1)=%d, my_array.to_string()=%s\n", rc,
json_object_to_json_string(my_array));
json_object_put(my_array);
/* Delete all array indexes at once */
my_array = make_array();
rc = json_object_array_del_idx(my_array, 0, orig_array_len);
printf("after del_idx(0,%d)=%d, my_array.to_string()=%s\n",
(int)orig_array_len, rc, json_object_to_json_string(my_array));
printf("after del_idx(0,%d)=%d, my_array.to_string()=%s\n", (int)orig_array_len, rc,
json_object_to_json_string(my_array));
json_object_put(my_array);
/* Delete *more* than all array indexes at once */
my_array = make_array();
rc = json_object_array_del_idx(my_array, 0, orig_array_len + 1);
printf("after del_idx(0,%d)=%d, my_array.to_string()=%s\n",
(int)(orig_array_len + 1), rc, json_object_to_json_string(my_array));
printf("after del_idx(0,%d)=%d, my_array.to_string()=%s\n", (int)(orig_array_len + 1), rc,
json_object_to_json_string(my_array));
json_object_put(my_array);
/* Delete some array indexes, then add more */
my_array = make_array();
rc = json_object_array_del_idx(my_array, 0, orig_array_len - 1);
printf("after del_idx(0,%d)=%d, my_array.to_string()=%s\n",
(int)(orig_array_len - 1), rc, json_object_to_json_string(my_array));
printf("after del_idx(0,%d)=%d, my_array.to_string()=%s\n", (int)(orig_array_len - 1), rc,
json_object_to_json_string(my_array));
json_object_array_add(my_array, json_object_new_string("s1"));
json_object_array_add(my_array, json_object_new_string("s2"));
json_object_array_add(my_array, json_object_new_string("s3"));
@@ -162,13 +163,15 @@ int main(int argc, char **argv)
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));
printf("my_string.to_string(NOSLASHESCAPE)=%s\n", json_object_to_json_string_ext(my_string, JSON_C_TO_STRING_NOSLASHESCAPE));
printf("my_string.to_string(NOSLASHESCAPE)=%s\n",
json_object_to_json_string_ext(my_string, JSON_C_TO_STRING_NOSLASHESCAPE));
json_object_put(my_string);
my_string = json_object_new_string("/foo/bar/baz");
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));
printf("my_string.to_string(NOSLASHESCAPE)=%s\n", json_object_to_json_string_ext(my_string, JSON_C_TO_STRING_NOSLASHESCAPE));
printf("my_string.to_string(NOSLASHESCAPE)=%s\n",
json_object_to_json_string_ext(my_string, JSON_C_TO_STRING_NOSLASHESCAPE));
json_object_put(my_string);
my_string = json_object_new_string("foo");
@@ -188,7 +191,7 @@ int main(int argc, char **argv)
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++)
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", (int)i, json_object_to_json_string(obj));
@@ -205,7 +208,7 @@ int main(int argc, char **argv)
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++)
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", (int)i, json_object_to_json_string(obj));
@@ -213,7 +216,7 @@ int main(int argc, char **argv)
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++)
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", (int)i, json_object_to_json_string(obj));

View File

@@ -1,18 +1,17 @@
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include "json.h"
#include "parse_flags.h"
#ifdef TEST_FORMATTED
#define json_object_to_json_string(obj) json_object_to_json_string_ext(obj,sflags)
#define json_object_to_json_string(obj) json_object_to_json_string_ext(obj, sflags)
#else
/* no special define */
#endif
int main(int argc, char **argv)
{
json_object *new_obj;
@@ -26,8 +25,13 @@ int main(int argc, char **argv)
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\"] } ] } } }");
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);

View File

@@ -2,24 +2,24 @@
* gcc -o utf8 utf8.c -I/home/y/include -L./.libs -ljson
*/
#include "config.h"
#include <stdio.h>
#include <string.h>
#include "config.h"
#include "json_inttypes.h"
#include "json_object.h"
#include "json_tokener.h"
void print_hex(const char* s)
void print_hex(const char *s)
{
const char *iter = s;
unsigned char ch;
while ((ch = *iter++) != 0)
{
if( ',' != ch)
if (',' != ch)
printf("%x ", ch);
else
printf( ",");
printf(",");
}
putchar('\n');
}
@@ -27,19 +27,22 @@ void print_hex(const char* s)
int main(void)
{
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";
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(input);
const char *unjson = json_object_get_string(parse_result);
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);
puts("PASS");
} else {
}
else
{
printf("JSON parse result doesn't match expected string\n");
printf("expected string bytes: ");
print_hex(expected);

View File

@@ -1,6 +1,6 @@
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include "json.h"
@@ -67,12 +67,12 @@ int main(int argc, char **argv)
}
if (new_count != orig_count)
{
printf("mismatch between original count (%d) and new count (%d)\n",
orig_count, new_count);
printf("mismatch between original count (%d) and new count (%d)\n", orig_count,
new_count);
retval = 1;
}
json_object_put( my_object );
json_object_put(my_object);
return retval;
}

View File

@@ -3,10 +3,10 @@
* Also checks the json_object_get_type and json_object_is_type functions.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "json_inttypes.h"
#include "json_object.h"
@@ -75,44 +75,31 @@ static void getit(struct json_object *new_obj, const char *field)
printf("Field %s does not exist\n", field);
enum json_type o_type = json_object_get_type(o);
printf("new_obj.%s json_object_get_type()=%s\n", field,
json_type_to_name(o_type));
printf("new_obj.%s json_object_get_int()=%d\n", field,
json_object_get_int(o));
printf("new_obj.%s json_object_get_int64()=%" PRId64 "\n", field,
json_object_get_int64(o));
printf("new_obj.%s json_object_get_type()=%s\n", field, json_type_to_name(o_type));
printf("new_obj.%s json_object_get_int()=%d\n", field, json_object_get_int(o));
printf("new_obj.%s json_object_get_int64()=%" PRId64 "\n", field, json_object_get_int64(o));
printf("new_obj.%s json_object_get_uint64()=%" PRIu64 "\n", field,
json_object_get_uint64(o));
printf("new_obj.%s json_object_get_boolean()=%d\n", field,
json_object_get_boolean(o));
printf("new_obj.%s json_object_get_double()=%f\n", field,
json_object_get_double(o));
printf("new_obj.%s json_object_get_boolean()=%d\n", field, json_object_get_boolean(o));
printf("new_obj.%s json_object_get_double()=%f\n", field, json_object_get_double(o));
}
static void checktype_header()
{
printf("json_object_is_type: %s,%s,%s,%s,%s,%s,%s\n",
json_type_to_name(json_type_null),
json_type_to_name(json_type_boolean),
json_type_to_name(json_type_double),
json_type_to_name(json_type_int),
json_type_to_name(json_type_object),
json_type_to_name(json_type_array),
json_type_to_name(json_type_string));
printf("json_object_is_type: %s,%s,%s,%s,%s,%s,%s\n", json_type_to_name(json_type_null),
json_type_to_name(json_type_boolean), json_type_to_name(json_type_double),
json_type_to_name(json_type_int), json_type_to_name(json_type_object),
json_type_to_name(json_type_array), json_type_to_name(json_type_string));
}
static void checktype(struct json_object *new_obj, const char *field)
{
struct json_object *o = new_obj;
if (field && !json_object_object_get_ex(new_obj, field, &o))
printf("Field %s does not exist\n", field);
printf("new_obj%s%-18s: %d,%d,%d,%d,%d,%d,%d\n",
field ? "." : " ", field ? field : "",
json_object_is_type(o, json_type_null),
json_object_is_type(o, json_type_boolean),
json_object_is_type(o, json_type_double),
json_object_is_type(o, json_type_int),
json_object_is_type(o, json_type_object),
json_object_is_type(o, json_type_array),
json_object_is_type(o, json_type_string));
printf("new_obj%s%-18s: %d,%d,%d,%d,%d,%d,%d\n", field ? "." : " ", field ? field : "",
json_object_is_type(o, json_type_null), json_object_is_type(o, json_type_boolean),
json_object_is_type(o, json_type_double), json_object_is_type(o, json_type_int),
json_object_is_type(o, json_type_object), json_object_is_type(o, json_type_array),
json_object_is_type(o, json_type_string));
}

View File

@@ -1,8 +1,8 @@
#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <assert.h>
#include "json.h"
#include "json_tokener.h"
@@ -28,13 +28,13 @@ static void test_case_parse()
json_tokener_set_flags(tok, JSON_TOKENER_STRICT);
new_obj = json_tokener_parse_ex(tok, "True", 4);
assert (new_obj == NULL);
assert(new_obj == NULL);
new_obj = json_tokener_parse_ex(tok, "False", 5);
assert (new_obj == NULL);
assert(new_obj == NULL);
new_obj = json_tokener_parse_ex(tok, "Null", 4);
assert (new_obj == NULL);
assert(new_obj == NULL);
printf("OK\n");

View File

@@ -2,9 +2,9 @@
* Tests if json_object_equal behaves correct.
*/
#include "config.h"
#include <stdio.h>
#include <string.h>
#include "config.h"
#include "json_inttypes.h"
#include "json_object.h"

View File

@@ -1,6 +1,6 @@
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#ifdef NDEBUG
#undef NDEBUG
@@ -14,76 +14,76 @@
static void do_benchmark(json_object *src1);
static const char *json_str1 =
"{"
" \"glossary\": {"
" \"title\": \"example glossary\","
" \"GlossDiv\": {"
" \"number\": 16446744073709551615,"
" \"title\": \"S\","
" \"null_obj\": null, "
" \"exixt\": false,"
" \"quantity\":20,"
" \"univalent\":19.8,"
" \"GlossList\": {"
" \"GlossEntry\": {"
" \"ID\": \"SGML\","
" \"SortAs\": \"SGML\","
" \"GlossTerm\": \"Standard Generalized Markup Language\","
" \"Acronym\": \"SGML\","
" \"Abbrev\": \"ISO 8879:1986\","
" \"GlossDef\": {"
" \"para\": \"A meta-markup language, used to create markup languages such as DocBook.\","
" \"GlossSeeAlso\": [\"GML\", \"XML\"]"
" },"
" \"GlossSee\": \"markup\""
" }"
" }"
" }"
" }"
"}";
static const char *json_str1 =
"{"
" \"glossary\": {"
" \"title\": \"example glossary\","
" \"GlossDiv\": {"
" \"number\": 16446744073709551615,"
" \"title\": \"S\","
" \"null_obj\": null, "
" \"exixt\": false,"
" \"quantity\":20,"
" \"univalent\":19.8,"
" \"GlossList\": {"
" \"GlossEntry\": {"
" \"ID\": \"SGML\","
" \"SortAs\": \"SGML\","
" \"GlossTerm\": \"Standard Generalized Markup Language\","
" \"Acronym\": \"SGML\","
" \"Abbrev\": \"ISO 8879:1986\","
" \"GlossDef\": {"
" \"para\": \"A meta-markup language, used to create markup languages "
"such as DocBook.\","
" \"GlossSeeAlso\": [\"GML\", \"XML\"]"
" },"
" \"GlossSee\": \"markup\""
" }"
" }"
" }"
" }"
"}";
static const char *json_str2 =
"{\"menu\": {"
" \"header\": \"SVG Viewer\","
" \"items\": ["
" {\"id\": \"Open\"},"
" {\"id\": \"OpenNew\", \"label\": \"Open New\"},"
" null,"
" {\"id\": \"ZoomIn\", \"label\": \"Zoom In\"},"
" {\"id\": \"ZoomOut\", \"label\": \"Zoom Out\"},"
" {\"id\": \"OriginalView\", \"label\": \"Original View\"},"
" null,"
" {\"id\": \"Quality\", \"another_null\": null},"
" {\"id\": \"Pause\"},"
" {\"id\": \"Mute\"},"
" null,"
" {\"id\": \"Find\", \"label\": \"Find...\"},"
" {\"id\": \"FindAgain\", \"label\": \"Find Again\"},"
" {\"id\": \"Copy\"},"
" {\"id\": \"CopyAgain\", \"label\": \"Copy Again\"},"
" {\"id\": \"CopySVG\", \"label\": \"Copy SVG\"},"
" {\"id\": \"ViewSVG\", \"label\": \"View SVG\"},"
" {\"id\": \"ViewSource\", \"label\": \"View Source\"},"
" {\"id\": \"SaveAs\", \"label\": \"Save As\"},"
" null,"
" {\"id\": \"Help\"},"
" {\"id\": \"About\", \"label\": \"About Adobe CVG Viewer...\"}"
" ]"
"}}";
"{\"menu\": {"
" \"header\": \"SVG Viewer\","
" \"items\": ["
" {\"id\": \"Open\"},"
" {\"id\": \"OpenNew\", \"label\": \"Open New\"},"
" null,"
" {\"id\": \"ZoomIn\", \"label\": \"Zoom In\"},"
" {\"id\": \"ZoomOut\", \"label\": \"Zoom Out\"},"
" {\"id\": \"OriginalView\", \"label\": \"Original View\"},"
" null,"
" {\"id\": \"Quality\", \"another_null\": null},"
" {\"id\": \"Pause\"},"
" {\"id\": \"Mute\"},"
" null,"
" {\"id\": \"Find\", \"label\": \"Find...\"},"
" {\"id\": \"FindAgain\", \"label\": \"Find Again\"},"
" {\"id\": \"Copy\"},"
" {\"id\": \"CopyAgain\", \"label\": \"Copy Again\"},"
" {\"id\": \"CopySVG\", \"label\": \"Copy SVG\"},"
" {\"id\": \"ViewSVG\", \"label\": \"View SVG\"},"
" {\"id\": \"ViewSource\", \"label\": \"View Source\"},"
" {\"id\": \"SaveAs\", \"label\": \"Save As\"},"
" null,"
" {\"id\": \"Help\"},"
" {\"id\": \"About\", \"label\": \"About Adobe CVG Viewer...\"}"
" ]"
"}}";
static const char *json_str3 =
"{\"menu\": {"
" \"id\": \"file\","
" \"value\": \"File\","
" \"popup\": {"
" \"menuitem\": ["
" {\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"},"
" {\"value\": \"Open\", \"onclick\": \"OpenDoc()\"},"
" {\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}"
" ]"
" }"
"}}";
static const char *json_str3 = "{\"menu\": {"
" \"id\": \"file\","
" \"value\": \"File\","
" \"popup\": {"
" \"menuitem\": ["
" {\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"},"
" {\"value\": \"Open\", \"onclick\": \"OpenDoc()\"},"
" {\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}"
" ]"
" }"
"}}";
json_object_to_json_string_fn my_custom_serializer;
int my_custom_serializer(struct json_object *jso, struct printbuf *pb, int level, int flags)
@@ -93,7 +93,8 @@ int my_custom_serializer(struct json_object *jso, struct printbuf *pb, int level
}
json_c_shallow_copy_fn my_shallow_copy;
int my_shallow_copy(json_object *src, json_object *parent, const char *key, size_t index, json_object **dst)
int my_shallow_copy(json_object *src, json_object *parent, const char *key, size_t index,
json_object **dst)
{
int rc;
rc = json_c_shallow_copy_default(src, parent, key, index, dst);
@@ -109,7 +110,6 @@ int my_shallow_copy(json_object *src, json_object *parent, const char *key, size
return rc;
}
int main(int argc, char **argv)
{
struct json_object *src1, *src2, *src3;
@@ -238,19 +238,24 @@ static void do_benchmark(json_object *src2)
time_t start = time(NULL);
start = time(NULL);
for (ii = 0; ii < iterations; ii++) {
for (ii = 0; ii < iterations; ii++)
{
dst2 = json_tokener_parse(json_object_get_string(src2));
json_object_put(dst2);
}
printf("BENCHMARK - %d iterations of 'dst2 = json_tokener_parse(json_object_get_string(src2))' took %d seconds\n", iterations, (int)(time(NULL) - start));
printf("BENCHMARK - %d iterations of 'dst2 = "
"json_tokener_parse(json_object_get_string(src2))' took %d seconds\n",
iterations, (int)(time(NULL) - start));
start = time(NULL);
dst2 = NULL;
for (ii = 0; ii < iterations; ii++) {
for (ii = 0; ii < iterations; ii++)
{
json_object_deep_copy(src2, &dst2, NULL);
json_object_put(dst2);
dst2 = NULL;
}
printf("BENCHMARK - %d iterations of 'json_object_deep_copy(src2, &dst2, NULL)' took %d seconds\n", iterations, (int)(time(NULL) - start));
printf("BENCHMARK - %d iterations of 'json_object_deep_copy(src2, &dst2, NULL)' took %d "
"seconds\n",
iterations, (int)(time(NULL) - start));
}

View File

@@ -2,8 +2,8 @@
* Tests if the format string for double serialization is handled correctly
*/
#include <stdio.h>
#include "config.h"
#include <stdio.h>
#include "json_object.h"
#include "json_object_private.h"
@@ -49,7 +49,8 @@ int main()
if (json_c_set_serialization_double_format("T%0.2fX", JSON_C_OPTION_THREAD) < 0)
printf("ERROR: json_c_set_serialization_double_format() failed");
printf("obj.to_string(with thread format)=%s\n", json_object_to_json_string(obj));
if (json_c_set_serialization_double_format("Ttttttttttttt%0.2fxxxxxxxxxxxxxxxxxxX", JSON_C_OPTION_THREAD) < 0)
if (json_c_set_serialization_double_format("Ttttttttttttt%0.2fxxxxxxxxxxxxxxxxxxX",
JSON_C_OPTION_THREAD) < 0)
printf("ERROR: json_c_set_serialization_double_format() failed");
printf("obj.to_string(long thread format)=%s\n", json_object_to_json_string(obj));
if (json_c_set_serialization_double_format(NULL, JSON_C_OPTION_THREAD) < 0)
@@ -96,11 +97,11 @@ int main()
json_object_put(obj);
/* Test Infinity and -Infinity handling */
obj = json_object_new_double(1.0/zero_dot_zero);
obj = json_object_new_double(1.0 / zero_dot_zero);
printf("obj(1.0/0.0)=%s\n", json_object_to_json_string(obj));
json_object_put(obj);
obj = json_object_new_double(-1.0/zero_dot_zero);
obj = json_object_new_double(-1.0 / zero_dot_zero);
printf("obj(-1.0/0.0)=%s\n", json_object_to_json_string(obj));
json_object_put(obj);

View File

@@ -1,28 +1,28 @@
/* Copyright (C) 2016 by Rainer Gerhards
* Released under ASL 2.0 */
#include "config.h"
#include <stdio.h>
#include "json_object.h"
#include "json_tokener.h"
#include <stdio.h>
int main(void)
{
json_object *json;
json_object *json;
json = json_object_new_double(1.0);
printf("json = %s\n", json_object_to_json_string_ext(json, JSON_C_TO_STRING_PRETTY));
json_object_put(json);
json = json_object_new_double(1.0);
printf("json = %s\n", json_object_to_json_string_ext(json, JSON_C_TO_STRING_PRETTY));
json_object_put(json);
json = json_object_new_double(-1.0);
printf("json = %s\n", json_object_to_json_string_ext(json, JSON_C_TO_STRING_PRETTY));
json_object_put(json);
json = json_object_new_double(1.23);
printf("json = %s\n", json_object_to_json_string_ext(json, JSON_C_TO_STRING_PRETTY));
json_object_put(json);
json = json_object_new_double(123456789.0);
printf("json = %s\n", json_object_to_json_string_ext(json, JSON_C_TO_STRING_PRETTY));
json_object_put(json);
json = json_object_new_double(123456789.123);
printf("json = %s\n", json_object_to_json_string_ext(json, JSON_C_TO_STRING_PRETTY));
json_object_put(json);
return 0;
json = json_object_new_double(-1.0);
printf("json = %s\n", json_object_to_json_string_ext(json, JSON_C_TO_STRING_PRETTY));
json_object_put(json);
json = json_object_new_double(1.23);
printf("json = %s\n", json_object_to_json_string_ext(json, JSON_C_TO_STRING_PRETTY));
json_object_put(json);
json = json_object_new_double(123456789.0);
printf("json = %s\n", json_object_to_json_string_ext(json, JSON_C_TO_STRING_PRETTY));
json_object_put(json);
json = json_object_new_double(123456789.123);
printf("json = %s\n", json_object_to_json_string_ext(json, JSON_C_TO_STRING_PRETTY));
json_object_put(json);
return 0;
}

View File

@@ -43,7 +43,7 @@ int main(int argc, char **argv)
assert(json_object_get_int64(tmp) != INT64_MIN);
json_object_put(tmp);
printf("INT64 ADD UNDERFLOW PASSED\n");
// uint64 + negative int64--> negative int64
// uint64 + negative int64--> negative int64
tmp = json_object_new_uint64(400);
json_object_int_inc(tmp, -200);
assert(json_object_get_int64(tmp) == 200);

View File

@@ -20,17 +20,17 @@ static void test_example_int(struct json_object *jo1, const char *json_pointer,
}
static const char *input_json_str = "{ "
"'foo': ['bar', 'baz'], "
"'': 0, "
"'a/b': 1, "
"'c%d': 2, "
"'e^f': 3, "
"'g|h': 4, "
"'i\\\\j': 5, "
"'k\\\"l': 6, "
"' ': 7, "
"'m~n': 8 "
"}";
"'foo': ['bar', 'baz'], "
"'': 0, "
"'a/b': 1, "
"'c%d': 2, "
"'e^f': 3, "
"'g|h': 4, "
"'i\\\\j': 5, "
"'k\\\"l': 6, "
"' ': 7, "
"'m~n': 8 "
"}";
/* clang-format off */
static const char *rec_input_json_str =
@@ -64,7 +64,8 @@ static void test_example_get()
{
int i;
struct json_object *jo1, *jo2, *jo3;
struct json_pointer_map_s_i {
struct json_pointer_map_s_i
{
const char *s;
int i;
};
@@ -118,7 +119,7 @@ static void test_example_get()
assert(0 == strcmp("bar", json_object_get_string(jo2)));
printf("PASSED - GET - /foo/0 == 'bar'\n");
for (i = 0 ; json_pointers[i].s; i++)
for (i = 0; json_pointers[i].s; i++)
test_example_int(jo1, json_pointers[i].s, json_pointers[i].i);
json_object_put(jo1);
@@ -239,7 +240,8 @@ static void test_example_set()
printf("%s\n", json_object_get_string(jo1));
assert(0 == json_pointer_set(&jo1, "/foo/1", json_object_new_string("cod")));
assert(0 == strcmp("cod", json_object_get_string(json_object_array_get_idx(json_object_object_get(jo1, "foo"), 1))));
assert(0 == strcmp("cod", json_object_get_string(json_object_array_get_idx(
json_object_object_get(jo1, "foo"), 1))));
printf("PASSED - SET - 'cod' in /foo/1\n");
assert(0 != json_pointer_set(&jo1, "/fud/gaw", (jo2 = json_tokener_parse("[1,2,3]"))));
assert(errno == ENOENT);
@@ -256,7 +258,9 @@ static void test_example_set()
assert(0 == json_pointer_set(&jo1, "/", json_object_new_int(9)));
printf("PASSED - SET - / == 9\n");
jo2 = json_tokener_parse("{ 'foo': [ 'bar', 'cod' ], '': 9, 'a/b': 1, 'c%d': 2, 'e^f': 3, 'g|h': 4, 'i\\\\j': 5, 'k\\\"l': 6, ' ': 7, 'm~n': 8, 'fud': { 'gaw': [ 0, 2, 3, 4 ] } }");
jo2 = json_tokener_parse(
"{ 'foo': [ 'bar', 'cod' ], '': 9, 'a/b': 1, 'c%d': 2, 'e^f': 3, 'g|h': 4, 'i\\\\j': "
"5, 'k\\\"l': 6, ' ': 7, 'm~n': 8, 'fud': { 'gaw': [ 0, 2, 3, 4 ] } }");
assert(json_object_equal(jo2, jo1));
printf("PASSED - SET - Final JSON is: %s\n", json_object_get_string(jo1));
json_object_put(jo2);
@@ -286,7 +290,8 @@ static void test_wrong_inputs_set()
printf("PASSED - SET - failed 'cod' with path 'foo/bar'\n");
json_object_put(jo2);
assert(0 != json_pointer_setf(&jo1, (jo2 = json_object_new_string("cod")), "%s", "foo/bar"));
assert(0 !=
json_pointer_setf(&jo1, (jo2 = json_object_new_string("cod")), "%s", "foo/bar"));
printf("PASSED - SET - failed 'cod' with path 'foo/bar'\n");
json_object_put(jo2);

View File

@@ -1,8 +1,8 @@
#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <assert.h>
#include "config.h"
#include "json.h"
@@ -37,8 +37,7 @@ int main(int argc, char **argv)
(void)snprintf(buf2, sizeof(buf2), "%f", 0.1);
if (strcmp(buf1, buf2) != 0)
printf("ERROR: Original locale not restored \"%s\" != \"%s\"",
buf1, buf2);
printf("ERROR: Original locale not restored \"%s\" != \"%s\"", buf1, buf2);
#ifdef HAVE_SETLOCALE
setlocale(LC_NUMERIC, "C");
@@ -49,16 +48,16 @@ int main(int argc, char **argv)
// string that was parsed. (see json_object_new_double_s())
printf("new_obj.to_string()=[");
unsigned int ii;
for (ii = 0 ; ii < json_object_array_length(new_obj); ii++)
for (ii = 0; ii < json_object_array_length(new_obj); ii++)
{
json_object *val = json_object_array_get_idx(new_obj, ii);
printf("%s%.2lf", (ii > 0) ? "," : "", json_object_get_double(val));
}
printf("]\n");
printf("new_obj.to_string()=%s\n", json_object_to_json_string_ext(new_obj,JSON_C_TO_STRING_NOZERO));
printf("new_obj.to_string()=%s\n",
json_object_to_json_string_ext(new_obj, JSON_C_TO_STRING_NOZERO));
json_object_put(new_obj);
return 0;
}

View File

@@ -2,9 +2,9 @@
* Tests if binary strings are supported.
*/
#include "config.h"
#include <stdio.h>
#include <string.h>
#include "config.h"
#include "json_inttypes.h"
#include "json_object.h"
@@ -18,20 +18,22 @@ int main(void)
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 strings_match = !strcmp(expected, json);
int retval = 0;
if (strings_match)
{
printf("JSON write result is correct: %s\n", json);
puts("PASS");
} else {
}
else
{
puts("JSON write result doesn't match expected string");
printf("expected string: ");
puts(expected);
printf("parsed string: ");
puts(json);
puts("FAIL");
retval=1;
retval = 1;
}
json_object_put(string);
@@ -42,7 +44,7 @@ int main(void)
const char *parsed_cstr = json_object_get_string(parsed_str);
int ii;
printf("Re-parsed object string len=%d, chars=[", parsed_len);
for (ii = 0; ii < parsed_len ; ii++)
for (ii = 0; ii < parsed_len; ii++)
{
printf("%s%d", (ii ? ", " : ""), (int)parsed_cstr[ii]);
}

View File

@@ -1,8 +1,8 @@
#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <assert.h>
#include "json.h"
#include "json_tokener.h"
@@ -106,7 +106,7 @@ static void test_basic_parse()
single_basic_parse("{\"FoO\" : -12.3E512}", 0);
single_basic_parse("{\"FoO\" : -12.3e512}", 0);
single_basic_parse("{\"FoO\" : -12.3E51.2}", 0); /* non-sensical, returns null */
single_basic_parse("{\"FoO\" : -12.3E51.2}", 0); /* non-sensical, returns null */
single_basic_parse("{\"FoO\" : -12.3E512E12}", 0); /* non-sensical, returns null */
single_basic_parse("[\"\\n\"]", 0);
single_basic_parse("[\"\\nabc\\n\"]", 0);
@@ -119,7 +119,9 @@ static void test_basic_parse()
single_basic_parse("{ \'foo\': \'bar\' }", 0);
single_basic_parse("{ \"foo\": \"bar\", \"baz\": null, \"bool0\": true }", 0);
single_basic_parse("{ \"foo\": [null, \"foo\"] }", 0);
single_basic_parse("{ \"abc\": 12, \"foo\": \"bar\", \"bool0\": false, \"bool1\": true, \"arr\": [ 1, 2, 3, null, 5 ] }", 0);
single_basic_parse("{ \"abc\": 12, \"foo\": \"bar\", \"bool0\": false, \"bool1\": true, "
"\"arr\": [ 1, 2, 3, null, 5 ] }",
0);
single_basic_parse("{ \"abc\": \"blue\nred\\ngreen\" }", 0);
// Clear serializer for these tests so we see the actual parsed value.
@@ -130,7 +132,7 @@ static void test_basic_parse()
single_basic_parse("[0e+-1]", 1);
single_basic_parse("\"hello world!\"", 1);
// uint64/int64 range test
// uint64/int64 range test
single_basic_parse("[9223372036854775806]", 1);
single_basic_parse("[9223372036854775807]", 1);
single_basic_parse("[9223372036854775808]", 1);
@@ -147,8 +149,8 @@ static void test_utf8_parse()
// json_tokener_parse doesn't support checking for byte order marks.
// It's the responsibility of the caller to detect and skip a BOM.
// Both of these checks return null.
char utf8_bom[] = { 0xEF, 0xBB, 0xBF, 0x00 };
char utf8_bom_and_chars[] = { 0xEF, 0xBB, 0xBF, '{', '}', 0x00 };
char utf8_bom[] = {0xEF, 0xBB, 0xBF, 0x00};
char utf8_bom_and_chars[] = {0xEF, 0xBB, 0xBF, '{', '}', 0x00};
single_basic_parse(utf8_bom, 0);
single_basic_parse(utf8_bom_and_chars, 0);
}
@@ -161,10 +163,8 @@ static void do_clear_serializer(json_object *jso)
json_c_visit(jso, 0, clear_serializer, NULL);
}
static int clear_serializer(json_object *jso, int flags,
json_object *parent_jso,
const char *jso_key,
size_t *jso_index, void *userarg)
static int clear_serializer(json_object *jso, int flags, json_object *parent_jso,
const char *jso_key, size_t *jso_index, void *userarg)
{
if (jso)
json_object_set_serializer(jso, NULL, NULL, NULL);
@@ -177,24 +177,25 @@ static void test_verbose_parse()
enum json_tokener_error error = json_tokener_success;
new_obj = json_tokener_parse_verbose("{ foo }", &error);
assert (error == json_tokener_error_parse_object_key_name);
assert (new_obj == NULL);
assert(error == json_tokener_error_parse_object_key_name);
assert(new_obj == NULL);
new_obj = json_tokener_parse("{ foo }");
assert (new_obj == NULL);
assert(new_obj == NULL);
new_obj = json_tokener_parse("foo");
assert (new_obj == NULL);
assert(new_obj == NULL);
new_obj = json_tokener_parse_verbose("foo", &error);
assert (new_obj == NULL);
assert(new_obj == NULL);
/* b/c the string starts with 'f' parsing return a boolean error */
assert (error == json_tokener_error_parse_boolean);
assert(error == json_tokener_error_parse_boolean);
puts("json_tokener_parse_verbose() OK");
}
struct incremental_step {
struct incremental_step
{
const char *string_to_parse;
int length;
int char_offset;
@@ -202,205 +203,205 @@ struct incremental_step {
int reset_tokener;
} incremental_steps[] = {
/* Check that full json messages can be parsed, both w/ and w/o a reset */
{ "{ \"foo\": 123 }", -1, -1, json_tokener_success, 0 },
{ "{ \"foo\": 456 }", -1, -1, json_tokener_success, 1 },
{ "{ \"foo\": 789 }", -1, -1, json_tokener_success, 1 },
/* Check that full json messages can be parsed, both w/ and w/o a reset */
{"{ \"foo\": 123 }", -1, -1, json_tokener_success, 0},
{"{ \"foo\": 456 }", -1, -1, json_tokener_success, 1},
{"{ \"foo\": 789 }", -1, -1, json_tokener_success, 1},
/* Check the comment parse*/
{ "/* hello */{ \"foo\"", -1, -1, json_tokener_continue, 0 },
{ "/* hello */:/* hello */", -1, -1, json_tokener_continue, 0 },
{ "\"bar\"/* hello */", -1, -1, json_tokener_continue, 0 },
{ "}/* hello */", -1, -1, json_tokener_success, 1 },
{ "/ hello ", -1, 1, json_tokener_error_parse_comment, 1 },
{ "/* hello\"foo\"", -1, -1, json_tokener_continue, 1 },
{ "/* hello*\"foo\"", -1, -1, json_tokener_continue, 1 },
{ "// hello\"foo\"", -1, -1, json_tokener_continue, 1 },
/* Check the comment parse*/
{"/* hello */{ \"foo\"", -1, -1, json_tokener_continue, 0},
{"/* hello */:/* hello */", -1, -1, json_tokener_continue, 0},
{"\"bar\"/* hello */", -1, -1, json_tokener_continue, 0},
{"}/* hello */", -1, -1, json_tokener_success, 1},
{"/ hello ", -1, 1, json_tokener_error_parse_comment, 1},
{"/* hello\"foo\"", -1, -1, json_tokener_continue, 1},
{"/* hello*\"foo\"", -1, -1, json_tokener_continue, 1},
{"// hello\"foo\"", -1, -1, json_tokener_continue, 1},
/* Check a basic incremental parse */
{ "{ \"foo", -1, -1, json_tokener_continue, 0 },
{ "\": {\"bar", -1, -1, json_tokener_continue, 0 },
{ "\":13}}", -1, -1, json_tokener_success, 1 },
/* Check a basic incremental parse */
{"{ \"foo", -1, -1, json_tokener_continue, 0},
{"\": {\"bar", -1, -1, json_tokener_continue, 0},
{"\":13}}", -1, -1, json_tokener_success, 1},
/* Check that json_tokener_reset actually resets */
{ "{ \"foo", -1, -1, json_tokener_continue, 1 },
{ ": \"bar\"}", -1, 0, json_tokener_error_parse_unexpected, 1 },
/* Check that json_tokener_reset actually resets */
{"{ \"foo", -1, -1, json_tokener_continue, 1},
{": \"bar\"}", -1, 0, json_tokener_error_parse_unexpected, 1},
/* Check incremental parsing with trailing characters */
{ "{ \"foo", -1, -1, json_tokener_continue, 0 },
{ "\": {\"bar", -1, -1, json_tokener_continue, 0 },
{ "\":13}}XXXX", 10, 6, json_tokener_success, 0 },
{ "XXXX", 4, 0, json_tokener_error_parse_unexpected, 1 },
/* Check incremental parsing with trailing characters */
{"{ \"foo", -1, -1, json_tokener_continue, 0},
{"\": {\"bar", -1, -1, json_tokener_continue, 0},
{"\":13}}XXXX", 10, 6, json_tokener_success, 0},
{"XXXX", 4, 0, json_tokener_error_parse_unexpected, 1},
/* Check that trailing characters can change w/o a reset */
{ "{\"x\": 123 }\"X\"", -1, 11, json_tokener_success, 0 },
{ "\"Y\"", -1, -1, json_tokener_success, 1 },
/* Check that trailing characters can change w/o a reset */
{"{\"x\": 123 }\"X\"", -1, 11, json_tokener_success, 0},
{"\"Y\"", -1, -1, json_tokener_success, 1},
/* To stop parsing a number we need to reach a non-digit, e.g. a \0 */
{ "1", 1, 1, json_tokener_continue, 0 },
/* This should parse as the number 12, since it continues the "1" */
{ "2", 2, 1, json_tokener_success, 0 },
{ "12{", 3, 2, json_tokener_success, 1 },
/* Parse number in strict model */
{ "[02]", -1, 3, json_tokener_error_parse_number, 3 },
/* To stop parsing a number we need to reach a non-digit, e.g. a \0 */
{"1", 1, 1, json_tokener_continue, 0},
/* This should parse as the number 12, since it continues the "1" */
{"2", 2, 1, json_tokener_success, 0},
{"12{", 3, 2, json_tokener_success, 1},
/* Parse number in strict model */
{"[02]", -1, 3, json_tokener_error_parse_number, 3},
/* Similar tests for other kinds of objects: */
/* These could all return success immediately, since regardless of
/* Similar tests for other kinds of objects: */
/* These could all return success immediately, since regardless of
what follows the false/true/null token we *will* return a json object,
but it currently doesn't work that way. hmm... */
{ "false", 5, 5, json_tokener_continue, 1 },
{ "false", 6, 5, json_tokener_success, 1 },
{ "true", 4, 4, json_tokener_continue, 1 },
{ "true", 5, 4, json_tokener_success, 1 },
{ "null", 4, 4, json_tokener_continue, 1 },
{ "null", 5, 4, json_tokener_success, 1 },
{"false", 5, 5, json_tokener_continue, 1},
{"false", 6, 5, json_tokener_success, 1},
{"true", 4, 4, json_tokener_continue, 1},
{"true", 5, 4, json_tokener_success, 1},
{"null", 4, 4, json_tokener_continue, 1},
{"null", 5, 4, json_tokener_success, 1},
{ "Infinity", 9, 8, json_tokener_success, 1 },
{ "infinity", 9, 8, json_tokener_success, 1 },
{ "-infinity", 10, 9, json_tokener_success, 1 },
{ "infinity", 9, 0, json_tokener_error_parse_unexpected, 3 },
{ "-infinity", 10, 1, json_tokener_error_parse_unexpected, 3 },
{"Infinity", 9, 8, json_tokener_success, 1},
{"infinity", 9, 8, json_tokener_success, 1},
{"-infinity", 10, 9, json_tokener_success, 1},
{"infinity", 9, 0, json_tokener_error_parse_unexpected, 3},
{"-infinity", 10, 1, json_tokener_error_parse_unexpected, 3},
{ "inf", 3, 3, json_tokener_continue, 0 },
{ "inity", 6, 5, json_tokener_success, 1 },
{ "-inf", 4, 4, json_tokener_continue, 0 },
{ "inity", 6, 5, json_tokener_success, 1 },
{"inf", 3, 3, json_tokener_continue, 0},
{"inity", 6, 5, json_tokener_success, 1},
{"-inf", 4, 4, json_tokener_continue, 0},
{"inity", 6, 5, json_tokener_success, 1},
{ "i", 1, 1, json_tokener_continue, 0 },
{ "n", 1, 1, json_tokener_continue, 0 },
{ "f", 1, 1, json_tokener_continue, 0 },
{ "i", 1, 1, json_tokener_continue, 0 },
{ "n", 1, 1, json_tokener_continue, 0 },
{ "i", 1, 1, json_tokener_continue, 0 },
{ "t", 1, 1, json_tokener_continue, 0 },
{ "y", 1, 1, json_tokener_continue, 0 },
{ "", 1, 0, json_tokener_success, 1 },
{"i", 1, 1, json_tokener_continue, 0},
{"n", 1, 1, json_tokener_continue, 0},
{"f", 1, 1, json_tokener_continue, 0},
{"i", 1, 1, json_tokener_continue, 0},
{"n", 1, 1, json_tokener_continue, 0},
{"i", 1, 1, json_tokener_continue, 0},
{"t", 1, 1, json_tokener_continue, 0},
{"y", 1, 1, json_tokener_continue, 0},
{"", 1, 0, json_tokener_success, 1},
{ "-", 1, 1, json_tokener_continue, 0 },
{ "inf", 3, 3, json_tokener_continue, 0 },
{ "ini", 3, 3, json_tokener_continue, 0 },
{ "ty", 3, 2, json_tokener_success, 1 },
{"-", 1, 1, json_tokener_continue, 0},
{"inf", 3, 3, json_tokener_continue, 0},
{"ini", 3, 3, json_tokener_continue, 0},
{"ty", 3, 2, json_tokener_success, 1},
{ "-", 1, 1, json_tokener_continue, 0 },
{ "i", 1, 1, json_tokener_continue, 0 },
{ "nfini", 5, 5, json_tokener_continue, 0 },
{ "ty", 3, 2, json_tokener_success, 1 },
{"-", 1, 1, json_tokener_continue, 0},
{"i", 1, 1, json_tokener_continue, 0},
{"nfini", 5, 5, json_tokener_continue, 0},
{"ty", 3, 2, json_tokener_success, 1},
{ "-i", 2, 2, json_tokener_continue, 0 },
{ "nfinity", 8, 7, json_tokener_success, 1 },
{"-i", 2, 2, json_tokener_continue, 0},
{"nfinity", 8, 7, json_tokener_success, 1},
{ "InfinityX", 10, 8, json_tokener_success, 0 },
{ "X", 1, 0, json_tokener_error_parse_unexpected, 1 },
{"InfinityX", 10, 8, json_tokener_success, 0},
{"X", 1, 0, json_tokener_error_parse_unexpected, 1},
{ "Infinity1234", 13, 8, json_tokener_success, 0 },
{ "1234", 5, 4, json_tokener_success, 1 },
{"Infinity1234", 13, 8, json_tokener_success, 0},
{"1234", 5, 4, json_tokener_success, 1},
{ "Infinity9999", 8, 8, json_tokener_continue, 0 },
{"Infinity9999", 8, 8, json_tokener_continue, 0},
/* returns the Infinity loaded up by the previous call: */
{ "1234", 5, 0, json_tokener_success, 0 },
{ "1234", 5, 4, json_tokener_success, 1 },
/* returns the Infinity loaded up by the previous call: */
{"1234", 5, 0, json_tokener_success, 0},
{"1234", 5, 4, json_tokener_success, 1},
/* offset=1 because "n" is the start of "null". hmm... */
{ "noodle", 7, 1, json_tokener_error_parse_null, 1 },
/* offset=2 because "na" is the start of "nan". hmm... */
{ "naodle", 7, 2, json_tokener_error_parse_null, 1 },
/* offset=2 because "tr" is the start of "true". hmm... */
{ "track", 6, 2, json_tokener_error_parse_boolean, 1 },
{ "fail", 5, 2, json_tokener_error_parse_boolean, 1 },
/* offset=1 because "n" is the start of "null". hmm... */
{"noodle", 7, 1, json_tokener_error_parse_null, 1},
/* offset=2 because "na" is the start of "nan". hmm... */
{"naodle", 7, 2, json_tokener_error_parse_null, 1},
/* offset=2 because "tr" is the start of "true". hmm... */
{"track", 6, 2, json_tokener_error_parse_boolean, 1},
{"fail", 5, 2, json_tokener_error_parse_boolean, 1},
/* Although they may initially look like they should fail,
/* Although they may initially look like they should fail,
* the next few tests check that parsing multiple sequential
* json objects in the input works as expected
*/
{ "null123", 9, 4, json_tokener_success, 0 },
{ &"null123"[4], 4, 3, json_tokener_success, 1 },
{ "nullx", 5, 4, json_tokener_success, 0 },
{ &"nullx"[4], 2, 0, json_tokener_error_parse_unexpected, 1 },
{ "{\"a\":1}{\"b\":2}",15, 7, json_tokener_success, 0 },
{ &"{\"a\":1}{\"b\":2}"[7],
8, 7, json_tokener_success, 1 },
{"null123", 9, 4, json_tokener_success, 0},
{&"null123"[4], 4, 3, json_tokener_success, 1},
{"nullx", 5, 4, json_tokener_success, 0},
{&"nullx"[4], 2, 0, json_tokener_error_parse_unexpected, 1},
{"{\"a\":1}{\"b\":2}", 15, 7, json_tokener_success, 0},
{&"{\"a\":1}{\"b\":2}"[7], 8, 7, json_tokener_success, 1},
/* Some bad formatting. Check we get the correct error status */
{ "2015-01-15", 10, 4, json_tokener_error_parse_number, 1 },
/* Some bad formatting. Check we get the correct error status */
{"2015-01-15", 10, 4, json_tokener_error_parse_number, 1},
/* Strings have a well defined end point, so we can stop at the quote */
{ "\"blue\"", -1, -1, json_tokener_success, 0 },
/* Strings have a well defined end point, so we can stop at the quote */
{"\"blue\"", -1, -1, json_tokener_success, 0},
/* Check each of the escape sequences defined by the spec */
{ "\"\\\"\"", -1, -1, json_tokener_success, 0 },
{ "\"\\\\\"", -1, -1, json_tokener_success, 0 },
{ "\"\\b\"", -1, -1, json_tokener_success, 0 },
{ "\"\\f\"", -1, -1, json_tokener_success, 0 },
{ "\"\\n\"", -1, -1, json_tokener_success, 0 },
{ "\"\\r\"", -1, -1, json_tokener_success, 0 },
{ "\"\\t\"", -1, -1, json_tokener_success, 0 },
{ "\"\\/\"", -1, -1, json_tokener_success, 0 },
// Escaping a forward slash is optional
{ "\"/\"", -1, -1, json_tokener_success, 0 },
/* Check wrong escape sequences */
{ "\"\\a\"", -1, 2, json_tokener_error_parse_string, 1 },
/* Check each of the escape sequences defined by the spec */
{"\"\\\"\"", -1, -1, json_tokener_success, 0},
{"\"\\\\\"", -1, -1, json_tokener_success, 0},
{"\"\\b\"", -1, -1, json_tokener_success, 0},
{"\"\\f\"", -1, -1, json_tokener_success, 0},
{"\"\\n\"", -1, -1, json_tokener_success, 0},
{"\"\\r\"", -1, -1, json_tokener_success, 0},
{"\"\\t\"", -1, -1, json_tokener_success, 0},
{"\"\\/\"", -1, -1, json_tokener_success, 0},
// Escaping a forward slash is optional
{"\"/\"", -1, -1, json_tokener_success, 0},
/* Check wrong escape sequences */
{"\"\\a\"", -1, 2, json_tokener_error_parse_string, 1},
/* Check '\'' in strict model */
{ "\'foo\'", -1, 0, json_tokener_error_parse_unexpected, 3 },
/* Check '\'' in strict model */
{"\'foo\'", -1, 0, json_tokener_error_parse_unexpected, 3},
/* Parse array/object */
{ "[1,2,3]", -1, -1, json_tokener_success, 0 },
{ "[1,2,3}", -1, 6, json_tokener_error_parse_array, 1 },
{ "{\"a\"}", -1, 4, json_tokener_error_parse_object_key_sep, 1 },
{ "{\"a\":1]", -1, 6, json_tokener_error_parse_object_value_sep, 1 },
{ "{\"a\"::1}", -1, 5, json_tokener_error_parse_unexpected, 1 },
{ "{\"a\":}", -1, 5, json_tokener_error_parse_unexpected, 1 },
{ "{\"a\":1,\"a\":2}",-1, -1, json_tokener_success, 1 },
{ "\"a\":1}", -1, 3, json_tokener_success, 1 },
{ "{\"a\":1", -1, -1, json_tokener_continue, 1 },
{ "[,]", -1, 1, json_tokener_error_parse_unexpected, 1 },
{ "[,1]", -1, 1, json_tokener_error_parse_unexpected, 1 },
/* Parse array/object */
{"[1,2,3]", -1, -1, json_tokener_success, 0},
{"[1,2,3}", -1, 6, json_tokener_error_parse_array, 1},
{"{\"a\"}", -1, 4, json_tokener_error_parse_object_key_sep, 1},
{"{\"a\":1]", -1, 6, json_tokener_error_parse_object_value_sep, 1},
{"{\"a\"::1}", -1, 5, json_tokener_error_parse_unexpected, 1},
{"{\"a\":}", -1, 5, json_tokener_error_parse_unexpected, 1},
{"{\"a\":1,\"a\":2}", -1, -1, json_tokener_success, 1},
{"\"a\":1}", -1, 3, json_tokener_success, 1},
{"{\"a\":1", -1, -1, json_tokener_continue, 1},
{"[,]", -1, 1, json_tokener_error_parse_unexpected, 1},
{"[,1]", -1, 1, json_tokener_error_parse_unexpected, 1},
/* This behaviour doesn't entirely follow the json spec, but until we have
/* This behaviour doesn't entirely follow the json spec, but until we have
* a way to specify how strict to be we follow Postel's Law and be liberal
* in what we accept (up to a point).
*/
{ "[1,2,3,]", -1, -1, json_tokener_success, 0 },
{ "[1,2,,3,]", -1, 5, json_tokener_error_parse_unexpected, 0 },
{"[1,2,3,]", -1, -1, json_tokener_success, 0},
{"[1,2,,3,]", -1, 5, json_tokener_error_parse_unexpected, 0},
{ "[1,2,3,]", -1, 7, json_tokener_error_parse_unexpected, 3 },
{ "{\"a\":1,}", -1, 7, json_tokener_error_parse_unexpected, 3 },
{"[1,2,3,]", -1, 7, json_tokener_error_parse_unexpected, 3},
{"{\"a\":1,}", -1, 7, json_tokener_error_parse_unexpected, 3},
// utf-8 test
// acsll encoding
{ "\x22\x31\x32\x33\x61\x73\x63\x24\x25\x26\x22",-1, -1, json_tokener_success, 5 },
{ "\x22\x31\x32\x33\x61\x73\x63\x24\x25\x26\x22",-1, -1, json_tokener_success, 1 },
// utf-8 encoding
{ "\x22\xe4\xb8\x96\xe7\x95\x8c\x22",-1, -1, json_tokener_success, 5 },
{ "\x22\xe4\xb8",-1, 3, json_tokener_error_parse_utf8_string, 4 },
{ "\x96\xe7\x95\x8c\x22",-1, 0, json_tokener_error_parse_utf8_string, 5 },
{ "\x22\xe4\xb8\x96\xe7\x95\x8c\x22",-1, -1, json_tokener_success, 1 },
{ "\x22\xcf\x80\xcf\x86\x22",-1, -1, json_tokener_success, 5 },
{ "\x22\xf0\xa5\x91\x95\x22",-1, -1, json_tokener_success, 5 },
// wrong utf-8 encoding
{ "\x22\xe6\x9d\x4e\x22",-1, 3, json_tokener_error_parse_utf8_string, 5 },
{ "\x22\xe6\x9d\x4e\x22",-1, 5, json_tokener_success, 1 },
// GBK encoding
{ "\x22\xc0\xee\xc5\xf4\x22",-1, 2, json_tokener_error_parse_utf8_string, 5 },
{ "\x22\xc0\xee\xc5\xf4\x22",-1, 6, json_tokener_success, 1 },
// char after space
{ "\x20\x20\x22\xe4\xb8\x96\x22",-1, -1, json_tokener_success, 5 },
{ "\x20\x20\x81\x22\xe4\xb8\x96\x22",-1, 2, json_tokener_error_parse_utf8_string, 5 },
{ "\x5b\x20\x81\x31\x5d",-1, 2, json_tokener_error_parse_utf8_string, 5 },
// char in state inf
{ "\x49\x6e\x66\x69\x6e\x69\x74\x79",9, 8, json_tokener_success, 1 },
{ "\x49\x6e\x66\x81\x6e\x69\x74\x79",-1, 3, json_tokener_error_parse_utf8_string, 5 },
// char in escape unicode
{ "\x22\x5c\x75\x64\x38\x35\x35\x5c\x75\x64\x63\x35\x35\x22",15, 14, json_tokener_success, 5 },
{ "\x22\x5c\x75\x64\x38\x35\x35\xc0\x75\x64\x63\x35\x35\x22",-1, 8, json_tokener_error_parse_utf8_string, 5 },
{ "\x22\x5c\x75\x64\x30\x30\x33\x31\xc0\x22",-1, 9, json_tokener_error_parse_utf8_string, 5 },
// char in number
{ "\x31\x31\x81\x31\x31",-1, 2, json_tokener_error_parse_utf8_string, 5 },
// char in object
{ "\x7b\x22\x31\x81\x22\x3a\x31\x7d",-1, 3, json_tokener_error_parse_utf8_string, 5 },
// utf-8 test
// acsll encoding
{"\x22\x31\x32\x33\x61\x73\x63\x24\x25\x26\x22", -1, -1, json_tokener_success, 5},
{"\x22\x31\x32\x33\x61\x73\x63\x24\x25\x26\x22", -1, -1, json_tokener_success, 1},
// utf-8 encoding
{"\x22\xe4\xb8\x96\xe7\x95\x8c\x22", -1, -1, json_tokener_success, 5},
{"\x22\xe4\xb8", -1, 3, json_tokener_error_parse_utf8_string, 4},
{"\x96\xe7\x95\x8c\x22", -1, 0, json_tokener_error_parse_utf8_string, 5},
{"\x22\xe4\xb8\x96\xe7\x95\x8c\x22", -1, -1, json_tokener_success, 1},
{"\x22\xcf\x80\xcf\x86\x22", -1, -1, json_tokener_success, 5},
{"\x22\xf0\xa5\x91\x95\x22", -1, -1, json_tokener_success, 5},
// wrong utf-8 encoding
{"\x22\xe6\x9d\x4e\x22", -1, 3, json_tokener_error_parse_utf8_string, 5},
{"\x22\xe6\x9d\x4e\x22", -1, 5, json_tokener_success, 1},
// GBK encoding
{"\x22\xc0\xee\xc5\xf4\x22", -1, 2, json_tokener_error_parse_utf8_string, 5},
{"\x22\xc0\xee\xc5\xf4\x22", -1, 6, json_tokener_success, 1},
// char after space
{"\x20\x20\x22\xe4\xb8\x96\x22", -1, -1, json_tokener_success, 5},
{"\x20\x20\x81\x22\xe4\xb8\x96\x22", -1, 2, json_tokener_error_parse_utf8_string, 5},
{"\x5b\x20\x81\x31\x5d", -1, 2, json_tokener_error_parse_utf8_string, 5},
// char in state inf
{"\x49\x6e\x66\x69\x6e\x69\x74\x79", 9, 8, json_tokener_success, 1},
{"\x49\x6e\x66\x81\x6e\x69\x74\x79", -1, 3, json_tokener_error_parse_utf8_string, 5},
// char in escape unicode
{"\x22\x5c\x75\x64\x38\x35\x35\x5c\x75\x64\x63\x35\x35\x22", 15, 14, json_tokener_success, 5},
{"\x22\x5c\x75\x64\x38\x35\x35\xc0\x75\x64\x63\x35\x35\x22", -1, 8,
json_tokener_error_parse_utf8_string, 5},
{"\x22\x5c\x75\x64\x30\x30\x33\x31\xc0\x22", -1, 9, json_tokener_error_parse_utf8_string, 5},
// char in number
{"\x31\x31\x81\x31\x31", -1, 2, json_tokener_error_parse_utf8_string, 5},
// char in object
{"\x7b\x22\x31\x81\x22\x3a\x31\x7d", -1, 3, json_tokener_error_parse_utf8_string, 5},
{ NULL, -1, -1, json_tokener_success, 0 },
{NULL, -1, -1, json_tokener_success, 0},
};
static void test_incremental_parse()
@@ -422,7 +423,8 @@ static void test_incremental_parse()
string_to_parse = "{ \"foo"; /* } */
printf("json_tokener_parse(%s) ... ", string_to_parse);
new_obj = json_tokener_parse(string_to_parse);
if (new_obj == NULL) puts("got error as expected");
if (new_obj == NULL)
puts("got error as expected");
/* test incremental parsing in various forms */
tok = json_tokener_new();
@@ -434,19 +436,19 @@ static void test_incremental_parse()
size_t expected_char_offset;
if (step->reset_tokener & 2)
{
if (step->reset_tokener & 4)
json_tokener_set_flags(tok, 3);
else
json_tokener_set_flags(tok, JSON_TOKENER_STRICT);
}
{
if (step->reset_tokener & 4)
json_tokener_set_flags(tok, 3);
else
json_tokener_set_flags(tok, JSON_TOKENER_STRICT);
}
else
{
if (step->reset_tokener & 4)
json_tokener_set_flags(tok, JSON_TOKENER_VALIDATE_UTF8);
else
json_tokener_set_flags(tok, 0);
}
{
if (step->reset_tokener & 4)
json_tokener_set_flags(tok, JSON_TOKENER_VALIDATE_UTF8);
else
json_tokener_set_flags(tok, 0);
}
if (length == -1)
length = strlen(step->string_to_parse);
@@ -455,8 +457,8 @@ static void test_incremental_parse()
else
expected_char_offset = step->char_offset;
printf("json_tokener_parse_ex(tok, %-12s, %3d) ... ",
step->string_to_parse, length);
printf("json_tokener_parse_ex(tok, %-12s, %3d) ... ", step->string_to_parse,
length);
new_obj = json_tokener_parse_ex(tok, step->string_to_parse, length);
jerr = json_tokener_get_error(tok);
@@ -470,8 +472,7 @@ static void test_incremental_parse()
json_tokener_error_desc(jerr));
else if (json_tokener_get_parse_end(tok) != expected_char_offset)
printf("ERROR: wrong char_offset %zu != expected %zu\n",
json_tokener_get_parse_end(tok),
expected_char_offset);
json_tokener_get_parse_end(tok), expected_char_offset);
else
{
printf("OK: got correct error: %s\n",
@@ -482,19 +483,17 @@ static void test_incremental_parse()
else
{
if (new_obj == NULL &&
!(step->length >= 4 &&
strncmp(step->string_to_parse, "null", 4) == 0))
!(step->length >= 4 && strncmp(step->string_to_parse, "null", 4) == 0))
printf("ERROR: expected valid object, instead: %s\n",
json_tokener_error_desc(jerr));
else if (json_tokener_get_parse_end(tok) != expected_char_offset)
printf("ERROR: wrong char_offset %zu != expected %zu\n",
json_tokener_get_parse_end(tok),
expected_char_offset);
json_tokener_get_parse_end(tok), expected_char_offset);
else
{
printf("OK: got object of type [%s]: %s\n",
json_type_to_name(json_object_get_type(new_obj)),
json_object_to_json_string(new_obj));
json_type_to_name(json_object_get_type(new_obj)),
json_object_to_json_string(new_obj));
this_step_ok = 1;
}
}

View File

@@ -35,7 +35,7 @@ int main()
{
char buf[100];
printf("==========json_parse_int64() test===========\n");
printf("==========json_parse_int64() test===========\n");
checkit("x");
checkit("0");
@@ -70,7 +70,7 @@ int main()
strcpy(buf, "4294967295"); // aka UINT32_MAX
checkit(buf);
strcpy(buf, "4294967296"); // aka UINT32_MAX + 1
strcpy(buf, "4294967296"); // aka UINT32_MAX + 1
checkit(buf);
strcpy(buf, "21474836470"); // INT32_MAX * 10
@@ -122,7 +122,7 @@ int main()
strcpy(buf, "123");
checkit(buf);
printf("\n==========json_parse_uint64() test===========\n");
printf("\n==========json_parse_uint64() test===========\n");
checkit_uint("x");
checkit_uint("0");
@@ -154,7 +154,7 @@ int main()
strcpy(buf, "4294967295"); // aka UINT32_MAX
checkit_uint(buf);
strcpy(buf, "4294967296"); // aka UINT32_MAX + 1
strcpy(buf, "4294967296"); // aka UINT32_MAX + 1
checkit_uint(buf);
strcpy(buf, "21474836470"); // INT32_MAX * 10

View File

@@ -1,9 +1,9 @@
#include <assert.h>
#include <limits.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include "debug.h"
#include "printbuf.h"
@@ -78,7 +78,7 @@ static void test_printbuf_memappend(int *before_resize)
initial_size = pb->size;
while(pb->size == initial_size)
while (pb->size == initial_size)
{
printbuf_memappend_fast(pb, "x", 1);
}
@@ -89,7 +89,7 @@ static void test_printbuf_memappend(int *before_resize)
printbuf_memappend_fast(pb, "bluexyz123", 3);
printf("Partial append: %d, [%s]\n", printbuf_length(pb), pb->buf);
char with_nulls[] = { 'a', 'b', '\0', 'c' };
char with_nulls[] = {'a', 'b', '\0', 'c'};
printbuf_reset(pb);
printbuf_memappend_fast(pb, with_nulls, (int)sizeof(with_nulls));
printf("With embedded \\0 character: %d, [%s]\n", printbuf_length(pb), pb->buf);
@@ -118,7 +118,7 @@ static void test_printbuf_memappend(int *before_resize)
printbuf_strappend(pb, SA_TEST_STR);
printf("Buffer size after printbuf_strappend(): %d, [%s]\n", printbuf_length(pb), pb->buf);
printbuf_free(pb);
#undef SA_TEST_STR
#undef SA_TEST_STR
printf("%s: end test\n", __func__);
}
@@ -127,10 +127,11 @@ static void test_sprintbuf(int before_resize);
static void test_sprintbuf(int before_resize)
{
struct printbuf *pb;
const char *max_char = "if string is greater than stack buffer, then use dynamic string"
" with vasprintf. Note: some implementation of vsnprintf return -1 "
" if output is truncated whereas some return the number of bytes that "
" would have been written - this code handles both cases.";
const char *max_char =
"if string is greater than stack buffer, then use dynamic string"
" with vasprintf. Note: some implementation of vsnprintf return -1 "
" if output is truncated whereas some return the number of bytes that "
" would have been written - this code handles both cases.";
printf("%s: starting test\n", __func__);
pb = printbuf_new();
@@ -141,7 +142,8 @@ static void test_sprintbuf(int before_resize)
data[before_resize + 1] = '\0';
sprintbuf(pb, "%s", data);
free(data);
printf("sprintbuf to just after resize(%d+1): %d, [%s], strlen(buf)=%d\n", before_resize, printbuf_length(pb), pb->buf, (int)strlen(pb->buf));
printf("sprintbuf to just after resize(%d+1): %d, [%s], strlen(buf)=%d\n", before_resize,
printbuf_length(pb), pb->buf, (int)strlen(pb->buf));
printbuf_reset(pb);
sprintbuf(pb, "plain");

View File

@@ -5,7 +5,8 @@
#include "json.h"
#include "printbuf.h"
struct myinfo {
struct myinfo
{
int value;
};
@@ -17,10 +18,7 @@ static void freeit(json_object *jso, void *userdata)
// 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)
static int custom_serializer(struct json_object *o, struct printbuf *pb, int level, int flags)
{
sprintbuf(pb, "Custom Output");
return 0;
@@ -39,10 +37,11 @@ int main(int argc, char **argv)
printf("my_object.to_string(standard)=%s\n", json_object_to_json_string(my_object));
struct myinfo userdata = { .value = 123 };
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("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;
@@ -60,7 +59,8 @@ int main(int argc, char **argv)
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("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;

View File

@@ -6,85 +6,85 @@
int main(int argc, char **argv)
{
json_object *tmp=json_object_new_int(123);
assert (json_object_get_int(tmp)==123);
json_object_set_int(tmp,321);
assert (json_object_get_int(tmp)==321);
json_object *tmp = json_object_new_int(123);
assert(json_object_get_int(tmp) == 123);
json_object_set_int(tmp, 321);
assert(json_object_get_int(tmp) == 321);
printf("INT PASSED\n");
json_object_set_int64(tmp,(int64_t)321321321);
assert (json_object_get_int64(tmp)==321321321);
json_object_set_int64(tmp, (int64_t)321321321);
assert(json_object_get_int64(tmp) == 321321321);
json_object_put(tmp);
printf("INT64 PASSED\n");
tmp=json_object_new_uint64(123);
assert (json_object_get_boolean(tmp)==1);
assert (json_object_get_int(tmp)==123);
assert (json_object_get_int64(tmp)==123);
assert (json_object_get_uint64(tmp)==123);
assert (json_object_get_double(tmp)==123.000000);
json_object_set_uint64(tmp,(uint64_t)321321321);
assert (json_object_get_uint64(tmp)==321321321);
json_object_set_uint64(tmp,9223372036854775808U);
assert (json_object_get_int(tmp)==INT32_MAX);
assert (json_object_get_uint64(tmp)==9223372036854775808U);
tmp = json_object_new_uint64(123);
assert(json_object_get_boolean(tmp) == 1);
assert(json_object_get_int(tmp) == 123);
assert(json_object_get_int64(tmp) == 123);
assert(json_object_get_uint64(tmp) == 123);
assert(json_object_get_double(tmp) == 123.000000);
json_object_set_uint64(tmp, (uint64_t)321321321);
assert(json_object_get_uint64(tmp) == 321321321);
json_object_set_uint64(tmp, 9223372036854775808U);
assert(json_object_get_int(tmp) == INT32_MAX);
assert(json_object_get_uint64(tmp) == 9223372036854775808U);
json_object_put(tmp);
printf("UINT64 PASSED\n");
tmp=json_object_new_boolean(1);
assert (json_object_get_boolean(tmp)==1);
json_object_set_boolean(tmp,0);
assert (json_object_get_boolean(tmp)==0);
json_object_set_boolean(tmp,1);
assert (json_object_get_boolean(tmp)==1);
tmp = json_object_new_boolean(1);
assert(json_object_get_boolean(tmp) == 1);
json_object_set_boolean(tmp, 0);
assert(json_object_get_boolean(tmp) == 0);
json_object_set_boolean(tmp, 1);
assert(json_object_get_boolean(tmp) == 1);
json_object_put(tmp);
printf("BOOL PASSED\n");
tmp=json_object_new_double(12.34);
assert (json_object_get_double(tmp)==12.34);
json_object_set_double(tmp,34.56);
assert (json_object_get_double(tmp)==34.56);
json_object_set_double(tmp,6435.34);
assert (json_object_get_double(tmp)==6435.34);
json_object_set_double(tmp,2e21);
assert (json_object_get_int(tmp)==INT32_MAX);
assert (json_object_get_int64(tmp)==INT64_MAX);
assert (json_object_get_uint64(tmp)==UINT64_MAX);
json_object_set_double(tmp,-2e21);
assert (json_object_get_int(tmp)==INT32_MIN);
assert (json_object_get_int64(tmp)==INT64_MIN);
assert (json_object_get_uint64(tmp)==0);
tmp = json_object_new_double(12.34);
assert(json_object_get_double(tmp) == 12.34);
json_object_set_double(tmp, 34.56);
assert(json_object_get_double(tmp) == 34.56);
json_object_set_double(tmp, 6435.34);
assert(json_object_get_double(tmp) == 6435.34);
json_object_set_double(tmp, 2e21);
assert(json_object_get_int(tmp) == INT32_MAX);
assert(json_object_get_int64(tmp) == INT64_MAX);
assert(json_object_get_uint64(tmp) == UINT64_MAX);
json_object_set_double(tmp, -2e21);
assert(json_object_get_int(tmp) == INT32_MIN);
assert(json_object_get_int64(tmp) == INT64_MIN);
assert(json_object_get_uint64(tmp) == 0);
json_object_put(tmp);
printf("DOUBLE PASSED\n");
#define SHORT "SHORT"
#define MID "A MID STRING"
// 12345678901234567890123456789012....
#define HUGE "A string longer than 32 chars as to check non local buf codepath"
tmp=json_object_new_string(SHORT);
assert (strcmp(json_object_get_string(tmp),SHORT)==0);
json_object_set_string(tmp,MID);
assert (strcmp(json_object_get_string(tmp),MID)==0);
json_object_set_string(tmp,HUGE);
assert (strcmp(json_object_get_string(tmp),HUGE)==0);
json_object_set_string(tmp,SHORT);
assert (strcmp(json_object_get_string(tmp),SHORT)==0);
#define SHORT "SHORT"
#define MID "A MID STRING"
// 12345678901234567890123456789012....
#define HUGE "A string longer than 32 chars as to check non local buf codepath"
tmp = json_object_new_string(SHORT);
assert(strcmp(json_object_get_string(tmp), SHORT) == 0);
json_object_set_string(tmp, MID);
assert(strcmp(json_object_get_string(tmp), MID) == 0);
json_object_set_string(tmp, HUGE);
assert(strcmp(json_object_get_string(tmp), HUGE) == 0);
json_object_set_string(tmp, SHORT);
assert(strcmp(json_object_get_string(tmp), SHORT) == 0);
json_object_put(tmp);
printf("STRING PASSED\n");
#define STR "STR"
#define DOUBLE "123.123"
#define DOUBLE_E "12E+3"
#define DOUBLE_STR "123.123STR"
#define DOUBLE_OVER "1.8E+308"
#define DOUBLE_OVER_NEGATIVE "-1.8E+308"
tmp=json_object_new_string(STR);
assert (json_object_get_double(tmp)==0.0);
json_object_set_string(tmp,DOUBLE);
assert (json_object_get_double(tmp)==123.123000);
json_object_set_string(tmp,DOUBLE_E);
assert (json_object_get_double(tmp)==12000.000000);
json_object_set_string(tmp,DOUBLE_STR);
assert (json_object_get_double(tmp)==0.0);
json_object_set_string(tmp,DOUBLE_OVER);
assert (json_object_get_double(tmp)==0.0);
json_object_set_string(tmp,DOUBLE_OVER_NEGATIVE);
assert (json_object_get_double(tmp)==0.0);
#define STR "STR"
#define DOUBLE "123.123"
#define DOUBLE_E "12E+3"
#define DOUBLE_STR "123.123STR"
#define DOUBLE_OVER "1.8E+308"
#define DOUBLE_OVER_NEGATIVE "-1.8E+308"
tmp = json_object_new_string(STR);
assert(json_object_get_double(tmp) == 0.0);
json_object_set_string(tmp, DOUBLE);
assert(json_object_get_double(tmp) == 123.123000);
json_object_set_string(tmp, DOUBLE_E);
assert(json_object_get_double(tmp) == 12000.000000);
json_object_set_string(tmp, DOUBLE_STR);
assert(json_object_get_double(tmp) == 0.0);
json_object_set_string(tmp, DOUBLE_OVER);
assert(json_object_get_double(tmp) == 0.0);
json_object_set_string(tmp, DOUBLE_OVER_NEGATIVE);
assert(json_object_get_double(tmp) == 0.0);
json_object_put(tmp);
printf("STRINGTODOUBLE PASSED\n");
@@ -92,7 +92,7 @@ int main(int argc, char **argv)
json_object_set_double(tmp, 12.3);
const char *serialized = json_object_to_json_string(tmp);
fprintf(stderr, "%s\n", serialized);
assert(strncmp(serialized, "12.3", 4)==0);
assert(strncmp(serialized, "12.3", 4) == 0);
json_object_put(tmp);
printf("PARSE AND SET PASSED\n");

View File

@@ -2,20 +2,20 @@
#include "strerror_override_private.h"
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <io.h>
#endif /* defined(WIN32) */
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <windows.h>
#endif /* defined(WIN32) */
#include <fcntl.h>
#include <limits.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if HAVE_UNISTD_H
# include <unistd.h>
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "json.h"
#include "json_util.h"
@@ -38,21 +38,20 @@ static void test_write_to_file()
json_object *jso;
jso = json_tokener_parse("{"
"\"foo\":1234,"
"\"foo1\":\"abcdefghijklmnopqrstuvwxyz\","
"\"foo2\":\"abcdefghijklmnopqrstuvwxyz\","
"\"foo3\":\"abcdefghijklmnopqrstuvwxyz\","
"\"foo4\":\"abcdefghijklmnopqrstuvwxyz\","
"\"foo5\":\"abcdefghijklmnopqrstuvwxyz\","
"\"foo6\":\"abcdefghijklmnopqrstuvwxyz\","
"\"foo7\":\"abcdefghijklmnopqrstuvwxyz\","
"\"foo8\":\"abcdefghijklmnopqrstuvwxyz\","
"\"foo9\":\"abcdefghijklmnopqrstuvwxyz\""
"}");
"\"foo\":1234,"
"\"foo1\":\"abcdefghijklmnopqrstuvwxyz\","
"\"foo2\":\"abcdefghijklmnopqrstuvwxyz\","
"\"foo3\":\"abcdefghijklmnopqrstuvwxyz\","
"\"foo4\":\"abcdefghijklmnopqrstuvwxyz\","
"\"foo5\":\"abcdefghijklmnopqrstuvwxyz\","
"\"foo6\":\"abcdefghijklmnopqrstuvwxyz\","
"\"foo7\":\"abcdefghijklmnopqrstuvwxyz\","
"\"foo8\":\"abcdefghijklmnopqrstuvwxyz\","
"\"foo9\":\"abcdefghijklmnopqrstuvwxyz\""
"}");
const char *outfile = "json.out";
int rv = json_object_to_file(outfile, jso);
printf("%s: json_object_to_file(%s, jso)=%d\n",
(rv == 0) ? "OK" : "FAIL", outfile, rv);
printf("%s: json_object_to_file(%s, jso)=%d\n", (rv == 0) ? "OK" : "FAIL", outfile, rv);
if (rv == 0)
stat_and_cat(outfile);
@@ -66,7 +65,7 @@ static void test_write_to_file()
stat_and_cat(outfile2);
const char *outfile3 = "json3.out";
int d = open(outfile3, O_WRONLY|O_CREAT, 0600);
int d = open(outfile3, O_WRONLY | O_CREAT, 0600);
if (d < 0)
{
printf("FAIL: unable to open %s %s\n", outfile3, strerror(errno));
@@ -92,19 +91,17 @@ static void stat_and_cat(const char *file)
int d = open(file, O_RDONLY, 0600);
if (d < 0)
{
printf("FAIL: unable to open %s: %s\n",
file, strerror(errno));
printf("FAIL: unable to open %s: %s\n", file, strerror(errno));
return;
}
if (fstat(d, &sb) < 0)
{
printf("FAIL: unable to stat %s: %s\n",
file, strerror(errno));
printf("FAIL: unable to stat %s: %s\n", file, strerror(errno));
close(d);
return;
}
char *buf = malloc(sb.st_size + 1);
if(!buf)
if (!buf)
{
printf("FAIL: unable to allocate memory\n");
close(d);
@@ -112,8 +109,7 @@ static void stat_and_cat(const char *file)
}
if (read(d, buf, sb.st_size) < sb.st_size)
{
printf("FAIL: unable to read all of %s: %s\n",
file, strerror(errno));
printf("FAIL: unable to read all of %s: %s\n", file, strerror(errno));
free(buf);
close(d);
return;
@@ -126,8 +122,8 @@ static void stat_and_cat(const char *file)
int main(int argc, char **argv)
{
// json_object_to_file(file, obj);
// json_object_to_file_ext(file, obj, flags);
// json_object_to_file(file, obj);
// json_object_to_file_ext(file, obj, flags);
_json_c_strerror_enable = 1;
@@ -135,9 +131,9 @@ int main(int argc, char **argv)
if (argc < 2)
{
fprintf(stderr,
"Usage: %s <testdir>\n"
" <testdir> is the location of input files\n",
argv[0]);
"Usage: %s <testdir>\n"
" <testdir> is the location of input files\n",
argv[0]);
return EXIT_FAILURE;
}
testdir = argv[1];
@@ -158,23 +154,19 @@ static void test_read_valid_with_fd(const char *testdir)
int d = open(filename, O_RDONLY, 0);
if (d < 0)
{
fprintf(stderr,
"FAIL: unable to open %s: %s\n",
filename, strerror(errno));
fprintf(stderr, "FAIL: unable to open %s: %s\n", filename, strerror(errno));
exit(EXIT_FAILURE);
}
json_object *jso = json_object_from_fd(d);
if (jso != NULL)
{
printf("OK: json_object_from_fd(valid.json)=%s\n",
json_object_to_json_string(jso));
printf("OK: json_object_from_fd(valid.json)=%s\n", json_object_to_json_string(jso));
json_object_put(jso);
}
else
{
fprintf(stderr,
"FAIL: unable to parse contents of %s: %s\n",
filename, json_util_get_last_err());
fprintf(stderr, "FAIL: unable to parse contents of %s: %s\n", filename,
json_util_get_last_err());
}
close(d);
}
@@ -187,9 +179,7 @@ static void test_read_valid_nested_with_fd(const char *testdir)
int d = open(filename, O_RDONLY, 0);
if (d < 0)
{
fprintf(stderr,
"FAIL: unable to open %s: %s\n",
filename, strerror(errno));
fprintf(stderr, "FAIL: unable to open %s: %s\n", filename, strerror(errno));
exit(EXIT_FAILURE);
}
json_object *jso = json_object_from_fd_ex(d, 20);
@@ -201,9 +191,8 @@ static void test_read_valid_nested_with_fd(const char *testdir)
}
else
{
fprintf(stderr,
"FAIL: unable to parse contents of %s: %s\n",
filename, json_util_get_last_err());
fprintf(stderr, "FAIL: unable to parse contents of %s: %s\n", filename,
json_util_get_last_err());
}
(void)lseek(d, SEEK_SET, 0);
@@ -211,14 +200,15 @@ static void test_read_valid_nested_with_fd(const char *testdir)
jso = json_object_from_fd_ex(d, 3);
if (jso != NULL)
{
printf("FAIL: json_object_from_fd_ex(%s, 3)=%s\n",
filename, json_object_to_json_string(jso));
printf("FAIL: json_object_from_fd_ex(%s, 3)=%s\n", filename,
json_object_to_json_string(jso));
json_object_put(jso);
}
else
{
printf("OK: correctly unable to parse contents of valid_nested.json with low max depth: %s\n",
json_util_get_last_err());
printf("OK: correctly unable to parse contents of valid_nested.json with low max "
"depth: %s\n",
json_util_get_last_err());
}
close(d);
}
@@ -230,14 +220,14 @@ static void test_read_nonexistant()
json_object *jso = json_object_from_file(filename);
if (jso != NULL)
{
printf("FAIL: json_object_from_file(%s) returned %p when NULL expected\n",
filename, (void *)jso);
printf("FAIL: json_object_from_file(%s) returned %p when NULL expected\n", filename,
(void *)jso);
json_object_put(jso);
}
else
{
printf("OK: json_object_from_file(%s) correctly returned NULL: %s\n",
filename, json_util_get_last_err());
printf("OK: json_object_from_file(%s) correctly returned NULL: %s\n", filename,
json_util_get_last_err());
}
}
@@ -245,7 +235,7 @@ static void test_read_closed()
{
// Test reading from a closed fd
int d = open("/dev/null", O_RDONLY, 0);
if(d < 0)
if (d < 0)
{
puts("FAIL: unable to open");
}
@@ -261,8 +251,7 @@ static void test_read_closed()
json_object *jso = json_object_from_fd(fixed_d);
if (jso != NULL)
{
printf("FAIL: read from closed fd returning non-NULL: %p\n",
(void *)jso);
printf("FAIL: read from closed fd returning non-NULL: %p\n", (void *)jso);
fflush(stdout);
printf(" jso=%s\n", json_object_to_json_string(jso));
json_object_put(jso);

View File

@@ -1,8 +1,8 @@
#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <assert.h>
#include "json.h"
#include "json_tokener.h"
@@ -68,24 +68,17 @@ int main(void)
return 0;
}
static int emit_object(json_object *jso, int flags,
json_object *parent_jso,
const char *jso_key,
size_t *jso_index, void *userarg)
static int emit_object(json_object *jso, int flags, json_object *parent_jso, const char *jso_key,
size_t *jso_index, void *userarg)
{
printf("flags: 0x%x, key: %s, index: %ld, value: %s\n",
flags,
(jso_key ? jso_key : "(null)"),
(jso_index ? (long)*jso_index : -1L),
printf("flags: 0x%x, key: %s, index: %ld, value: %s\n", flags,
(jso_key ? jso_key : "(null)"), (jso_index ? (long)*jso_index : -1L),
json_object_to_json_string(jso));
return JSON_C_VISIT_RETURN_CONTINUE;
}
static int skip_arrays(json_object *jso, int flags,
json_object *parent_jso,
const char *jso_key,
size_t *jso_index, void *userarg)
static int skip_arrays(json_object *jso, int flags, json_object *parent_jso, const char *jso_key,
size_t *jso_index, void *userarg)
{
(void)emit_object(jso, flags, parent_jso, jso_key, jso_index, userarg);
if (json_object_get_type(jso) == json_type_array)
@@ -93,10 +86,8 @@ static int skip_arrays(json_object *jso, int flags,
return JSON_C_VISIT_RETURN_CONTINUE;
}
static int pop_and_stop(json_object *jso, int flags,
json_object *parent_jso,
const char *jso_key,
size_t *jso_index, void *userarg)
static int pop_and_stop(json_object *jso, int flags, json_object *parent_jso, const char *jso_key,
size_t *jso_index, void *userarg)
{
(void)emit_object(jso, flags, parent_jso, jso_key, jso_index, userarg);
if (jso_key != NULL && strcmp(jso_key, "subobj1") == 0)
@@ -112,10 +103,8 @@ static int pop_and_stop(json_object *jso, int flags,
return JSON_C_VISIT_RETURN_CONTINUE;
}
static int err_on_subobj2(json_object *jso, int flags,
json_object *parent_jso,
const char *jso_key,
size_t *jso_index, void *userarg)
static int err_on_subobj2(json_object *jso, int flags, json_object *parent_jso, const char *jso_key,
size_t *jso_index, void *userarg)
{
(void)emit_object(jso, flags, parent_jso, jso_key, jso_index, userarg);
if (jso_key != NULL && strcmp(jso_key, "subobj2") == 0)
@@ -126,9 +115,7 @@ static int err_on_subobj2(json_object *jso, int flags,
return JSON_C_VISIT_RETURN_CONTINUE;
}
static int pop_array(json_object *jso, int flags,
json_object *parent_jso,
const char *jso_key,
static int pop_array(json_object *jso, int flags, json_object *parent_jso, const char *jso_key,
size_t *jso_index, void *userarg)
{
(void)emit_object(jso, flags, parent_jso, jso_key, jso_index, userarg);
@@ -140,10 +127,8 @@ static int pop_array(json_object *jso, int flags,
return JSON_C_VISIT_RETURN_CONTINUE;
}
static int stop_array(json_object *jso, int flags,
json_object *parent_jso,
const char *jso_key,
size_t *jso_index, void *userarg)
static int stop_array(json_object *jso, int flags, json_object *parent_jso, const char *jso_key,
size_t *jso_index, void *userarg)
{
(void)emit_object(jso, flags, parent_jso, jso_key, jso_index, userarg);
if (jso_index != NULL && (*jso_index == 0))
@@ -154,15 +139,11 @@ static int stop_array(json_object *jso, int flags,
return JSON_C_VISIT_RETURN_CONTINUE;
}
static int err_return(json_object *jso, int flags,
json_object *parent_jso,
const char *jso_key,
size_t *jso_index, void *userarg)
static int err_return(json_object *jso, int flags, json_object *parent_jso, const char *jso_key,
size_t *jso_index, void *userarg)
{
printf("flags: 0x%x, key: %s, index: %ld, value: %s\n",
flags,
(jso_key ? jso_key : "(null)"),
(jso_index ? (long)*jso_index : -1L),
printf("flags: 0x%x, key: %s, index: %ld, value: %s\n", flags,
(jso_key ? jso_key : "(null)"), (jso_index ? (long)*jso_index : -1L),
json_object_to_json_string(jso));
return 100;
}