xref: /3.0.3-GA/platform/include/cJSON.h (revision 21dc4478)
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 cJSON__h
24#define cJSON__h
25
26#ifdef BUILDING_CJSON
27
28#if defined (__SUNPRO_C) && (__SUNPRO_C >= 0x550)
29#define CJSON_PUBLIC_API __global
30#elif defined __GNUC__
31#define CJSON_PUBLIC_API __attribute__ ((visibility("default")))
32#elif defined(_MSC_VER)
33#define CJSON_PUBLIC_API __declspec(dllexport)
34#else
35/* unknown compiler */
36#define CJSON_PUBLIC_API
37#endif
38
39#else
40
41#if defined(_MSC_VER)
42#define CJSON_PUBLIC_API __declspec(dllimport)
43#else
44#define CJSON_PUBLIC_API
45#endif
46
47#endif
48
49#ifdef __cplusplus
50extern "C"
51{
52#endif
53
54/* cJSON Types: */
55#define cJSON_False 0
56#define cJSON_True 1
57#define cJSON_NULL 2
58#define cJSON_Number 3
59#define cJSON_String 4
60#define cJSON_Array 5
61#define cJSON_Object 6
62
63#define cJSON_IsReference 256
64
65/* The cJSON structure: */
66typedef struct cJSON {
67        struct cJSON *next,*prev; /* next/prev allow you to walk
68                                     array/object
69                                     chains. Alternatively, use
70                                     GetArraySize/GetArrayItem/GetObjectItem */
71        struct cJSON *child; /* An array or object item will have a
72                                child pointer pointing to a chain of
73                                the items in the array/object. */
74
75        int type; /* The type of the item, as above. */
76
77        char *valuestring; /* The item's string, if type==cJSON_String */
78        int valueint; /* The item's number, if type==cJSON_Number */
79        double valuedouble; /* The item's number, if type==cJSON_Number */
80
81        char *string; /* The item's name string, if this item is the
82                         child of, or is in the list of subitems of an
83                         object. */
84} cJSON;
85
86typedef struct cJSON_Hooks {
87      void *(*malloc_fn)(size_t sz);
88      void (*free_fn)(void *ptr);
89} cJSON_Hooks;
90
91/* Supply malloc, realloc and free functions to cJSON */
92CJSON_PUBLIC_API
93extern void cJSON_InitHooks(cJSON_Hooks* hooks);
94
95
96/* Supply a block of JSON, and this returns a cJSON object you can
97   interrogate. Call cJSON_Delete when finished. */
98CJSON_PUBLIC_API
99extern cJSON *cJSON_Parse(const char *value);
100/* Render a cJSON entity to text for transfer/storage. Free the char*
101   when finished. */
102CJSON_PUBLIC_API
103extern char  *cJSON_Print(cJSON *item);
104/* Render a cJSON entity to text for transfer/storage without any
105   formatting. Free the char* when finished. */
106CJSON_PUBLIC_API
107extern char  *cJSON_PrintUnformatted(cJSON *item);
108/* Release the memory returned by cJSON_Print and cJSON_PrintUnformatted */
109CJSON_PUBLIC_API
110extern void   cJSON_Free(char *ptr);
111/* Delete a cJSON entity and all subentities. */
112CJSON_PUBLIC_API
113extern void   cJSON_Delete(cJSON *c);
114
115/* Returns the number of items in an array (or object). */
116CJSON_PUBLIC_API
117extern int    cJSON_GetArraySize(cJSON *array);
118/* Retrieve item number "item" from array "array". Returns NULL if
119   unsuccessful. */
120CJSON_PUBLIC_API
121extern cJSON *cJSON_GetArrayItem(cJSON *array,int item);
122/* Get item "string" from object. Case insensitive. */
123CJSON_PUBLIC_API
124extern cJSON *cJSON_GetObjectItem(cJSON *object,const char *string);
125
126/* These calls create a cJSON item of the appropriate type. */
127CJSON_PUBLIC_API
128extern cJSON *cJSON_CreateNull(void);
129CJSON_PUBLIC_API
130extern cJSON *cJSON_CreateTrue(void);
131CJSON_PUBLIC_API
132extern cJSON *cJSON_CreateFalse(void);
133CJSON_PUBLIC_API
134extern cJSON *cJSON_CreateNumber(double num);
135CJSON_PUBLIC_API
136extern cJSON *cJSON_CreateString(const char *string);
137CJSON_PUBLIC_API
138extern cJSON *cJSON_CreateArray(void);
139CJSON_PUBLIC_API
140extern cJSON *cJSON_CreateObject(void);
141
142/* These utilities create an Array of count items. */
143CJSON_PUBLIC_API
144extern cJSON *cJSON_CreateIntArray(int *numbers,int count);
145CJSON_PUBLIC_API
146extern cJSON *cJSON_CreateFloatArray(float *numbers,int count);
147CJSON_PUBLIC_API
148extern cJSON *cJSON_CreateDoubleArray(double *numbers,int count);
149CJSON_PUBLIC_API
150extern cJSON *cJSON_CreateStringArray(const char **strings,int count);
151
152/* Append item to the specified array/object. */
153CJSON_PUBLIC_API
154extern void cJSON_AddItemToArray(cJSON *array, cJSON *item);
155CJSON_PUBLIC_API
156extern void cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item);
157/* Append reference to item to the specified array/object. Use this
158   when you want to add an existing cJSON to a new cJSON, but don't
159   want to corrupt your existing cJSON. */
160CJSON_PUBLIC_API
161extern void cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item);
162CJSON_PUBLIC_API
163extern void cJSON_AddItemReferenceToObject(cJSON *object,const char *string,cJSON *item);
164
165/* Remove/Detatch items from Arrays/Objects. */
166CJSON_PUBLIC_API
167extern cJSON *cJSON_DetachItemFromArray(cJSON *array,int which);
168CJSON_PUBLIC_API
169extern void   cJSON_DeleteItemFromArray(cJSON *array,int which);
170CJSON_PUBLIC_API
171extern cJSON *cJSON_DetachItemFromObject(cJSON *object,const char *string);
172CJSON_PUBLIC_API
173extern void   cJSON_DeleteItemFromObject(cJSON *object,const char *string);
174
175/* Update array items. */
176CJSON_PUBLIC_API
177extern void cJSON_ReplaceItemInArray(cJSON *array,int which,cJSON *newitem);
178CJSON_PUBLIC_API
179extern void cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem);
180
181#define cJSON_AddNullToObject(object,name)      cJSON_AddItemToObject(object, name, cJSON_CreateNull())
182#define cJSON_AddTrueToObject(object,name)      cJSON_AddItemToObject(object, name, cJSON_CreateTrue())
183#define cJSON_AddFalseToObject(object,name)             cJSON_AddItemToObject(object, name, cJSON_CreateFalse())
184#define cJSON_AddNumberToObject(object,name,n)  cJSON_AddItemToObject(object, name, cJSON_CreateNumber(n))
185#define cJSON_AddStringToObject(object,name,s)  cJSON_AddItemToObject(object, name, cJSON_CreateString(s))
186
187#ifdef __cplusplus
188}
189#endif
190
191#endif
192