Host driver/HAL to build a LoRa Picocell Gateway which communicates through USB with a concentrator board based on Semtech SX1308 multi-channel modem and SX1257/SX1255 RF transceivers.
util_pkt_logger/inc/parson.h@0:102b50f941d0, 2018-04-11 (annotated)
- Committer:
- dgabino
- Date:
- Wed Apr 11 14:38:42 2018 +0000
- Revision:
- 0:102b50f941d0
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dgabino | 0:102b50f941d0 | 1 | /* |
dgabino | 0:102b50f941d0 | 2 | Parson ( http://kgabis.github.com/parson/ ) |
dgabino | 0:102b50f941d0 | 3 | Copyright (c) 2012 - 2016 Krzysztof Gabis |
dgabino | 0:102b50f941d0 | 4 | |
dgabino | 0:102b50f941d0 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
dgabino | 0:102b50f941d0 | 6 | of this software and associated documentation files (the "Software"), to deal |
dgabino | 0:102b50f941d0 | 7 | in the Software without restriction, including without limitation the rights |
dgabino | 0:102b50f941d0 | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
dgabino | 0:102b50f941d0 | 9 | copies of the Software, and to permit persons to whom the Software is |
dgabino | 0:102b50f941d0 | 10 | furnished to do so, subject to the following conditions: |
dgabino | 0:102b50f941d0 | 11 | |
dgabino | 0:102b50f941d0 | 12 | The above copyright notice and this permission notice shall be included in |
dgabino | 0:102b50f941d0 | 13 | all copies or substantial portions of the Software. |
dgabino | 0:102b50f941d0 | 14 | |
dgabino | 0:102b50f941d0 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
dgabino | 0:102b50f941d0 | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
dgabino | 0:102b50f941d0 | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
dgabino | 0:102b50f941d0 | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
dgabino | 0:102b50f941d0 | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
dgabino | 0:102b50f941d0 | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
dgabino | 0:102b50f941d0 | 21 | THE SOFTWARE. |
dgabino | 0:102b50f941d0 | 22 | */ |
dgabino | 0:102b50f941d0 | 23 | |
dgabino | 0:102b50f941d0 | 24 | #ifndef parson_parson_h |
dgabino | 0:102b50f941d0 | 25 | #define parson_parson_h |
dgabino | 0:102b50f941d0 | 26 | |
dgabino | 0:102b50f941d0 | 27 | #ifdef __cplusplus |
dgabino | 0:102b50f941d0 | 28 | extern "C" |
dgabino | 0:102b50f941d0 | 29 | { |
dgabino | 0:102b50f941d0 | 30 | #endif |
dgabino | 0:102b50f941d0 | 31 | |
dgabino | 0:102b50f941d0 | 32 | #include <stddef.h> /* size_t */ |
dgabino | 0:102b50f941d0 | 33 | |
dgabino | 0:102b50f941d0 | 34 | /* Types and enums */ |
dgabino | 0:102b50f941d0 | 35 | typedef struct json_object_t JSON_Object; |
dgabino | 0:102b50f941d0 | 36 | typedef struct json_array_t JSON_Array; |
dgabino | 0:102b50f941d0 | 37 | typedef struct json_value_t JSON_Value; |
dgabino | 0:102b50f941d0 | 38 | |
dgabino | 0:102b50f941d0 | 39 | enum json_value_type { |
dgabino | 0:102b50f941d0 | 40 | JSONError = -1, |
dgabino | 0:102b50f941d0 | 41 | JSONNull = 1, |
dgabino | 0:102b50f941d0 | 42 | JSONString = 2, |
dgabino | 0:102b50f941d0 | 43 | JSONNumber = 3, |
dgabino | 0:102b50f941d0 | 44 | JSONObject = 4, |
dgabino | 0:102b50f941d0 | 45 | JSONArray = 5, |
dgabino | 0:102b50f941d0 | 46 | JSONBoolean = 6 |
dgabino | 0:102b50f941d0 | 47 | }; |
dgabino | 0:102b50f941d0 | 48 | typedef int JSON_Value_Type; |
dgabino | 0:102b50f941d0 | 49 | |
dgabino | 0:102b50f941d0 | 50 | enum json_result_t { |
dgabino | 0:102b50f941d0 | 51 | JSONSuccess = 0, |
dgabino | 0:102b50f941d0 | 52 | JSONFailure = -1 |
dgabino | 0:102b50f941d0 | 53 | }; |
dgabino | 0:102b50f941d0 | 54 | typedef int JSON_Status; |
dgabino | 0:102b50f941d0 | 55 | |
dgabino | 0:102b50f941d0 | 56 | typedef void * (*JSON_Malloc_Function)(size_t); |
dgabino | 0:102b50f941d0 | 57 | typedef void (*JSON_Free_Function)(void *); |
dgabino | 0:102b50f941d0 | 58 | |
dgabino | 0:102b50f941d0 | 59 | /* Call only once, before calling any other function from parson API. If not called, malloc and free |
dgabino | 0:102b50f941d0 | 60 | from stdlib will be used for all allocations */ |
dgabino | 0:102b50f941d0 | 61 | void json_set_allocation_functions(JSON_Malloc_Function malloc_fun, JSON_Free_Function free_fun); |
dgabino | 0:102b50f941d0 | 62 | |
dgabino | 0:102b50f941d0 | 63 | /* Parses first JSON value in a file, returns NULL in case of error */ |
dgabino | 0:102b50f941d0 | 64 | JSON_Value * json_parse_file(const char *filename); |
dgabino | 0:102b50f941d0 | 65 | |
dgabino | 0:102b50f941d0 | 66 | /* Parses first JSON value in a file and ignores comments (/ * * / and //), |
dgabino | 0:102b50f941d0 | 67 | returns NULL in case of error */ |
dgabino | 0:102b50f941d0 | 68 | JSON_Value * json_parse_file_with_comments(const char *filename); |
dgabino | 0:102b50f941d0 | 69 | |
dgabino | 0:102b50f941d0 | 70 | /* Parses first JSON value in a string, returns NULL in case of error */ |
dgabino | 0:102b50f941d0 | 71 | JSON_Value * json_parse_string(const char *string); |
dgabino | 0:102b50f941d0 | 72 | |
dgabino | 0:102b50f941d0 | 73 | /* Parses first JSON value in a string and ignores comments (/ * * / and //), |
dgabino | 0:102b50f941d0 | 74 | returns NULL in case of error */ |
dgabino | 0:102b50f941d0 | 75 | JSON_Value * json_parse_string_with_comments(const char *string); |
dgabino | 0:102b50f941d0 | 76 | |
dgabino | 0:102b50f941d0 | 77 | /* Serialization */ |
dgabino | 0:102b50f941d0 | 78 | size_t json_serialization_size(const JSON_Value *value); /* returns 0 on fail */ |
dgabino | 0:102b50f941d0 | 79 | JSON_Status json_serialize_to_buffer(const JSON_Value *value, char *buf, size_t buf_size_in_bytes); |
dgabino | 0:102b50f941d0 | 80 | JSON_Status json_serialize_to_file(const JSON_Value *value, const char *filename); |
dgabino | 0:102b50f941d0 | 81 | char * json_serialize_to_string(const JSON_Value *value); |
dgabino | 0:102b50f941d0 | 82 | |
dgabino | 0:102b50f941d0 | 83 | /* Pretty serialization */ |
dgabino | 0:102b50f941d0 | 84 | size_t json_serialization_size_pretty(const JSON_Value *value); /* returns 0 on fail */ |
dgabino | 0:102b50f941d0 | 85 | JSON_Status json_serialize_to_buffer_pretty(const JSON_Value *value, char *buf, size_t buf_size_in_bytes); |
dgabino | 0:102b50f941d0 | 86 | JSON_Status json_serialize_to_file_pretty(const JSON_Value *value, const char *filename); |
dgabino | 0:102b50f941d0 | 87 | char * json_serialize_to_string_pretty(const JSON_Value *value); |
dgabino | 0:102b50f941d0 | 88 | |
dgabino | 0:102b50f941d0 | 89 | void json_free_serialized_string(char *string); /* frees string from json_serialize_to_string and json_serialize_to_string_pretty */ |
dgabino | 0:102b50f941d0 | 90 | |
dgabino | 0:102b50f941d0 | 91 | /* Comparing */ |
dgabino | 0:102b50f941d0 | 92 | int json_value_equals(const JSON_Value *a, const JSON_Value *b); |
dgabino | 0:102b50f941d0 | 93 | |
dgabino | 0:102b50f941d0 | 94 | /* Validation |
dgabino | 0:102b50f941d0 | 95 | This is *NOT* JSON Schema. It validates json by checking if object have identically |
dgabino | 0:102b50f941d0 | 96 | named fields with matching types. |
dgabino | 0:102b50f941d0 | 97 | For example schema {"name":"", "age":0} will validate |
dgabino | 0:102b50f941d0 | 98 | {"name":"Joe", "age":25} and {"name":"Joe", "age":25, "gender":"m"}, |
dgabino | 0:102b50f941d0 | 99 | but not {"name":"Joe"} or {"name":"Joe", "age":"Cucumber"}. |
dgabino | 0:102b50f941d0 | 100 | In case of arrays, only first value in schema is checked against all values in tested array. |
dgabino | 0:102b50f941d0 | 101 | Empty objects ({}) validate all objects, empty arrays ([]) validate all arrays, |
dgabino | 0:102b50f941d0 | 102 | null validates values of every type. |
dgabino | 0:102b50f941d0 | 103 | */ |
dgabino | 0:102b50f941d0 | 104 | JSON_Status json_validate(const JSON_Value *schema, const JSON_Value *value); |
dgabino | 0:102b50f941d0 | 105 | |
dgabino | 0:102b50f941d0 | 106 | /* |
dgabino | 0:102b50f941d0 | 107 | * JSON Object |
dgabino | 0:102b50f941d0 | 108 | */ |
dgabino | 0:102b50f941d0 | 109 | JSON_Value * json_object_get_value (const JSON_Object *object, const char *name); |
dgabino | 0:102b50f941d0 | 110 | const char * json_object_get_string (const JSON_Object *object, const char *name); |
dgabino | 0:102b50f941d0 | 111 | JSON_Object * json_object_get_object (const JSON_Object *object, const char *name); |
dgabino | 0:102b50f941d0 | 112 | JSON_Array * json_object_get_array (const JSON_Object *object, const char *name); |
dgabino | 0:102b50f941d0 | 113 | double json_object_get_number (const JSON_Object *object, const char *name); /* returns 0 on fail */ |
dgabino | 0:102b50f941d0 | 114 | int json_object_get_boolean(const JSON_Object *object, const char *name); /* returns -1 on fail */ |
dgabino | 0:102b50f941d0 | 115 | |
dgabino | 0:102b50f941d0 | 116 | /* dotget functions enable addressing values with dot notation in nested objects, |
dgabino | 0:102b50f941d0 | 117 | just like in structs or c++/java/c# objects (e.g. objectA.objectB.value). |
dgabino | 0:102b50f941d0 | 118 | Because valid names in JSON can contain dots, some values may be inaccessible |
dgabino | 0:102b50f941d0 | 119 | this way. */ |
dgabino | 0:102b50f941d0 | 120 | JSON_Value * json_object_dotget_value (const JSON_Object *object, const char *name); |
dgabino | 0:102b50f941d0 | 121 | const char * json_object_dotget_string (const JSON_Object *object, const char *name); |
dgabino | 0:102b50f941d0 | 122 | JSON_Object * json_object_dotget_object (const JSON_Object *object, const char *name); |
dgabino | 0:102b50f941d0 | 123 | JSON_Array * json_object_dotget_array (const JSON_Object *object, const char *name); |
dgabino | 0:102b50f941d0 | 124 | double json_object_dotget_number (const JSON_Object *object, const char *name); /* returns 0 on fail */ |
dgabino | 0:102b50f941d0 | 125 | int json_object_dotget_boolean(const JSON_Object *object, const char *name); /* returns -1 on fail */ |
dgabino | 0:102b50f941d0 | 126 | |
dgabino | 0:102b50f941d0 | 127 | /* Functions to get available names */ |
dgabino | 0:102b50f941d0 | 128 | size_t json_object_get_count(const JSON_Object *object); |
dgabino | 0:102b50f941d0 | 129 | const char * json_object_get_name (const JSON_Object *object, size_t index); |
dgabino | 0:102b50f941d0 | 130 | |
dgabino | 0:102b50f941d0 | 131 | /* Creates new name-value pair or frees and replaces old value with a new one. |
dgabino | 0:102b50f941d0 | 132 | * json_object_set_value does not copy passed value so it shouldn't be freed afterwards. */ |
dgabino | 0:102b50f941d0 | 133 | JSON_Status json_object_set_value(JSON_Object *object, const char *name, JSON_Value *value); |
dgabino | 0:102b50f941d0 | 134 | JSON_Status json_object_set_string(JSON_Object *object, const char *name, const char *string); |
dgabino | 0:102b50f941d0 | 135 | JSON_Status json_object_set_number(JSON_Object *object, const char *name, double number); |
dgabino | 0:102b50f941d0 | 136 | JSON_Status json_object_set_boolean(JSON_Object *object, const char *name, int boolean); |
dgabino | 0:102b50f941d0 | 137 | JSON_Status json_object_set_null(JSON_Object *object, const char *name); |
dgabino | 0:102b50f941d0 | 138 | |
dgabino | 0:102b50f941d0 | 139 | /* Works like dotget functions, but creates whole hierarchy if necessary. |
dgabino | 0:102b50f941d0 | 140 | * json_object_dotset_value does not copy passed value so it shouldn't be freed afterwards. */ |
dgabino | 0:102b50f941d0 | 141 | JSON_Status json_object_dotset_value(JSON_Object *object, const char *name, JSON_Value *value); |
dgabino | 0:102b50f941d0 | 142 | JSON_Status json_object_dotset_string(JSON_Object *object, const char *name, const char *string); |
dgabino | 0:102b50f941d0 | 143 | JSON_Status json_object_dotset_number(JSON_Object *object, const char *name, double number); |
dgabino | 0:102b50f941d0 | 144 | JSON_Status json_object_dotset_boolean(JSON_Object *object, const char *name, int boolean); |
dgabino | 0:102b50f941d0 | 145 | JSON_Status json_object_dotset_null(JSON_Object *object, const char *name); |
dgabino | 0:102b50f941d0 | 146 | |
dgabino | 0:102b50f941d0 | 147 | /* Frees and removes name-value pair */ |
dgabino | 0:102b50f941d0 | 148 | JSON_Status json_object_remove(JSON_Object *object, const char *name); |
dgabino | 0:102b50f941d0 | 149 | |
dgabino | 0:102b50f941d0 | 150 | /* Works like dotget function, but removes name-value pair only on exact match. */ |
dgabino | 0:102b50f941d0 | 151 | JSON_Status json_object_dotremove(JSON_Object *object, const char *key); |
dgabino | 0:102b50f941d0 | 152 | |
dgabino | 0:102b50f941d0 | 153 | /* Removes all name-value pairs in object */ |
dgabino | 0:102b50f941d0 | 154 | JSON_Status json_object_clear(JSON_Object *object); |
dgabino | 0:102b50f941d0 | 155 | |
dgabino | 0:102b50f941d0 | 156 | /* |
dgabino | 0:102b50f941d0 | 157 | *JSON Array |
dgabino | 0:102b50f941d0 | 158 | */ |
dgabino | 0:102b50f941d0 | 159 | JSON_Value * json_array_get_value (const JSON_Array *array, size_t index); |
dgabino | 0:102b50f941d0 | 160 | const char * json_array_get_string (const JSON_Array *array, size_t index); |
dgabino | 0:102b50f941d0 | 161 | JSON_Object * json_array_get_object (const JSON_Array *array, size_t index); |
dgabino | 0:102b50f941d0 | 162 | JSON_Array * json_array_get_array (const JSON_Array *array, size_t index); |
dgabino | 0:102b50f941d0 | 163 | double json_array_get_number (const JSON_Array *array, size_t index); /* returns 0 on fail */ |
dgabino | 0:102b50f941d0 | 164 | int json_array_get_boolean(const JSON_Array *array, size_t index); /* returns -1 on fail */ |
dgabino | 0:102b50f941d0 | 165 | size_t json_array_get_count (const JSON_Array *array); |
dgabino | 0:102b50f941d0 | 166 | |
dgabino | 0:102b50f941d0 | 167 | /* Frees and removes value at given index, does nothing and returns JSONFailure if index doesn't exist. |
dgabino | 0:102b50f941d0 | 168 | * Order of values in array may change during execution. */ |
dgabino | 0:102b50f941d0 | 169 | JSON_Status json_array_remove(JSON_Array *array, size_t i); |
dgabino | 0:102b50f941d0 | 170 | |
dgabino | 0:102b50f941d0 | 171 | /* Frees and removes from array value at given index and replaces it with given one. |
dgabino | 0:102b50f941d0 | 172 | * Does nothing and returns JSONFailure if index doesn't exist. |
dgabino | 0:102b50f941d0 | 173 | * json_array_replace_value does not copy passed value so it shouldn't be freed afterwards. */ |
dgabino | 0:102b50f941d0 | 174 | JSON_Status json_array_replace_value(JSON_Array *array, size_t i, JSON_Value *value); |
dgabino | 0:102b50f941d0 | 175 | JSON_Status json_array_replace_string(JSON_Array *array, size_t i, const char* string); |
dgabino | 0:102b50f941d0 | 176 | JSON_Status json_array_replace_number(JSON_Array *array, size_t i, double number); |
dgabino | 0:102b50f941d0 | 177 | JSON_Status json_array_replace_boolean(JSON_Array *array, size_t i, int boolean); |
dgabino | 0:102b50f941d0 | 178 | JSON_Status json_array_replace_null(JSON_Array *array, size_t i); |
dgabino | 0:102b50f941d0 | 179 | |
dgabino | 0:102b50f941d0 | 180 | /* Frees and removes all values from array */ |
dgabino | 0:102b50f941d0 | 181 | JSON_Status json_array_clear(JSON_Array *array); |
dgabino | 0:102b50f941d0 | 182 | |
dgabino | 0:102b50f941d0 | 183 | /* Appends new value at the end of array. |
dgabino | 0:102b50f941d0 | 184 | * json_array_append_value does not copy passed value so it shouldn't be freed afterwards. */ |
dgabino | 0:102b50f941d0 | 185 | JSON_Status json_array_append_value(JSON_Array *array, JSON_Value *value); |
dgabino | 0:102b50f941d0 | 186 | JSON_Status json_array_append_string(JSON_Array *array, const char *string); |
dgabino | 0:102b50f941d0 | 187 | JSON_Status json_array_append_number(JSON_Array *array, double number); |
dgabino | 0:102b50f941d0 | 188 | JSON_Status json_array_append_boolean(JSON_Array *array, int boolean); |
dgabino | 0:102b50f941d0 | 189 | JSON_Status json_array_append_null(JSON_Array *array); |
dgabino | 0:102b50f941d0 | 190 | |
dgabino | 0:102b50f941d0 | 191 | /* |
dgabino | 0:102b50f941d0 | 192 | *JSON Value |
dgabino | 0:102b50f941d0 | 193 | */ |
dgabino | 0:102b50f941d0 | 194 | JSON_Value * json_value_init_object (void); |
dgabino | 0:102b50f941d0 | 195 | JSON_Value * json_value_init_array (void); |
dgabino | 0:102b50f941d0 | 196 | JSON_Value * json_value_init_string (const char *string); /* copies passed string */ |
dgabino | 0:102b50f941d0 | 197 | JSON_Value * json_value_init_number (double number); |
dgabino | 0:102b50f941d0 | 198 | JSON_Value * json_value_init_boolean(int boolean); |
dgabino | 0:102b50f941d0 | 199 | JSON_Value * json_value_init_null (void); |
dgabino | 0:102b50f941d0 | 200 | JSON_Value * json_value_deep_copy (const JSON_Value *value); |
dgabino | 0:102b50f941d0 | 201 | void json_value_free (JSON_Value *value); |
dgabino | 0:102b50f941d0 | 202 | |
dgabino | 0:102b50f941d0 | 203 | JSON_Value_Type json_value_get_type (const JSON_Value *value); |
dgabino | 0:102b50f941d0 | 204 | JSON_Object * json_value_get_object (const JSON_Value *value); |
dgabino | 0:102b50f941d0 | 205 | JSON_Array * json_value_get_array (const JSON_Value *value); |
dgabino | 0:102b50f941d0 | 206 | const char * json_value_get_string (const JSON_Value *value); |
dgabino | 0:102b50f941d0 | 207 | double json_value_get_number (const JSON_Value *value); |
dgabino | 0:102b50f941d0 | 208 | int json_value_get_boolean(const JSON_Value *value); |
dgabino | 0:102b50f941d0 | 209 | |
dgabino | 0:102b50f941d0 | 210 | /* Same as above, but shorter */ |
dgabino | 0:102b50f941d0 | 211 | JSON_Value_Type json_type (const JSON_Value *value); |
dgabino | 0:102b50f941d0 | 212 | JSON_Object * json_object (const JSON_Value *value); |
dgabino | 0:102b50f941d0 | 213 | JSON_Array * json_array (const JSON_Value *value); |
dgabino | 0:102b50f941d0 | 214 | const char * json_string (const JSON_Value *value); |
dgabino | 0:102b50f941d0 | 215 | double json_number (const JSON_Value *value); |
dgabino | 0:102b50f941d0 | 216 | int json_boolean(const JSON_Value *value); |
dgabino | 0:102b50f941d0 | 217 | |
dgabino | 0:102b50f941d0 | 218 | #ifdef __cplusplus |
dgabino | 0:102b50f941d0 | 219 | } |
dgabino | 0:102b50f941d0 | 220 | #endif |
dgabino | 0:102b50f941d0 | 221 | |
dgabino | 0:102b50f941d0 | 222 | #endif |