![](/media/cache/profiles/ndu.jpg.50x50_q85.jpg)
condens
Dependencies: mbed MbedJSONValue
main.cpp@1:5abef2328a97, 2019-08-22 (annotated)
- Committer:
- duchonic
- Date:
- Thu Aug 22 11:37:10 2019 +0000
- Revision:
- 1:5abef2328a97
- Parent:
- 0:8dd13dfd2e4e
- Child:
- 2:d5f4f429f3db
json parser
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
duchonic | 0:8dd13dfd2e4e | 1 | #include "mbed.h" |
duchonic | 1:5abef2328a97 | 2 | #include "MbedJSONValue.h" |
duchonic | 1:5abef2328a97 | 3 | #include <string> |
duchonic | 0:8dd13dfd2e4e | 4 | |
duchonic | 0:8dd13dfd2e4e | 5 | Ticker toggle_led_ticker; |
duchonic | 0:8dd13dfd2e4e | 6 | Ticker sendStuffTicker; |
duchonic | 0:8dd13dfd2e4e | 7 | |
duchonic | 0:8dd13dfd2e4e | 8 | DigitalOut led1(LED1); |
duchonic | 0:8dd13dfd2e4e | 9 | DigitalOut led2(LED2); |
duchonic | 0:8dd13dfd2e4e | 10 | Serial pc(SERIAL_TX, SERIAL_RX); |
duchonic | 0:8dd13dfd2e4e | 11 | |
duchonic | 0:8dd13dfd2e4e | 12 | void toggle_led() { |
duchonic | 0:8dd13dfd2e4e | 13 | led1 = !led1; |
duchonic | 0:8dd13dfd2e4e | 14 | } |
duchonic | 0:8dd13dfd2e4e | 15 | |
duchonic | 0:8dd13dfd2e4e | 16 | void sendStuff() { |
duchonic | 0:8dd13dfd2e4e | 17 | static int counter=0; |
duchonic | 0:8dd13dfd2e4e | 18 | pc.printf("some stuff %u\n", counter++); |
duchonic | 0:8dd13dfd2e4e | 19 | } |
duchonic | 0:8dd13dfd2e4e | 20 | |
duchonic | 0:8dd13dfd2e4e | 21 | |
duchonic | 1:5abef2328a97 | 22 | void makeJson(){ |
duchonic | 1:5abef2328a97 | 23 | MbedJSONValue demo; |
duchonic | 1:5abef2328a97 | 24 | std::string s; |
duchonic | 1:5abef2328a97 | 25 | |
duchonic | 1:5abef2328a97 | 26 | //fill the object |
duchonic | 1:5abef2328a97 | 27 | demo["my_array"][0] = "demo_string"; |
duchonic | 1:5abef2328a97 | 28 | demo["my_array"][1] = 10; |
duchonic | 1:5abef2328a97 | 29 | demo["my_boolean"] = false; |
duchonic | 1:5abef2328a97 | 30 | |
duchonic | 1:5abef2328a97 | 31 | //serialize it into a JSON string |
duchonic | 1:5abef2328a97 | 32 | s = demo.serialize(); |
duchonic | 1:5abef2328a97 | 33 | pc.printf("json: %s\r\n", s.c_str()); |
duchonic | 1:5abef2328a97 | 34 | } |
duchonic | 1:5abef2328a97 | 35 | |
duchonic | 1:5abef2328a97 | 36 | void parseJson(){ |
duchonic | 1:5abef2328a97 | 37 | MbedJSONValue demo; |
duchonic | 1:5abef2328a97 | 38 | |
duchonic | 1:5abef2328a97 | 39 | const char * json = "{\"my_array\": [\"demo_string\", 10], \"my_boolean\": true}"; |
duchonic | 1:5abef2328a97 | 40 | |
duchonic | 1:5abef2328a97 | 41 | //parse the previous string and fill the object demo |
duchonic | 1:5abef2328a97 | 42 | parse(demo, json); |
duchonic | 1:5abef2328a97 | 43 | |
duchonic | 1:5abef2328a97 | 44 | std::string my_str; |
duchonic | 1:5abef2328a97 | 45 | int my_int; |
duchonic | 1:5abef2328a97 | 46 | bool my_bool; |
duchonic | 1:5abef2328a97 | 47 | |
duchonic | 1:5abef2328a97 | 48 | my_str = demo["my_array"][0].get<std::string>(); |
duchonic | 1:5abef2328a97 | 49 | my_int = demo["my_array"][1].get<int>(); |
duchonic | 1:5abef2328a97 | 50 | my_bool = demo["my_boolean"].get<bool>(); |
duchonic | 1:5abef2328a97 | 51 | |
duchonic | 1:5abef2328a97 | 52 | pc.printf("my_str: %s\r\n", my_str.c_str()); |
duchonic | 1:5abef2328a97 | 53 | pc.printf("my_int: %d\r\n", my_int); |
duchonic | 1:5abef2328a97 | 54 | pc.printf("my_bool: %s\r\n", my_bool ? "true" : "false"); |
duchonic | 1:5abef2328a97 | 55 | } |
duchonic | 1:5abef2328a97 | 56 | |
duchonic | 0:8dd13dfd2e4e | 57 | int main() { |
duchonic | 0:8dd13dfd2e4e | 58 | pc.baud(115200); |
duchonic | 0:8dd13dfd2e4e | 59 | pc.printf("start main()\n\r"); |
duchonic | 0:8dd13dfd2e4e | 60 | |
duchonic | 1:5abef2328a97 | 61 | makeJson(); |
duchonic | 1:5abef2328a97 | 62 | parseJson(); |
duchonic | 1:5abef2328a97 | 63 | |
duchonic | 1:5abef2328a97 | 64 | toggle_led_ticker.attach(&toggle_led, 0.2); |
duchonic | 1:5abef2328a97 | 65 | sendStuffTicker.attach(&sendStuff, 2); |
duchonic | 0:8dd13dfd2e4e | 66 | |
duchonic | 0:8dd13dfd2e4e | 67 | while (true) { |
duchonic | 0:8dd13dfd2e4e | 68 | // Do other things... |
duchonic | 1:5abef2328a97 | 69 | |
duchonic | 0:8dd13dfd2e4e | 70 | wait(1); |
duchonic | 0:8dd13dfd2e4e | 71 | } |
duchonic | 0:8dd13dfd2e4e | 72 | } |