condens project

Dependencies:   mbed MbedJSONValue

Files at this revision

API Documentation at this revision

Comitter:
duchonic
Date:
Thu Aug 22 11:37:10 2019 +0000
Parent:
0:8dd13dfd2e4e
Commit message:
json parser

Changed in this revision

MbedJSONValue.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 8dd13dfd2e4e -r 5abef2328a97 MbedJSONValue.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MbedJSONValue.lib	Thu Aug 22 11:37:10 2019 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/samux/code/MbedJSONValue/#10a99cdf7846
diff -r 8dd13dfd2e4e -r 5abef2328a97 main.cpp
--- a/main.cpp	Thu Aug 22 10:51:23 2019 +0000
+++ b/main.cpp	Thu Aug 22 11:37:10 2019 +0000
@@ -1,4 +1,6 @@
 #include "mbed.h"
+#include "MbedJSONValue.h"
+#include <string>
 
 Ticker toggle_led_ticker;
 Ticker sendStuffTicker;
@@ -17,17 +19,54 @@
 }
 
 
+void makeJson(){
+    MbedJSONValue demo;
+    std::string s;
+ 
+    //fill the object
+    demo["my_array"][0] = "demo_string";
+    demo["my_array"][1] = 10;
+    demo["my_boolean"] = false;
+ 
+    //serialize it into a JSON string
+    s = demo.serialize();
+    pc.printf("json: %s\r\n", s.c_str());    
+}
+
+void parseJson(){
+     MbedJSONValue demo;
+ 
+    const  char * json = "{\"my_array\": [\"demo_string\", 10], \"my_boolean\": true}";
+ 
+    //parse the previous string and fill the object demo
+    parse(demo, json);
+ 
+    std::string my_str;
+    int my_int;
+    bool my_bool;
+ 
+    my_str = demo["my_array"][0].get<std::string>();
+    my_int = demo["my_array"][1].get<int>();
+    my_bool = demo["my_boolean"].get<bool>();
+    
+    pc.printf("my_str: %s\r\n", my_str.c_str());
+    pc.printf("my_int: %d\r\n", my_int);
+    pc.printf("my_bool: %s\r\n", my_bool ? "true" : "false");
+}
+
 int main() {
     pc.baud(115200);
     pc.printf("start main()\n\r");    
     
-    // Init the ticker with the address of the function (toggle_led) to be attached and the interval (100 ms)
-    toggle_led_ticker.attach(&toggle_led, 0.1);
-    sendStuffTicker.attach(&sendStuff, 1);
+    makeJson();
+    parseJson();
+    
+    toggle_led_ticker.attach(&toggle_led, 0.2);
+    sendStuffTicker.attach(&sendStuff, 2);
     
     while (true) {
         // Do other things...
-        led2 = !led2;
+
         wait(1);
     }
 }
\ No newline at end of file