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
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:
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:
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:
"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",
char *store;
int storelen;
} strings;
+ struct {
+ int *store;
+ int storelen;
+ } integers;
} arr;
int *count, maxlen;
};
#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 */
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* */
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);