You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by cc...@apache.org on 2016/05/19 21:32:27 UTC

incubator-mynewt-core git commit: json - Fix array-of-int decoding; add basic test.

Repository: incubator-mynewt-core
Updated Branches:
  refs/heads/develop 9004fbfcd -> ffd7dd376


json - Fix array-of-int decoding; add basic test.

Array decoding seems to be broken.  This is an inefficient and ugly fix
just to get it working.  We will need to rethink the semantics of the
function pointers that get used here (read_next, read_prev, and readn).
The fundamental problem is that the current offset is adjusted
automatically upon read when sometimes we just need to "peek" at the
current character.

The other issue is that we don't know how many characters an string
representation of an int occupies.  Currently we just read up to 64
characters and then backtrack until the end of the number.  It would be
better to read individual characters until we hit the end of the number.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/ffd7dd37
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/ffd7dd37
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/ffd7dd37

Branch: refs/heads/develop
Commit: ffd7dd376df7393d516b62853eb8dee302e0931b
Parents: 9004fbf
Author: Christopher Collins <cc...@apache.org>
Authored: Wed May 18 20:24:32 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Thu May 19 14:32:04 2016 -0700

----------------------------------------------------------------------
 libs/json/src/json_decode.c           | 10 ++---
 libs/json/src/test/test_json_simple.c | 62 +++++++++++++++++++++++++-----
 2 files changed, 58 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ffd7dd37/libs/json/src/json_decode.c
----------------------------------------------------------------------
diff --git a/libs/json/src/json_decode.c b/libs/json/src/json_decode.c
index 0d81c0d..78d1bc9 100644
--- a/libs/json/src/json_decode.c
+++ b/libs/json/src/json_decode.c
@@ -508,6 +508,7 @@ json_read_array(struct json_buffer *jb, const struct json_array_t *arr)
         goto breakout;
     }
 
+    jb->jb_read_prev(jb);
     for (offset = 0; offset < arr->maxlen; offset++) {
         char *ep = NULL;
         switch (arr->element_type) {
@@ -554,10 +555,11 @@ json_read_array(struct json_buffer *jb, const struct json_array_t *arr)
             if (ep == valbuf) {
                 return JSON_ERR_BADNUM;
             } else {
-                count = ep - valbuf;
+                count = n - (ep - valbuf);
                 while (count-- > 0) {
-                    c = jb->jb_read_next(jb);
+                    jb->jb_read_prev(jb);
                 }
+                c = jb->jb_read_next(jb);
             }
             break;
         case t_uinteger:
@@ -623,9 +625,7 @@ json_read_array(struct json_buffer *jb, const struct json_array_t *arr)
         }
         if (c == ']') {
             goto breakout;
-        } else if (c == ',') {
-            c = jb->jb_read_next(jb);
-        } else {
+        } else if (c != ',') {
             return JSON_ERR_BADSUBTRAIL;
         }
     }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ffd7dd37/libs/json/src/test/test_json_simple.c
----------------------------------------------------------------------
diff --git a/libs/json/src/test/test_json_simple.c b/libs/json/src/test/test_json_simple.c
index 39d14b8..45121f8 100644
--- a/libs/json/src/test/test_json_simple.c
+++ b/libs/json/src/test/test_json_simple.c
@@ -23,7 +23,7 @@
 #include "test_json.h"
 #include "json/json.h"
 
-static char *output = "{\"KeyBool\": true,\"KeyInt\": -1234,\"KeyUint\": 1353214,\"KeyString\": \"foobar\",\"KeyStringN\": \"foobarlong\"}";
+static char *output = "{\"KeyBool\": true,\"KeyInt\": -1234,\"KeyUint\": 1353214,\"KeyString\": \"foobar\",\"KeyStringN\": \"foobarlong\",\"KeyIntArr\": [153,2532,-322]}";
 static char bigbuf[512];
 static int buf_index;
 
@@ -70,7 +70,28 @@ TEST_CASE(test_json_simple_encode){
     JSON_VALUE_STRINGN(&value, "foobarlongstring", 10);
     rc = json_encode_object_entry(&encoder, "KeyStringN", &value);
     TEST_ASSERT(rc == 0);  
-    
+
+    rc = json_encode_array_name(&encoder, "KeyIntArr");
+    TEST_ASSERT(rc == 0);
+
+    rc = json_encode_array_start(&encoder);
+    TEST_ASSERT(rc == 0);
+
+    JSON_VALUE_INT(&value, 153);
+    rc = json_encode_array_value(&encoder, &value);
+    TEST_ASSERT(rc == 0);
+
+    JSON_VALUE_INT(&value, 2532);
+    rc = json_encode_array_value(&encoder, &value);
+    TEST_ASSERT(rc == 0);
+
+    JSON_VALUE_INT(&value, -322);
+    rc = json_encode_array_value(&encoder, &value);
+    TEST_ASSERT(rc == 0);
+
+    rc = json_encode_array_finish(&encoder);
+    TEST_ASSERT(rc == 0);
+
     rc = json_encode_object_finish(&encoder);    
     TEST_ASSERT(rc == 0);
     
@@ -123,13 +144,17 @@ test_jbuf_read_prev(struct json_buffer *jb) {
 static int 
 test_jbuf_readn(struct json_buffer *jb, char *buf, int size) {
     struct test_jbuf  *ptjb = (struct test_jbuf*) jb;    
-    
-    if((ptjb->end_buf - (ptjb->start_buf + ptjb->current_position) + 1) >= size) {
-        memcpy(buf, ptjb->start_buf + ptjb->current_position, size);
-        ptjb->current_position += size;
-        return 0;
+
+    int remlen;
+
+    remlen = ptjb->end_buf - (ptjb->start_buf + ptjb->current_position);
+    if (size > remlen) {
+        size = remlen;
     }
-    return -1;
+
+    memcpy(buf, ptjb->start_buf + ptjb->current_position, size);
+    ptjb->current_position += size;
+    return size;
 }
 
 static void 
@@ -152,9 +177,11 @@ TEST_CASE(test_json_simple_decode){
     bool bool_val;
     char string1[16];
     char string2[16];
+    long long int intarr[8];
     int rc;
+    int array_count;
 
-    const struct json_attr_t test_attr[6] = {
+    struct json_attr_t test_attr[7] = {
         [0] = {
             .attribute = "KeyBool",
             .type = t_boolean,
@@ -188,6 +215,18 @@ TEST_CASE(test_json_simple_decode){
             .len = sizeof(string2)
         },
         [5] = {
+            .attribute = "KeyIntArr",
+            .type = t_array,
+            .addr.array = {
+                .element_type = t_integer,
+                .arr.integers.store = intarr,
+                .maxlen = sizeof intarr / sizeof intarr[0],
+                .count = &array_count,
+            },
+            .nodefault = true,
+            .len = sizeof(intarr)
+        },
+        [6] = {
             .attribute = NULL
         }
     };
@@ -205,4 +244,9 @@ TEST_CASE(test_json_simple_decode){
 
     rc = memcmp(string2, "foobarlongstring", 10);
     TEST_ASSERT(rc==0);
+
+    TEST_ASSERT(array_count == 3);
+    TEST_ASSERT(intarr[0] == 153);
+    TEST_ASSERT(intarr[1] == 2532);
+    TEST_ASSERT(intarr[2] == -322);
 }