static void action_led_callback(const char* json_str, size_t size) { jobj_t json; jsontok_t json_tokens[JSON_NUM_TOKENS]; int err = json_init(&json, json_tokens, JSON_NUM_TOKENS, (char*)json_str, size); if (err != WM_SUCCESS) { wmprintf("Wrong json string\n\r"); return; } output_gpio_cfg_t led; char type[32]; if (json_get_val_str(&json, "type", type, sizeof(type)) != WM_SUCCESS) { wmprintf("type doesn't exist\r\n"); return; } if (strcmp(type, "_led1") == 0) { led = led_1; } else if (strcmp(type, "_led2") == 0) { led = led_2; } else { wmprintf("not expected type value\r\n"); return; } if (json_get_composite_object(&json, "customFields") != WM_SUCCESS) { wmprintf("Custom fields doesn't exist\n\r"); return; } char status[16]; if (json_get_val_str(&json, "status", status, sizeof(status)) != WM_SUCCESS) { wmprintf("Status doesn't exist\n\r"); return; } json_release_composite_object(&json); wmprintf("Received action: type = \"%s\", status = \"%s\"\n\r", type, status); if (strcmp(status, "0") == 0) { led_off(led); } else if (strcmp(status, "1") == 0) { led_on(led); } }
void json_test(int argc, char **argv) { int num; int fmt; char text[] = "{\n \"precision\": \"zip\",\n \"Latitude\": 37668,\n \"Longitude\": -12259,\n \"Address\": \"\",\n \"City\": \"SAN FRANCISCO\",\n \"State\": \"CA\",\n \"Zip\": \"94107\",\n \"Country\": \"US\"\n }"; char complex_obj[] = "{ \"glossary\": { \"title\": \"example glossary\", \"GlossDiv\": { \"title\": \"S\", \"GlossList\": { \"GlossEntry\": { \"ID\": \"SGML\", \"SortAs\": \"SGML\",\"GlossTerm\": \"Standard Generalized Markup Language\", \"Acronym\": \"SGML\", \"Abbrev\": \"ISO 8879:1986\", \"GlossDef\": { \"para\": \"language\", \"GlossSeeAlso\": \"XML\" }, \"GlossSee\": \"markup\" } } } } }"; debug("#### json_parser ####\n"); json_object_init(&tmp, text); if (!json_get_val_str(&tmp, "Country", str, MAX_JSON_STR_LEN)) { if (!strcmp(str, "US")) { debug("***Json_parser ---str_get match\n"); } else { debug("***Err ---str_get mismatch\n"); goto ERROR; } } else { debug("***Err ---str_get failed\n"); goto ERROR; } if (!json_get_val_int(&tmp, "Longitude", &num)) { if (num == -12259) { debug("***Json_parser ---int_get match\n"); } else { debug("***Err ---int_get mismatch\n"); goto ERROR; } } else { debug("***Err ---int_get failed\n"); goto ERROR; } fmt = 1; debug("***Json_parser ---Simple Set Example\n"); json_str_init(&jstr, tmpstr, sizeof(tmpstr), fmt); json_start_object(&jstr); json_set_val_str(&jstr, "name", "John Galt"); json_set_val_int(&jstr, "age", 29); json_set_val_str(&jstr, "numberstr", "123"); json_close_object(&jstr); debug("The JSON String is :%s:\n", jstr.buff); debug("***Json_parser --- Complex object Set Example\n"); json_str_init(&jstr_c, tmpstr, sizeof(tmpstr), fmt); json_start_object(&jstr_c); json_push_object(&jstr_c, "menu"); json_set_val_str(&jstr_c, "pictures", "tmp"); json_push_object(&jstr_c, "Image"); json_set_val_str(&jstr_c, "name", "sun"); json_set_val_int(&jstr_c, "length", 1000); json_set_val_str(&jstr_c, "size", "1600pi"); json_pop_object(&jstr_c); /* Close Image */ json_push_object(&jstr_c, "text"); json_set_val_str(&jstr_c, "style", "bold"); json_set_val_str(&jstr_c, "alignment", "center"); json_set_val_str(&jstr_c, "name", "text1"); json_pop_object(&jstr_c); /* Close text */ json_set_val_str(&jstr_c, "object", "close"); json_pop_object(&jstr_c); /* Close menu */ json_close_object(&jstr_c); /* Close main */ debug("The JSON String is *%s*\n", jstr_c.buff); struct json_object obj; json_object_init(&obj, jstr_c.buff); if (!json_get_composite_object(&obj, "text")) { if (!json_get_val_int(&obj, "length", &num)) debug("val is %d \n", num); else debug("***Json_parser ---passed, no such member\n"); if (!json_get_val_str(&obj, "style", str, MAX_JSON_STR_LEN)) { if (!strcmp(str, "bold")) { debug ("***Json_parser ---composite_str_get match\n"); debug("member->style:val->%s\n", str); } else { debug("***Err ---composite_str_get mismatch\n"); goto ERROR; } } else { debug("***Err ---composite_str_get failed\n"); goto ERROR; } } else { debug("***Err ---get_object_offsets failed\n"); goto ERROR; } json_release_composite_object(&obj); json_object_init(&obj, complex_obj); if (!json_get_composite_object(&obj, "GlossEntry")) { if (!json_get_val_str(&obj, "GlossSee", str, 64)) { if (!strcmp(str, "markup")) { debug ("***Json_parser ---composite_str_get match\n"); debug("member->GlossEntry:val->%s\n", str); } else { debug("***Err ---composite_str_get mismatch\n"); goto ERROR; } } else { debug("***Err ---composite_str_get failed\n"); goto ERROR; } } else { debug("***Err ---get_object_offsets failed\n"); goto ERROR; } json_release_composite_object(&obj); goto SUCCESS; ERROR: wmprintf("Error"); SUCCESS: wmprintf("Success"); }