TuyaOS
ty_cJSON.h
1/*
2 Copyright (c) 2009 Dave Gamble
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21*/
22
23#ifndef ty_cJSON__h
24#define ty_cJSON__h
25
26#ifdef __cplusplus
27extern "C"
28{
29#endif
30
31#include <stdlib.h>
32
33/* ty_cJSON Types: */
34#define ty_cJSON_False 0
35#define ty_cJSON_True 1
36#define ty_cJSON_NULL 2
37#define ty_cJSON_Number 3
38#define ty_cJSON_String 4
39#define ty_cJSON_Array 5
40#define ty_cJSON_Object 6
41
42#define ty_cJSON_IsReference 256
43
44/* The ty_cJSON structure: */
45typedef struct ty_cJSON {
46 struct ty_cJSON *next, *prev; /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
47 struct ty_cJSON *child; /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
48
49 int type; /* The type of the item, as above. */
50
51 char *valuestring; /* The item's string, if type==ty_cJSON_String */
52 int valueint; /* The item's number, if type==ty_cJSON_Number */
53 double valuedouble; /* The item's number, if type==ty_cJSON_Number */
54
55 char *string; /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
56} ty_cJSON;
57
58/* Supply a block of JSON, and this returns a ty_cJSON object you can interrogate. Call ty_cJSON_Delete when finished. */
59extern ty_cJSON *ty_cJSON_Parse(const char *value);
60/* Render a ty_cJSON entity to text for transfer/storage. Free the char* when finished. */
61extern char *ty_cJSON_Print(ty_cJSON *item);
62/* Render a ty_cJSON entity to text for transfer/storage without any formatting. Free the char* when finished. */
63extern char *ty_cJSON_PrintUnformatted(ty_cJSON *item);
64extern void ty_cJSON_FreeBuffer(char *buffer);
65/* Delete a ty_cJSON entity and all subentities. */
66extern void ty_cJSON_Delete(ty_cJSON *c);
67
68/* Returns the number of items in an array (or object). */
69extern int ty_cJSON_GetArraySize(ty_cJSON *array);
70/* Retrieve item number "item" from array "array". Returns NULL if unsuccessful. */
71extern ty_cJSON *ty_cJSON_GetArrayItem(ty_cJSON *array, int item);
72/* Get item "string" from object. Case insensitive. */
73extern ty_cJSON *ty_cJSON_GetObjectItem(ty_cJSON *object, const char *string);
74
75/* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when ty_cJSON_Parse() returns 0. 0 when ty_cJSON_Parse() succeeds. */
76extern const char *ty_cJSON_GetErrorPtr(void);
77
78/* These calls create a ty_cJSON item of the appropriate type. */
79extern ty_cJSON *ty_cJSON_CreateNull(void);
80extern ty_cJSON *ty_cJSON_CreateTrue(void);
81extern ty_cJSON *ty_cJSON_CreateFalse(void);
82extern ty_cJSON *ty_cJSON_CreateBool(int b);
83extern ty_cJSON *ty_cJSON_CreateNumber(double num);
84extern ty_cJSON *ty_cJSON_CreateString(const char *string);
85extern ty_cJSON *ty_cJSON_CreateArray(void);
86extern ty_cJSON *ty_cJSON_CreateObject(void);
87
88/* These utilities create an Array of count items. */
89extern ty_cJSON *ty_cJSON_CreateIntArray(const int *numbers, int count);
90extern ty_cJSON *ty_cJSON_CreateFloatArray(const float *numbers, int count);
91extern ty_cJSON *ty_cJSON_CreateDoubleArray(const double *numbers, int count);
92extern ty_cJSON *ty_cJSON_CreateStringArray(const char **strings, int count);
93
94/* Append item to the specified array/object. */
95extern void ty_cJSON_AddItemToArray(ty_cJSON *array, ty_cJSON *item);
96extern void ty_cJSON_AddItemToObject(ty_cJSON *object, const char *string, ty_cJSON *item);
97/* Append reference to item to the specified array/object. Use this when you want to add an existing ty_cJSON to a new ty_cJSON, but don't want to corrupt your existing ty_cJSON. */
98extern void ty_cJSON_AddItemReferenceToArray(ty_cJSON *array, ty_cJSON *item);
99extern void ty_cJSON_AddItemReferenceToObject(ty_cJSON *object, const char *string, ty_cJSON *item);
100
101/* Remove/Detatch items from Arrays/Objects. */
102extern ty_cJSON *ty_cJSON_DetachItemFromArray(ty_cJSON *array, int which);
103extern void ty_cJSON_DeleteItemFromArray(ty_cJSON *array, int which);
104extern ty_cJSON *ty_cJSON_DetachItemFromObject(ty_cJSON *object, const char *string);
105extern void ty_cJSON_DeleteItemFromObject(ty_cJSON *object, const char *string);
106
107/* Update array items. */
108extern void ty_cJSON_ReplaceItemInArray(ty_cJSON *array, int which, ty_cJSON *newitem);
109extern void ty_cJSON_ReplaceItemInObject(ty_cJSON *object, const char *string, ty_cJSON *newitem);
110
111/* Duplicate a ty_cJSON item */
112extern ty_cJSON *ty_cJSON_Duplicate(ty_cJSON *item, int recurse);
113/* Duplicate will create a new, identical ty_cJSON item to the one you pass, in new memory that will
114need to be released. With recurse!=0, it will duplicate any children connected to the item.
115The item->next and ->prev pointers are always zero on return from Duplicate. */
116
117/* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */
118extern ty_cJSON *ty_cJSON_ParseWithOpts(const char *value, const char **return_parse_end, int require_null_terminated);
119
120extern void ty_cJSON_Minify(char *json);
121
122/*extern for p2p libuv */
123extern ty_cJSON * ty_cJSON_GetObjectItemCaseSensitive(ty_cJSON *object, const char *string);
124extern int ty_cJSON_IsObject(const ty_cJSON * object);
125extern int ty_cJSON_IsArray(const ty_cJSON * object);
126extern int ty_cJSON_IsString(const ty_cJSON * object);
127extern int ty_cJSON_IsNumber(const ty_cJSON * object);
128extern int ty_cJSON_IsBool(const ty_cJSON * object);
129
130
131
132/* Macros for creating things quickly. */
133#define ty_cJSON_AddNullToObject(object,name) ty_cJSON_AddItemToObject(object, name, ty_cJSON_CreateNull())
134#define ty_cJSON_AddTrueToObject(object,name) ty_cJSON_AddItemToObject(object, name, ty_cJSON_CreateTrue())
135#define ty_cJSON_AddFalseToObject(object,name) ty_cJSON_AddItemToObject(object, name, ty_cJSON_CreateFalse())
136#define ty_cJSON_AddBoolToObject(object,name,b) ty_cJSON_AddItemToObject(object, name, ty_cJSON_CreateBool(b))
137#define ty_cJSON_AddNumberToObject(object,name,n) ty_cJSON_AddItemToObject(object, name, ty_cJSON_CreateNumber(n))
138#define ty_cJSON_AddStringToObject(object,name,s) ty_cJSON_AddItemToObject(object, name, ty_cJSON_CreateString(s))
139
140/* When assigning an integer value, it needs to be propagated to valuedouble too. */
141#define ty_cJSON_SetIntValue(object,val) ((object)?(object)->valueint=(object)->valuedouble=(val):(val))
142
143/* Macro for iterating over an array */
144#define ty_cJSON_ArrayForEach(pos, head) for(pos = (head)->child; pos != NULL; pos = pos->next)
145
146#ifdef __cplusplus
147}
148#endif
149
150#endif
Definition: ty_cJSON.h:45