]> thyrsus.com Git - microjson.git/commitdiff
Integer arrays are now accepted (with unut test).
authorEric S. Raymond <esr@thyrsus.com>
Wed, 17 Sep 2014 21:03:56 +0000 (17:03 -0400)
committerEric S. Raymond <esr@thyrsus.com>
Wed, 17 Sep 2014 21:03:56 +0000 (17:03 -0400)
Makefile
microjson.c
microjson.h
test_microjson.c

index 025db306ac126458f0090c5853d85b7e3aa29b9a..4f4e62b8f6f2ac2b509f9c9cddc0e8907883574a 100644 (file)
--- 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:
index 7d19ccae83d592645dec4c5eb8e5653cc2add483..58725ae97b5706481e9ecc7540b605232b3d0e74 100644 (file)
@@ -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",
index 8f60917559dead036b701fbb1c311ee56edb0a75..43b453e81515cfdc719c3d58d47807f9efb1c884 100644 (file)
@@ -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 */
index b2ba68a5b8065355e11381548c9229ea025c8cfc..fd4358ce628716876c7e5e26ad29049f518d7814 100644 (file)
@@ -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);