|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | + |
| 15 | +#include <stdio.h> |
| 16 | +#include <string.h> |
| 17 | +#include <json_parser.h> |
| 18 | + |
| 19 | +#define json_test_str "{\n\"str_val\" : \"JSON Parser\",\n" \ |
| 20 | + "\t\"float_val\" : 2.0,\n" \ |
| 21 | + "\"int_val\" : 2017,\n" \ |
| 22 | + "\"bool_val\" : false,\n" \ |
| 23 | + "\"supported_el\" :\t [\"bool\",\"int\","\ |
| 24 | + "\"float\",\"str\"" \ |
| 25 | + ",\"object\",\"array\"],\n" \ |
| 26 | + "\"features\" : { \"objects\":true, "\ |
| 27 | + "\"arrays\":\"yes\"},\n"\ |
| 28 | + "\"int_64\":109174583252}" |
| 29 | + |
| 30 | +int main(int argc, char **argv) |
| 31 | +{ |
| 32 | + jparse_ctx_t jctx; |
| 33 | + int ret = json_parse_start(&jctx, json_test_str, strlen(json_test_str)); |
| 34 | + if (ret != OS_SUCCESS) { |
| 35 | + printf("Parser failed\n"); |
| 36 | + return -1; |
| 37 | + } |
| 38 | + char str_val[20]; |
| 39 | + int int_val, num_elem; |
| 40 | + int64_t int64_val; |
| 41 | + bool bool_val; |
| 42 | + float float_val; |
| 43 | + |
| 44 | + if (json_obj_get_string(&jctx, "str_val", str_val, sizeof(str_val)) == OS_SUCCESS) |
| 45 | + printf("str_val %s\n", str_val); |
| 46 | + |
| 47 | + if (json_obj_get_float(&jctx, "float_val", &float_val) == OS_SUCCESS) |
| 48 | + printf("float_val %f\n", float_val); |
| 49 | + |
| 50 | + if (json_obj_get_int(&jctx, "int_val", &int_val) == OS_SUCCESS) |
| 51 | + printf("int_val %d\n", int_val); |
| 52 | + |
| 53 | + if (json_obj_get_bool(&jctx, "bool_val", &bool_val) == OS_SUCCESS) |
| 54 | + printf("bool_val %s\n", bool_val ? "true" : "false"); |
| 55 | + |
| 56 | + if (json_obj_get_array(&jctx, "supported_el", &num_elem) == OS_SUCCESS) { |
| 57 | + printf("Array has %d elements\n", num_elem); |
| 58 | + int i; |
| 59 | + for (i = 0; i < num_elem; i++) { |
| 60 | + json_arr_get_string(&jctx, i, str_val, sizeof(str_val)); |
| 61 | + printf("index %d: %s\n", i, str_val); |
| 62 | + } |
| 63 | + json_obj_leave_array(&jctx); |
| 64 | + } |
| 65 | + if (json_obj_get_object(&jctx, "features") == OS_SUCCESS) { |
| 66 | + printf("Found object\n"); |
| 67 | + if (json_obj_get_bool(&jctx, "objects", &bool_val) == OS_SUCCESS) |
| 68 | + printf("objects %s\n", bool_val ? "true" : "false"); |
| 69 | + if (json_obj_get_string(&jctx, "arrays", str_val, sizeof(str_val)) == OS_SUCCESS) |
| 70 | + printf("arrays %s\n", str_val); |
| 71 | + json_obj_leave_object(&jctx); |
| 72 | + } |
| 73 | + if (json_obj_get_int64(&jctx, "int_64", &int64_val) == OS_SUCCESS) |
| 74 | + printf("int64_val %lld\n", int64_val); |
| 75 | + |
| 76 | + json_parse_end(&jctx); |
| 77 | + return 0; |
| 78 | + |
| 79 | +} |
0 commit comments