/*
  Copyright (c) 2009 Dave Gamble
 
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:
 
  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.
 
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
*/

#ifndef XYSDK_cJSON__h
#define XYSDK_cJSON__h

#ifdef __cplusplus
extern "C"
{
#endif

#include  "system/includes.h"
#include  "app_config.h"
/* xm_cJSON Types: */
#define xm_cJSON_False 0
#define xm_cJSON_True 1
#define xm_cJSON_NULL 2
#define xm_cJSON_Number 3
#define xm_cJSON_String 4
#define xm_cJSON_Array 5
#define xm_cJSON_Object 6
	
#define xm_cJSON_IsReference 256

/* The xm_cJSON structure: */
typedef struct xm_cJSON {
	struct xm_cJSON *next,*prev;	/* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
	struct xm_cJSON *child;		/* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */

	int type;					/* The type of the item, as above. */

	char *valuestring;			/* The item's string, if type==xm_cJSON_String */
	int valueint;				/* The item's number, if type==xm_cJSON_Number */
	double valuedouble;			/* The item's number, if type==xm_cJSON_Number */

	char *string;				/* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
} xm_cJSON;

typedef struct xm_cJSON_Hooks {
      void *(*malloc_fn)(size_t sz);
      void (*free_fn)(void *ptr);
} xm_cJSON_Hooks;

/* Supply malloc, realloc and free functions to xm_cJSON */
extern void xm_cJSON_InitHooks(xm_cJSON_Hooks* hooks);


/* Supply a block of JSON, and this returns a xm_cJSON object you can interrogate. Call xm_cJSON_Delete when finished. */
extern xm_cJSON *xm_cJSON_Parse(const char *value);
/* Render a xm_cJSON entity to text for transfer/storage. Free the char* when finished. */
extern char  *xm_cJSON_Print(xm_cJSON *item);
/* Render a xm_cJSON entity to text for transfer/storage without any formatting. Free the char* when finished. */
extern char  *xm_cJSON_PrintUnformatted(xm_cJSON *item);
/* Delete a xm_cJSON entity and all subentities. */
extern void   xm_cJSON_Delete(xm_cJSON *c);

/* Returns the number of items in an array (or object). */
extern int	  xm_cJSON_GetArraySize(xm_cJSON *array);
/* Retrieve item number "item" from array "array". Returns NULL if unsuccessful. */
extern xm_cJSON *xm_cJSON_GetArrayItem(xm_cJSON *array,int item);
/* Get item "string" from object. Case insensitive. */
extern xm_cJSON *xm_cJSON_GetObjectItem(xm_cJSON *object,const char *string);

/* 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 xm_cJSON_Parse() returns 0. 0 when xm_cJSON_Parse() succeeds. */
extern const char *xm_cJSON_GetErrorPtr(void);
	
/* These calls create a xm_cJSON item of the appropriate type. */
extern xm_cJSON *xm_cJSON_CreateNull(void);
extern xm_cJSON *xm_cJSON_CreateTrue(void);
extern xm_cJSON *xm_cJSON_CreateFalse(void);
extern xm_cJSON *xm_cJSON_CreateBool(int b);
extern xm_cJSON *xm_cJSON_CreateNumber(double num);
extern xm_cJSON *xm_cJSON_CreateString(const char *string);
extern xm_cJSON *xm_cJSON_CreateArray(void);
extern xm_cJSON *xm_cJSON_CreateObject(void);

/* These utilities create an Array of count items. */
extern xm_cJSON *xm_cJSON_CreateIntArray(const int *numbers,int count);
extern xm_cJSON *xm_cJSON_CreateFloatArray(const float *numbers,int count);
extern xm_cJSON *xm_cJSON_CreateDoubleArray(const double *numbers,int count);
extern xm_cJSON *xm_cJSON_CreateStringArray(const char **strings,int count);

/* Append item to the specified array/object. */
extern void xm_cJSON_AddItemToArray(xm_cJSON *array, xm_cJSON *item);
extern void	xm_cJSON_AddItemToObject(xm_cJSON *object,const char *string,xm_cJSON *item);
/* Append reference to item to the specified array/object. Use this when you want to add an existing xm_cJSON to a new xm_cJSON, but don't want to corrupt your existing xm_cJSON. */
extern void xm_cJSON_AddItemReferenceToArray(xm_cJSON *array, xm_cJSON *item);
extern void	xm_cJSON_AddItemReferenceToObject(xm_cJSON *object,const char *string,xm_cJSON *item);

/* Remove/Detatch items from Arrays/Objects. */
extern xm_cJSON *xm_cJSON_DetachItemFromArray(xm_cJSON *array,int which);
extern void   xm_cJSON_DeleteItemFromArray(xm_cJSON *array,int which);
extern xm_cJSON *xm_cJSON_DetachItemFromObject(xm_cJSON *object,const char *string);
extern void   xm_cJSON_DeleteItemFromObject(xm_cJSON *object,const char *string);
	
/* Update array items. */
extern void xm_cJSON_ReplaceItemInArray(xm_cJSON *array,int which,xm_cJSON *newitem);
extern void xm_cJSON_ReplaceItemInObject(xm_cJSON *object,const char *string,xm_cJSON *newitem);

/* Duplicate a xm_cJSON item */
extern xm_cJSON *xm_cJSON_Duplicate(xm_cJSON *item,int recurse);
/* Duplicate will create a new, identical xm_cJSON item to the one you pass, in new memory that will
need to be released. With recurse!=0, it will duplicate any children connected to the item.
The item->next and ->prev pointers are always zero on return from Duplicate. */

/* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */
extern xm_cJSON *xm_cJSON_ParseWithOpts(const char *value,const char **return_parse_end,int require_null_terminated);

extern void xm_cJSON_Minify(char *json);

/* Macros for creating things quickly. */
#define xm_cJSON_AddNullToObject(object,name)		xm_cJSON_AddItemToObject(object, name, xm_cJSON_CreateNull())
#define xm_cJSON_AddTrueToObject(object,name)		xm_cJSON_AddItemToObject(object, name, xm_cJSON_CreateTrue())
#define xm_cJSON_AddFalseToObject(object,name)		xm_cJSON_AddItemToObject(object, name, xm_cJSON_CreateFalse())
#define xm_cJSON_AddBoolToObject(object,name,b)	xm_cJSON_AddItemToObject(object, name, xm_cJSON_CreateBool(b))
#define xm_cJSON_AddNumberToObject(object,name,n)	xm_cJSON_AddItemToObject(object, name, xm_cJSON_CreateNumber(n))
#define xm_cJSON_AddStringToObject(object,name,s)	xm_cJSON_AddItemToObject(object, name, xm_cJSON_CreateString(s))

/* When assigning an integer value, it needs to be propagated to valuedouble too. */
#define xm_cJSON_SetIntValue(object,val)			((object)?(object)->valueint=(object)->valuedouble=(val):(val))

#ifdef __cplusplus
}
#endif

#endif