ucx/cx/json.h

changeset 31
287484519844
parent 30
d33eaaec15da
equal deleted inserted replaced
30:d33eaaec15da 31:287484519844
41 #include "string.h" 41 #include "string.h"
42 #include "buffer.h" 42 #include "buffer.h"
43 #include "array_list.h" 43 #include "array_list.h"
44 #include "map.h" 44 #include "map.h"
45 45
46 #include <string.h>
47
48 #ifdef __cplusplus 46 #ifdef __cplusplus
49 extern "C" { 47 extern "C" {
50 #endif 48 #endif
51 49
52 50
183 * Type alias for the JSON value struct. 181 * Type alias for the JSON value struct.
184 */ 182 */
185 typedef struct cx_json_value_s CxJsonValue; 183 typedef struct cx_json_value_s CxJsonValue;
186 184
187 /** 185 /**
188 * Type alias for the JSON array struct.
189 */
190 typedef struct cx_json_array_s CxJsonArray;
191 /**
192 * Type alias for the map representing a JSON object. 186 * Type alias for the map representing a JSON object.
193 * The map contains pointers of type @c CxJsonValue. 187 * The map contains pointers of type @c CxJsonValue.
194 */ 188 */
195 typedef CxMap* CxJsonObject; 189 typedef CxMap* CxJsonObject;
196 /** 190 /**
207 typedef double CxJsonNumber; 201 typedef double CxJsonNumber;
208 /** 202 /**
209 * Type alias for a JSON literal. 203 * Type alias for a JSON literal.
210 */ 204 */
211 typedef enum cx_json_literal CxJsonLiteral; 205 typedef enum cx_json_literal CxJsonLiteral;
212
213 /**
214 * JSON array structure.
215 */
216 struct cx_json_array_s {
217 /**
218 * The array data.
219 */
220 CX_ARRAY_DECLARE(CxJsonValue*, data);
221 };
222 206
223 /** 207 /**
224 * Structure for a JSON value. 208 * Structure for a JSON value.
225 */ 209 */
226 struct cx_json_value_s { 210 struct cx_json_value_s {
241 */ 225 */
242 union { 226 union {
243 /** 227 /**
244 * The array data if the type is #CX_JSON_ARRAY. 228 * The array data if the type is #CX_JSON_ARRAY.
245 */ 229 */
246 CxJsonArray array; 230 CX_ARRAY(CxJsonValue*, array);
247 /** 231 /**
248 * The object data if the type is #CX_JSON_OBJECT. 232 * The object data if the type is #CX_JSON_OBJECT.
249 */ 233 */
250 CxJsonObject object; 234 CxJsonObject object;
251 /** 235 /**
296 struct cx_json_s { 280 struct cx_json_s {
297 /** 281 /**
298 * The allocator used for produced JSON values. 282 * The allocator used for produced JSON values.
299 */ 283 */
300 const CxAllocator *allocator; 284 const CxAllocator *allocator;
285
301 /** 286 /**
302 * The input buffer. 287 * The input buffer.
303 */ 288 */
304 CxBuffer buffer; 289 CxBuffer buffer;
305 290
325 cxmutstr uncompleted_member_name; 310 cxmutstr uncompleted_member_name;
326 311
327 /** 312 /**
328 * State stack. 313 * State stack.
329 */ 314 */
330 CX_ARRAY_DECLARE_SIZED(int, states, unsigned); 315 CX_ARRAY(int, states);
331 316
332 /** 317 /**
333 * Value buffer stack. 318 * Value buffer stack.
334 */ 319 */
335 CX_ARRAY_DECLARE_SIZED(CxJsonValue*, vbuf, unsigned); 320 CX_ARRAY(CxJsonValue*, vbuf);
336 321
337 /** 322 /**
338 * Internally reserved memory for the state stack. 323 * Internally reserved memory for the state stack.
339 */ 324 */
340 int states_internal[8]; 325 int states_internal[8];
470 * @retval non-zero when no or not all data could be written 455 * @retval non-zero when no or not all data could be written
471 */ 456 */
472 cx_attr_nonnull_arg(1, 2, 3) 457 cx_attr_nonnull_arg(1, 2, 3)
473 CX_EXPORT int cxJsonWrite(void* target, const CxJsonValue* value, 458 CX_EXPORT int cxJsonWrite(void* target, const CxJsonValue* value,
474 cx_write_func wfunc, const CxJsonWriter* settings); 459 cx_write_func wfunc, const CxJsonWriter* settings);
460
461
462 /**
463 * Produces a compact string representation of the specified JSON value.
464 *
465 * @param allocator the allocator for the string
466 * @param value the JSON value
467 * @return the produced string
468 * @see cxJsonWrite()
469 * @see cxJsonWriterCompact()
470 * @see cxJsonToPrettyString()
471 */
472 cx_attr_nonnull_arg(2)
473 CX_EXPORT cxmutstr cxJsonToString(const CxAllocator *allocator, CxJsonValue *value);
474
475 /**
476 * Produces a pretty string representation of the specified JSON value.
477 *
478 * @param allocator the allocator for the string
479 * @param value the JSON value
480 * @return the produced string
481 * @see cxJsonWrite()
482 * @see cxJsonWriterPretty()
483 * @see cxJsonToString()
484 */
485 cx_attr_nonnull_arg(2)
486 CX_EXPORT cxmutstr cxJsonToPrettyString(const CxAllocator *allocator, CxJsonValue *value);
475 487
476 /** 488 /**
477 * Initializes the JSON interface. 489 * Initializes the JSON interface.
478 * 490 *
479 * @param json the JSON interface 491 * @param json the JSON interface
599 CX_EXPORT CxJsonValue* cxJsonCreateObj(const CxAllocator* allocator); 611 CX_EXPORT CxJsonValue* cxJsonCreateObj(const CxAllocator* allocator);
600 612
601 /** 613 /**
602 * Creates a new (empty) JSON array. 614 * Creates a new (empty) JSON array.
603 * 615 *
616 * Optionally, this function already allocates memory with the given capacity.
617 *
604 * @param allocator the allocator to use 618 * @param allocator the allocator to use
619 * @param capacity optional capacity or zero if it's unknown how many elements the array will have
605 * @return the new JSON array or @c NULL if allocation fails 620 * @return the new JSON array or @c NULL if allocation fails
606 * @see cxJsonObjPutArr() 621 * @see cxJsonObjPutArr()
607 * @see cxJsonArrAddValues() 622 * @see cxJsonArrAddValues()
608 */ 623 */
609 cx_attr_nodiscard 624 cx_attr_nodiscard
610 CX_EXPORT CxJsonValue* cxJsonCreateArr(const CxAllocator* allocator); 625 CX_EXPORT CxJsonValue* cxJsonCreateArr(const CxAllocator* allocator, size_t capacity);
611 626
612 /** 627 /**
613 * Creates a new JSON number value. 628 * Creates a new JSON number value.
614 * 629 *
615 * @param allocator the allocator to use 630 * @param allocator the allocator to use
811 * 826 *
812 * Internal function - use cxJsonObjPutArr(). 827 * Internal function - use cxJsonObjPutArr().
813 * 828 *
814 * @param obj the target JSON object 829 * @param obj the target JSON object
815 * @param name the name of the new value 830 * @param name the name of the new value
831 * @param capacity optional initial capacity
816 * @return the new value or @c NULL if allocation fails 832 * @return the new value or @c NULL if allocation fails
817 * @see cxJsonObjPut() 833 * @see cxJsonObjPut()
818 * @see cxJsonCreateArr() 834 * @see cxJsonCreateArr()
819 */ 835 */
820 cx_attr_nonnull 836 cx_attr_nonnull
821 CX_EXPORT CxJsonValue* cx_json_obj_put_arr(CxJsonValue* obj, cxstring name); 837 CX_EXPORT CxJsonValue* cx_json_obj_put_arr(CxJsonValue* obj, cxstring name, size_t capacity);
822 838
823 /** 839 /**
824 * Creates a new JSON array and adds it to an object. 840 * Creates a new JSON array and adds it to an object.
825 * 841 *
826 * @param obj (@c CxJsonValue*) the target JSON object 842 * @param obj (@c CxJsonValue*) the target JSON object
827 * @param name (any string) the name of the new value 843 * @param name (any string) the name of the new value
844 * @param capacity (@c size_t) optional initial capacity
828 * @return (@c CxJsonValue*) the new value or @c NULL if allocation fails 845 * @return (@c CxJsonValue*) the new value or @c NULL if allocation fails
829 * @see cxJsonObjPut() 846 * @see cxJsonObjPut()
830 * @see cxJsonCreateArr() 847 * @see cxJsonCreateArr()
831 */ 848 */
832 #define cxJsonObjPutArr(obj, name) cx_json_obj_put_arr(obj, cx_strcast(name)) 849 #define cxJsonObjPutArr(obj, name, capacity) cx_json_obj_put_arr(obj, cx_strcast(name), capacity)
833 850
834 /** 851 /**
835 * Creates a new JSON number and adds it to an object. 852 * Creates a new JSON number and adds it to an object.
836 * 853 *
837 * Internal function - use cxJsonObjPutNumber(). 854 * Internal function - use cxJsonObjPutNumber().
1209 * @return the size of the array 1226 * @return the size of the array
1210 * @see cxJsonIsArray() 1227 * @see cxJsonIsArray()
1211 */ 1228 */
1212 cx_attr_nonnull 1229 cx_attr_nonnull
1213 CX_INLINE size_t cxJsonArrSize(const CxJsonValue *value) { 1230 CX_INLINE size_t cxJsonArrSize(const CxJsonValue *value) {
1214 return value->array.data_size; 1231 return value->array.size;
1215 } 1232 }
1216 1233
1217 /** 1234 /**
1218 * Returns an element from a JSON array. 1235 * Returns an element from a JSON array.
1219 * 1236 *
1337 * @return the value corresponding to the key or @c NULL when the key is not part of the object 1354 * @return the value corresponding to the key or @c NULL when the key is not part of the object
1338 * @see cxJsonIsObject() 1355 * @see cxJsonIsObject()
1339 */ 1356 */
1340 #define cxJsonObjRemove(value, name) cx_json_obj_remove(value, cx_strcast(name)) 1357 #define cxJsonObjRemove(value, name) cx_json_obj_remove(value, cx_strcast(name))
1341 1358
1359 /**
1360 * Performs a deep comparison of two JSON values.
1361 *
1362 * The order of object members is ignored during comparison.
1363 *
1364 * @param json the JSON value
1365 * @param other the other JSON value that the JSON value is compared to
1366 * @retval zero the values are equal (except for ordering of object members)
1367 * @retval non-zero the values differ
1368 */
1369 CX_EXPORT int cxJsonCompare(const CxJsonValue *json, const CxJsonValue *other);
1370
1371
1372 /**
1373 * Creates a deep copy of the specified JSON value.
1374 *
1375 * If you need a @c cx_clone_func compatible version, see cxJsonCloneFunc().
1376 *
1377 * @note when you are cloning @c NULL, you will get a pointer to a statically
1378 * allocated value which represents nothing.
1379 *
1380 * @param value the value to be cloned
1381 * @param allocator the allocator for the new value
1382 * @return the new value or @c NULL if any allocation was unsuccessful
1383 * @see cxJsonCloneFunc()
1384 */
1385 cx_attr_nodiscard
1386 CX_EXPORT CxJsonValue* cxJsonClone(const CxJsonValue* value,
1387 const CxAllocator* allocator);
1388
1389
1390 /**
1391 * A @c cx_clone_func compatible version of cxJsonClone().
1392 *
1393 * Internal function - use cxJsonCloneFunc() to get a properly casted function pointer.
1394 *
1395 * @param target the target memory or @c NULL
1396 * @param source the value to be cloned
1397 * @param allocator the allocator for the new value
1398 * @param data unused
1399 * @return the new value or @c NULL if any allocation was unsuccessful
1400 * @see cxJsonClone()
1401 */
1402 cx_attr_nodiscard
1403 CX_EXPORT CxJsonValue* cx_json_clone_func(
1404 CxJsonValue* target, const CxJsonValue* source,
1405 const CxAllocator* allocator, void *data);
1406
1407 /**
1408 * A @c cx_clone_func compatible version of cxJsonClone().
1409 *
1410 * @param target (@c CxJsonValue*) the target memory or @c NULL
1411 * @param source (@c CxJsonValue*) the value to be cloned
1412 * @param allocator (@c CxAllocator*) the allocator for the new value
1413 * @param data unused
1414 * @return the new value or @c NULL if any allocation was unsuccessful
1415 * @see cxJsonClone()
1416 */
1417 #define cxJsonCloneFunc ((cx_clone_func) cx_json_clone_func)
1418
1342 #ifdef __cplusplus 1419 #ifdef __cplusplus
1343 } 1420 }
1344 #endif 1421 #endif
1345 1422
1346 #endif /* UCX_JSON_H */ 1423 #endif /* UCX_JSON_H */

mercurial