Sample program using picojson library. Note: it seems that there are some memory? issues with \"complex\" structures. So always test your json with the lib before using it within your application.

Dependencies:   mbed picojson

Files at this revision

API Documentation at this revision

Comitter:
mimil
Date:
Fri Sep 09 13:13:17 2011 +0000
Commit message:
first release

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
picojson.lib Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 81c978de0e2b main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Sep 09 13:13:17 2011 +0000
@@ -0,0 +1,68 @@
+#include "mbed.h"
+#include "picojson.h"
+
+
+void parse() {
+    picojson::value v;
+    const char *json = "{\"string\": \"it works\", \"number\": 3.14, \"integer\":5}";
+
+    string err = picojson::parse(v, json, json + strlen(json));
+    printf("res error? %s\r\n", err.c_str());
+    printf("string =%s\r\n" ,  v.get("string").get<string>().c_str());
+    printf("number =%f\r\n" ,  v.get("number").get<double>());
+    printf("integer =%d\r\n" ,  (int)v.get("integer").get<double>());
+}
+
+void serialize() {
+    picojson::object v;
+    picojson::object inner;
+    string val = "tt";
+
+    v["aa"] = picojson::value(val);
+    v["bb"] = picojson::value(1.66);
+    inner["test"] =  picojson::value(true);
+    inner["integer"] =  picojson::value(1.0);
+    v["inner"] =  picojson::value(inner);
+
+    string str = picojson::value(v).serialize();
+    printf("serialized content = %s\r\n" ,  str.c_str());
+}
+
+void advanced() {
+    picojson::value v;
+    const char *jsonsoure =
+        "{\"menu\": {"
+        "\"id\": \"f\","
+        "\"popup\": {"
+        "  \"menuitem\": ["
+        "    {\"v\": \"0\"},"
+        "    {\"v\": \"1\"},"
+        "    {\"v\": \"2\"}"
+        "   ]"
+        "  }"
+        "}"
+        "}";
+    char * json = (char*) malloc(strlen(jsonsoure)+1);
+    strcpy(json, jsonsoure);
+    string err = picojson::parse(v, json, json + strlen(json));
+    printf("res error? %s\r\n", err.c_str());
+
+    printf("id =%s\r\n", v.get("menu").get("id").get<string>().c_str());
+
+    picojson::array list = v.get("menu").get("popup").get("menuitem").get<picojson::array>();
+
+    for (picojson::array::iterator iter = list.begin(); iter != list.end(); ++iter) {
+        printf("menu item value =%s\r\n", (*iter).get("v").get<string>().c_str());
+    }
+}
+
+int main() {
+    printf("Starting PicoJSONSample "__TIME__"\r\n");
+    printf(">>> parsing \r\n");
+    parse();
+    printf(">>> serializing \r\n");
+    serialize();
+    printf(">>> advanced parsing \r\n");
+    advanced();
+    printf("Ending PicoJSONSample " __TIME__ "\r\n");
+}
diff -r 000000000000 -r 81c978de0e2b mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Fri Sep 09 13:13:17 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/e2ac27c8e93e
diff -r 000000000000 -r 81c978de0e2b picojson.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/picojson.lib	Fri Sep 09 13:13:17 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mimil/code/picojson/#2bb500b021e2