1 /* 2 * Copyright (C) 2015-2018 Alibaba Group Holding Limited 3 */ 4 5 #ifndef _INFRA_CJSON_H_ 6 #define _INFRA_CJSON_H_ 7 8 /* cJSON Types: */ 9 #define cJSON_Invalid (0) 10 #define cJSON_False (1 << 0) 11 #define cJSON_True (1 << 1) 12 #define cJSON_NULL (1 << 2) 13 #define cJSON_Number (1 << 3) 14 #define cJSON_String (1 << 4) 15 #define cJSON_Array (1 << 5) 16 #define cJSON_Object (1 << 6) 17 #define cJSON_Raw (1 << 7) /* raw json */ 18 19 #ifndef LITE_CJSON_NESTING_LIMIT 20 #define LITE_CJSON_NESTING_LIMIT 1000 21 #endif 22 23 /* The cJSON structure: */ 24 typedef struct lite_cjson_st { 25 /* The type of the item, as above. */ 26 int type; 27 28 /* The item's string, if type==cJSON_String and type == cJSON_Raw */ 29 char *value; 30 int value_length; 31 32 /* The item's size, if type == cJSON_Array and type == cJSON_Object */ 33 int size; 34 35 double value_double; 36 int value_int; 37 } lite_cjson_t; 38 39 int lite_cjson_parse(const char *src, int src_len, lite_cjson_t *lite); 40 41 int lite_cjson_is_false(lite_cjson_t *lite); 42 int lite_cjson_is_true(lite_cjson_t *lite); 43 int lite_cjson_is_null(lite_cjson_t *lite); 44 int lite_cjson_is_number(lite_cjson_t *lite); 45 int lite_cjson_is_string(lite_cjson_t *lite); 46 int lite_cjson_is_array(lite_cjson_t *lite); 47 int lite_cjson_is_object(lite_cjson_t *lite); 48 49 int lite_cjson_array_item(lite_cjson_t *lite, int index, 50 lite_cjson_t *lite_item); 51 int lite_cjson_object_item(lite_cjson_t *lite, const char *key, int key_len, 52 lite_cjson_t *lite_item); 53 int lite_cjson_object_item_by_index(lite_cjson_t *lite, int index, 54 lite_cjson_t *lite_item_key, 55 lite_cjson_t *lite_item_value); 56 57 /*** lite_cjson create, add and print ***/ 58 typedef int cJSON_bool; 59 60 #define cJSON_IsReference 256 61 #define cJSON_StringIsConst 512 62 63 /* The cJSON structure: */ 64 typedef struct lite_cjson_item_t { 65 struct lite_cjson_item_t *next, 66 *prev; /* next/prev allow you to walk array/object chains. 67 Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */ 68 struct lite_cjson_item_t 69 *child; /* An array or object item will have a child pointer pointing to 70 a chain of the items in the array/object. */ 71 72 int type; /* The type of the item, as above. */ 73 74 char *valuestring; /* The item's string, if type==cJSON_String */ 75 int valueint; /* The item's number, if type==cJSON_Number */ 76 double valuedouble; /* The item's number, if type==cJSON_Number */ 77 78 char *string; /* The item's name string, if this item is the child of, or is 79 in the list of subitems of an object. */ 80 } lite_cjson_item_t; 81 82 typedef struct { 83 void *(*malloc_fn)(unsigned int sz); 84 void (*free_fn)(void *ptr); 85 } lite_cjson_hooks; 86 87 void lite_cjson_init_hooks(lite_cjson_hooks *hooks); 88 89 /* Render a lite_cjson_item_t entity to text for transfer/storage. Free the 90 * char* when finished. */ 91 char *lite_cjson_print(lite_cjson_item_t *item); 92 char *lite_cjson_print_unformatted(lite_cjson_item_t *item); 93 94 /* Delete a lite_cjson_item_t entity and all subentities. */ 95 void lite_cjson_delete(lite_cjson_item_t *item); 96 97 /* Append item to specific object */ 98 void lite_cjson_add_item_to_array(lite_cjson_item_t *array, 99 lite_cjson_item_t *item); 100 void lite_cjson_add_item_to_object(lite_cjson_item_t *object, 101 const char *string, lite_cjson_item_t *item); 102 103 /* These calls create a lite_cjson_item_t item of the appropriate type. */ 104 lite_cjson_item_t *lite_cjson_create_null(void); 105 lite_cjson_item_t *lite_cjson_create_true(void); 106 lite_cjson_item_t *lite_cjson_create_false(void); 107 lite_cjson_item_t *lite_cjson_create_bool(int b); 108 lite_cjson_item_t *lite_cjson_create_number(double num); 109 lite_cjson_item_t *lite_cjson_create_string(const char *string); 110 lite_cjson_item_t *lite_cjson_create_array(void); 111 lite_cjson_item_t *lite_cjson_create_object(void); 112 113 /* These utilities create an Array of count items. */ 114 lite_cjson_item_t *lite_cjson_create_intArray(const int *numbers, int count); 115 lite_cjson_item_t *lite_cjson_create_floatArray(const float *numbers, 116 int count); 117 lite_cjson_item_t *lite_cjson_create_doubleArray(const double *numbers, 118 int count); 119 lite_cjson_item_t *lite_cjson_create_stringArray(const char **strings, 120 int count); 121 122 /* Macros for creating things quickly. */ 123 #define lite_cjson_add_null_to_object(object, name) \ 124 lite_cjson_add_item_to_object(object, name, lite_cjson_create_null()) 125 #define lite_cjson_add_true_to_object(object, name) \ 126 lite_cjson_add_item_to_object(object, name, lite_cjson_create_true()) 127 #define lite_cjson_add_false_to_object(object, name) \ 128 lite_cjson_add_item_to_object(object, name, lite_cjson_create_false()) 129 #define lite_cjson_add_bool_to_object(object, name, b) \ 130 lite_cjson_add_item_to_object(object, name, lite_cjson_create_bool(b)) 131 #define lite_cjson_add_number_to_object(object, name, n) \ 132 lite_cjson_add_item_to_object(object, name, lite_cjson_create_number(n)) 133 #define lite_cjson_add_string_to_object(object, name, s) \ 134 lite_cjson_add_item_to_object(object, name, lite_cjson_create_string(s)) 135 #endif 136