From: Eric S. Raymond Date: Wed, 17 Sep 2014 21:03:56 +0000 (-0400) Subject: Integer arrays are now accepted (with unut test). X-Git-Tag: 1.0~25 X-Git-Url: http://thyrsus.com/gitweb/?a=commitdiff_plain;h=e1eccb4e151c2f065f90ec0483f27c2ab25dcf24;p=microjson.git Integer arrays are now accepted (with unut test). --- diff --git a/Makefile b/Makefile index 025db30..4f4e62b 100644 --- a/Makefile +++ b/Makefile @@ -5,12 +5,12 @@ VERSION=0.1 CFLAGS = -O # Add DEBUG_ENABLE for the tracing code -# CFLAGS += -DDEBUG_ENABLE +CFLAGS += -DDEBUG_ENABLE -g microjson.o: microjson.c microjson.h test_microjson: test_microjson.o microjson.o - $(CC) -o test_microjson test_microjson.o microjson.o + $(CC) $(CFLAGS) -o test_microjson test_microjson.o microjson.o check: test_microjson test_microjson @@ -19,7 +19,7 @@ check: test_microjson example1: example1.c microjson.c clean: - rm -f microjson.o test_json example1 + rm -f microjson.o test_microjson.o test_microjson example1 CSUPPRESSIONS = -U__UNUSED__ cppcheck: diff --git a/microjson.c b/microjson.c index 7d19cca..58725ae 100644 --- a/microjson.c +++ b/microjson.c @@ -816,6 +816,7 @@ int json_read_array(const char *cp, const struct json_array_t *arr, goto breakout; for (offset = 0; offset < arr->maxlen; offset++) { + char *ep = NULL; json_debug_trace((1, "Looking at %s\n", cp)); switch (arr->element_type) { case t_string: @@ -852,7 +853,19 @@ int json_read_array(const char *cp, const struct json_array_t *arr, return substatus; break; case t_integer: + arr->arr.integers.store[offset] = (int)strtol(cp, &ep, 0); + if (ep == cp) + return JSON_ERR_BADNUM; + else + cp = ep; + break; case t_uinteger: + arr->arr.integers.store[offset] = (int)strtoul(cp, &ep, 0); + if (ep == cp) + return JSON_ERR_BADNUM; + else + cp = ep; + break; case t_time: case t_real: case t_boolean: @@ -913,7 +926,7 @@ const /*@observer@*/ char *json_error_string(int err) "array element specified, but no [", "string value too long", "token value too long", - "garbage while expecting , or }", + "garbage while expecting comma or } or ]", "didn't find expected array start", "error while parsing object array", "too many array elements", diff --git a/microjson.h b/microjson.h index 8f60917..43b453e 100644 --- a/microjson.h +++ b/microjson.h @@ -32,6 +32,10 @@ struct json_array_t { char *store; int storelen; } strings; + struct { + int *store; + int storelen; + } integers; } arr; int *count, maxlen; }; @@ -87,7 +91,7 @@ void json_enable_debug(int, FILE *); #define JSON_ERR_NOBRAK 6 /* array element specified, but no [ */ #define JSON_ERR_STRLONG 7 /* string value too long */ #define JSON_ERR_TOKLONG 8 /* token value too long */ -#define JSON_ERR_BADTRAIL 9 /* garbage while expecting , or } */ +#define JSON_ERR_BADTRAIL 9 /* garbage while expecting comma or } or ] */ #define JSON_ERR_ARRAYSTART 10 /* didn't find expected array start */ #define JSON_ERR_OBJARR 11 /* error while parsing object array */ #define JSON_ERR_SUBTOOLONG 12 /* too many array elements */ diff --git a/test_microjson.c b/test_microjson.c index b2ba68a..fd4358c 100644 --- a/test_microjson.c +++ b/test_microjson.c @@ -606,6 +606,21 @@ static const struct json_attr_t json_attrs_8[] = { static const char *json_str9 = "{\"parts\":[]}"; +/* Case 10: Read array of integers in a member */ + +static const char *json_str10 = "[23,-17,5]"; +static int intstore[4], intcount; + +/*@-type@*/ +static const struct json_array_t json_array_10 = { + .element_type = t_integer, + .arr.integers.store = intstore, + .arr.integers.storelen = sizeof(intstore), + .count = &intcount, + .maxlen = sizeof(intstore)/sizeof(intstore[0]), +}; +/*@+type@*/ + /*@ +fullinitblock @*/ /* *INDENT-ON* */ @@ -710,7 +725,15 @@ static void jsontest(int i) assert_integer("dumbcount", dumbcount, 0); break; -#define MAXTEST 9 + case 10: + status = json_read_array(json_str10, &json_array_10, NULL); + assert_integer("instore[0]", intstore[0], 23); + assert_integer("instore[1]", intstore[1], -17); + assert_integer("instore[2]", intstore[2], 5); + assert_integer("instore[3]", intstore[3], 0); + break; + +#define MAXTEST 10 default: (int)fputs("Unknown test number\n", stderr);