16e7c7370SDerry/*
26e7c7370SDerry  Copyright (c) 2009 Dave Gamble
36e7c7370SDerry
46e7c7370SDerry  Permission is hereby granted, free of charge, to any person obtaining a copy
56e7c7370SDerry  of this software and associated documentation files (the "Software"), to deal
66e7c7370SDerry  in the Software without restriction, including without limitation the rights
76e7c7370SDerry  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
86e7c7370SDerry  copies of the Software, and to permit persons to whom the Software is
96e7c7370SDerry  furnished to do so, subject to the following conditions:
106e7c7370SDerry
116e7c7370SDerry  The above copyright notice and this permission notice shall be included in
126e7c7370SDerry  all copies or substantial portions of the Software.
136e7c7370SDerry
146e7c7370SDerry  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
156e7c7370SDerry  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
166e7c7370SDerry  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
176e7c7370SDerry  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
186e7c7370SDerry  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
196e7c7370SDerry  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
206e7c7370SDerry  THE SOFTWARE.
216e7c7370SDerry*/
226e7c7370SDerry
236e7c7370SDerry#ifndef cJSON__h
246e7c7370SDerry#define cJSON__h
256e7c7370SDerry#include <linux/slab.h>
266e7c7370SDerry
276e7c7370SDerry// cJSON Types:
286e7c7370SDerry#define cJSON_False 0
296e7c7370SDerry#define cJSON_True 1
306e7c7370SDerry#define cJSON_NULL 2
316e7c7370SDerry#define cJSON_Number 3
326e7c7370SDerry#define cJSON_String 4
336e7c7370SDerry#define cJSON_Array 5
346e7c7370SDerry#define cJSON_Object 6
356e7c7370SDerry
366e7c7370SDerry// The cJSON structure:
376e7c7370SDerrytypedef struct cJSON {
386e7c7370SDerry	struct cJSON *next,*prev;	// next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem
396e7c7370SDerry	struct cJSON *child;		// An array or object item will have a child pointer pointing to a chain of the items in the array/object.
406e7c7370SDerry
416e7c7370SDerry	int type;					// The type of the item, as above.
426e7c7370SDerry
436e7c7370SDerry	char *valuestring;			// The item's string, if type==cJSON_String
446e7c7370SDerry	int valueint;				// The item's number, if type==cJSON_Number
456e7c7370SDerry	char *string;				// The item's name string, if this item is the child of, or is in the list of subitems of an object.
466e7c7370SDerry} cJSON;
476e7c7370SDerry
486e7c7370SDerrytypedef struct cJSON_Hooks {
496e7c7370SDerry      void *(*malloc_fn)(size_t sz);
506e7c7370SDerry      void *(*realloc_fn)(void *ptr, size_t sz);
516e7c7370SDerry      void (*free_fn)(void *ptr);
526e7c7370SDerry} cJSON_Hooks;
536e7c7370SDerry
546e7c7370SDerry// Supply malloc, realloc and free functions to cJSON
556e7c7370SDerryextern void cJSON_InitHooks(cJSON_Hooks* hooks);
566e7c7370SDerry
576e7c7370SDerry
586e7c7370SDerry// Supply a block of JSON, and this returns a cJSON object you can interrogate. Call cJSON_Delete when finished.
596e7c7370SDerryextern cJSON *cJSON_Parse(const char *value);
606e7c7370SDerry// Render a cJSON entity to text for transfer/storage. Free the char* when finished.
616e7c7370SDerryextern char  *cJSON_Print(cJSON *item);
626e7c7370SDerry// Delete a cJSON entity and all subentities.
636e7c7370SDerryextern void   cJSON_Delete(cJSON *c);
646e7c7370SDerry
656e7c7370SDerry// Returns the number of items in an array (or object).
666e7c7370SDerryextern int	  cJSON_GetArraySize(cJSON *array);
676e7c7370SDerry// Retrieve item number "item" from array "array". Returns NULL if unsuccessful.
686e7c7370SDerryextern cJSON *cJSON_GetArrayItem(cJSON *array,int item);
696e7c7370SDerry// Get item "string" from object. Case insensitive.
706e7c7370SDerryextern cJSON *cJSON_GetObjectItem(cJSON *object,const char *string);
716e7c7370SDerry
726e7c7370SDerry// These calls create a cJSON item of the appropriate type.
736e7c7370SDerryextern cJSON *cJSON_CreateNull(void);
746e7c7370SDerryextern cJSON *cJSON_CreateTrue(void);
756e7c7370SDerryextern cJSON *cJSON_CreateFalse(void);
766e7c7370SDerryextern cJSON *cJSON_CreateNumber(int num);
776e7c7370SDerryextern cJSON *cJSON_CreateString(const char *string);
786e7c7370SDerryextern cJSON *cJSON_CreateArray(void);
796e7c7370SDerryextern cJSON *cJSON_CreateObject(void);
800055281cSdestanextern void cJSON_Minify(char *json);
816e7c7370SDerry// These utilities create an Array of count items.
826e7c7370SDerryextern cJSON *cJSON_CreateIntArray(int *numbers,int count);
836e7c7370SDerry
846e7c7370SDerry// Append item to the specified array/object.
856e7c7370SDerryextern void   cJSON_AddItemToArray(cJSON *array, cJSON *item);
866e7c7370SDerryextern void	  cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item);
876e7c7370SDerry
886e7c7370SDerry#define cJSON_AddNullToObject(object,name)	cJSON_AddItemToObject(object, name, cJSON_CreateNull())
896e7c7370SDerry#define cJSON_AddTrueToObject(object,name)	cJSON_AddItemToObject(object, name, cJSON_CreateTrue())
906e7c7370SDerry#define cJSON_AddFalseToObject(object,name)		cJSON_AddItemToObject(object, name, cJSON_CreateFalse())
916e7c7370SDerry#define cJSON_AddNumberToObject(object,name,n)	cJSON_AddItemToObject(object, name, cJSON_CreateNumber(n))
926e7c7370SDerry#define cJSON_AddStringToObject(object,name,s)	cJSON_AddItemToObject(object, name, cJSON_CreateString(s))
936e7c7370SDerry
946e7c7370SDerry#endif
95