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

[2/5] incubator-mynewt-larva git commit: fix json encoding. add test code to slinky, prior to rebase.

fix json encoding.  add test code to slinky, prior to rebase.


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

Branch: refs/heads/master
Commit: 8f7f01ad8d5fb5dbdce5736213980be0c51a7d35
Parents: 4fb9ec5
Author: Sterling Hughes <st...@apache.org>
Authored: Fri Jan 15 13:21:37 2016 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Tue Jan 19 10:01:51 2016 -0800

----------------------------------------------------------------------
 libs/json/include/json/json.h |  6 +++---
 libs/json/src/json_encode.c   | 28 +++++++++++++++++++++----
 project/slinky/src/main.c     | 43 ++++++++++++++++++++++++++++++++++----
 3 files changed, 66 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/8f7f01ad/libs/json/include/json/json.h
----------------------------------------------------------------------
diff --git a/libs/json/include/json/json.h b/libs/json/include/json/json.h
index 346fc59..d220cc7 100644
--- a/libs/json/include/json/json.h
+++ b/libs/json/include/json/json.h
@@ -65,16 +65,16 @@ struct json_value {
 #define JSON_VALUE_STRING(__jv, __str)        \
     (__jv)->jv_type = JSON_VALUE_TYPE_STRING; \
     (__jv)->jv_len = strlen(__str);           \
-    (__jv)->jv_val.str = str;
+    (__jv)->jv_val.str = (__str);
 
 #define JSON_VALUE_STRINGN(__jv, __str, __len) \
     (__jv)->jv_type = JSON_VALUE_TYPE_STRING;  \
     (__jv)->jv_len = __len;                    \
-    (__jv)->jv_val.str = str;
+    (__jv)->jv_val.str = (__str);
 
 #define JSON_VALUE_BOOL(__jv, __v)            \
     (__jv)->jv_type = JSON_VALUE_TYPE_BOOL;   \
-    (__jv)->jv_val.u = __v;
+    (__jv)->jv_val.u = (__v);
 
 #define JSON_VALUE_INT(__jv, __v)             \
     (__jv)->jv_type = JSON_VALUE_TYPE_INT64;  \

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/8f7f01ad/libs/json/src/json_encode.c
----------------------------------------------------------------------
diff --git a/libs/json/src/json_encode.c b/libs/json/src/json_encode.c
index 94a877f..a5f6018 100644
--- a/libs/json/src/json_encode.c
+++ b/libs/json/src/json_encode.c
@@ -73,18 +73,38 @@ json_encode_value(struct json_encoder *encoder, struct json_value *jv)
                     case '"':
                     case '/':
                     case '\\':
+                        encoder->je_write(encoder->je_arg, "\\", 
+                                sizeof("\\")-1);
+                        encoder->je_write(encoder->je_arg, 
+                                (char *) &jv->jv_val.str[i], 1);
+ 
+                        break;
                     case '\t':
+                        encoder->je_write(encoder->je_arg, "\\t", 
+                                sizeof("\\t")-1);
+                        break;
                     case '\r':
+                        encoder->je_write(encoder->je_arg, "\\r", 
+                                sizeof("\\r")-1);
+                        break;
                     case '\n':
+                        encoder->je_write(encoder->je_arg, "\\n", 
+                                sizeof("\\n")-1);
+                        break;
                     case '\f':
+                        encoder->je_write(encoder->je_arg, "\\f", 
+                                sizeof("\\f")-1);
+                        break;
                     case '\b':
-                        encoder->je_write(encoder->je_arg, "\\", 
-                                sizeof("\\")-1);
+                        encoder->je_write(encoder->je_arg, "\\b", 
+                                sizeof("\\b")-1);
+                        break;
+                   default:
+                        encoder->je_write(encoder->je_arg, 
+                                (char *) &jv->jv_val.str[i], 1);
                         break;
                 }
 
-                encoder->je_write(encoder->je_arg, (char *) &jv->jv_val.str[i], 
-                        1);
             }
             break;
         case JSON_VALUE_TYPE_ARRAY:

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/8f7f01ad/project/slinky/src/main.c
----------------------------------------------------------------------
diff --git a/project/slinky/src/main.c b/project/slinky/src/main.c
index bad9dda..42991ad 100755
--- a/project/slinky/src/main.c
+++ b/project/slinky/src/main.c
@@ -19,16 +19,18 @@
 #include <hal/hal_flash.h>
 #include <console/console.h>
 #include <shell/shell.h>
-#include <log/log.h>
-#include <stats/stats.h>
-#include <config/config.h>
-#include <hal/flash_map.h>
+#include <util/log.h>
+#include <util/stats.h>
+#include <util/config.h>
+#include <util/flash_map.h>
 #include <fs/fs.h>
 #include <nffs/nffs.h>
 #include <newtmgr/newtmgr.h>
 #include <imgmgr/imgmgr.h>
 #include <assert.h>
 #include <string.h>
+#include <json/json.h>
+
 #ifdef ARCH_sim
 #include <mcu/mcu_sim.h>
 #endif
@@ -153,6 +155,37 @@ init_tasks(void)
     return 0;
 }
 
+static void 
+json_test(void)
+{
+    char req[128];
+    char req1[128];
+    char req2[128];
+    int rc;
+
+    struct json_attr_t attrs[] = {
+        {"cmd", t_string, .addr.string = req, .len = sizeof(req)},
+        {"group", t_string, .addr.string = req1, .len = sizeof(req1)},
+        {"req", t_string, .addr.string = req2, .len = sizeof(req2)},
+        {NULL},
+    };
+
+    char *json_data = "{\n" 
+        "  \"group\": \"sterly\",\n"
+        "  \"cmd\": \"read\",\n"
+        "  \"req\": \"{ \\\"name\\\": \\\"blablabalblala\\\" }\", \n"
+        "}\n";
+
+    rc = json_read_object(json_data, attrs, NULL);
+    req[127] = 0;
+
+    puts(json_data);
+    printf("%d req = %s, req1 = %s, req2 = %s\n", rc, req, req1, req2);
+}
+
+
+
+
 /**
  * main
  *
@@ -224,6 +257,8 @@ main(int argc, char **argv)
 
     stats_module_init();
 
+    json_test();
+
     rc = init_tasks();
     os_start();