|
Packit Service |
30b792 |
/* cJSON.h
|
|
Packit Service |
30b792 |
* Copyright (c) 2009 Dave Gamble
|
|
Packit Service |
30b792 |
*
|
|
Packit Service |
30b792 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
Packit Service |
30b792 |
* of this software and associated documentation files (the "Software"), to deal
|
|
Packit Service |
30b792 |
* in the Software without restriction, including without limitation the rights
|
|
Packit Service |
30b792 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
Packit Service |
30b792 |
* copies of the Software, and to permit persons to whom the Software is
|
|
Packit Service |
30b792 |
* furnished to do so, subject to the following conditions:
|
|
Packit Service |
30b792 |
*
|
|
Packit Service |
30b792 |
* The above copyright notice and this permission notice shall be included in
|
|
Packit Service |
30b792 |
* all copies or substantial portions of the Software.
|
|
Packit Service |
30b792 |
*
|
|
Packit Service |
30b792 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
Packit Service |
30b792 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
Packit Service |
30b792 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
Packit Service |
30b792 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
Packit Service |
30b792 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
Packit Service |
30b792 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
Packit Service |
30b792 |
* THE SOFTWARE.
|
|
Packit Service |
30b792 |
*
|
|
Packit Service |
30b792 |
* SPDX-License-Identifier: MIT
|
|
Packit Service |
30b792 |
*
|
|
Packit Service |
30b792 |
* Note that this code has been modified from the original code taken
|
|
Packit Service |
30b792 |
* from cjson-code-58.zip.
|
|
Packit Service |
30b792 |
*/
|
|
Packit Service |
30b792 |
|
|
Packit Service |
30b792 |
#ifndef cJSON_h
|
|
Packit Service |
30b792 |
#define cJSON_h
|
|
Packit Service |
30b792 |
|
|
Packit Service |
30b792 |
#ifdef __cplusplus
|
|
Packit Service |
30b792 |
extern "C"
|
|
Packit Service |
30b792 |
{
|
|
Packit Service |
30b792 |
#if 0 /*(to make Emacs auto-indent happy)*/
|
|
Packit Service |
30b792 |
}
|
|
Packit Service |
30b792 |
#endif
|
|
Packit Service |
30b792 |
#endif
|
|
Packit Service |
30b792 |
|
|
Packit Service |
30b792 |
/* cJSON Types: */
|
|
Packit Service |
30b792 |
#define cJSON_False 0
|
|
Packit Service |
30b792 |
#define cJSON_True 1
|
|
Packit Service |
30b792 |
#define cJSON_NULL 2
|
|
Packit Service |
30b792 |
#define cJSON_Number 3
|
|
Packit Service |
30b792 |
#define cJSON_String 4
|
|
Packit Service |
30b792 |
#define cJSON_Array 5
|
|
Packit Service |
30b792 |
#define cJSON_Object 6
|
|
Packit Service |
30b792 |
|
|
Packit Service |
30b792 |
#define cJSON_IsReference 256
|
|
Packit Service |
30b792 |
|
|
Packit Service |
30b792 |
/* The cJSON structure: */
|
|
Packit Service |
30b792 |
typedef struct cJSON
|
|
Packit Service |
30b792 |
{
|
|
Packit Service |
30b792 |
/* next/prev allow you to walk array/object chains. Alternatively,
|
|
Packit Service |
30b792 |
use GetArraySize/GetArrayItem/GetObjectItem */
|
|
Packit Service |
30b792 |
struct cJSON *next, *prev;
|
|
Packit Service |
30b792 |
|
|
Packit Service |
30b792 |
/* An array or object item will have a child pointer pointing to a
|
|
Packit Service |
30b792 |
chain of the items in the array/object. */
|
|
Packit Service |
30b792 |
struct cJSON *child;
|
|
Packit Service |
30b792 |
|
|
Packit Service |
30b792 |
int type; /* The type of the item, as above. */
|
|
Packit Service |
30b792 |
|
|
Packit Service |
30b792 |
char *valuestring; /* The item's string, if type==cJSON_String */
|
|
Packit Service |
30b792 |
int valueint; /* The item's number, if type==cJSON_Number */
|
|
Packit Service |
30b792 |
double valuedouble; /* The item's number, if type==cJSON_Number */
|
|
Packit Service |
30b792 |
|
|
Packit Service |
30b792 |
/* The item's name string, if this item is the child of, or is in
|
|
Packit Service |
30b792 |
the list of subitems of an object. */
|
|
Packit Service |
30b792 |
char *string;
|
|
Packit Service |
30b792 |
} cJSON;
|
|
Packit Service |
30b792 |
|
|
Packit Service |
30b792 |
typedef struct cJSON *cjson_t;
|
|
Packit Service |
30b792 |
|
|
Packit Service |
30b792 |
/* Macros to test the type of an object. */
|
|
Packit Service |
30b792 |
#define cjson_is_boolean(a) (!((a)->type & ~1))
|
|
Packit Service |
30b792 |
#define cjson_is_false(a) ((a)->type == cJSON_False)
|
|
Packit Service |
30b792 |
#define cjson_is_true(a) ((a)->type == cJSON_True)
|
|
Packit Service |
30b792 |
#define cjson_is_null(a) ((a)->type == cJSON_NULL)
|
|
Packit Service |
30b792 |
#define cjson_is_number(a) ((a)->type == cJSON_Number)
|
|
Packit Service |
30b792 |
#define cjson_is_string(a) ((a)->type == cJSON_String)
|
|
Packit Service |
30b792 |
#define cjson_is_array(a) ((a)->type == cJSON_Array)
|
|
Packit Service |
30b792 |
#define cjson_is_object(a) ((a)->type == cJSON_Object)
|
|
Packit Service |
30b792 |
|
|
Packit Service |
30b792 |
/* Supply a block of JSON, and this returns a cJSON object you can
|
|
Packit Service |
30b792 |
interrogate. Call cJSON_Delete when finished. */
|
|
Packit Service |
30b792 |
extern cJSON *cJSON_Parse(const char *value, size_t *r_erroff);
|
|
Packit Service |
30b792 |
|
|
Packit Service |
30b792 |
/* Render a cJSON entity to text for transfer/storage. Free the char*
|
|
Packit Service |
30b792 |
when finished. */
|
|
Packit Service |
30b792 |
extern char *cJSON_Print(cJSON *item);
|
|
Packit Service |
30b792 |
|
|
Packit Service |
30b792 |
/* Render a cJSON entity to text for transfer/storage without any
|
|
Packit Service |
30b792 |
formatting. Free the char* when finished. */
|
|
Packit Service |
30b792 |
extern char *cJSON_PrintUnformatted(cJSON *item);
|
|
Packit Service |
30b792 |
|
|
Packit Service |
30b792 |
/* Delete a cJSON entity and all subentities. */
|
|
Packit Service |
30b792 |
extern void cJSON_Delete(cJSON *c);
|
|
Packit Service |
30b792 |
|
|
Packit Service |
30b792 |
/* Returns the number of items in an array (or object). */
|
|
Packit Service |
30b792 |
extern int cJSON_GetArraySize(cJSON *array);
|
|
Packit Service |
30b792 |
|
|
Packit Service |
30b792 |
/* Retrieve item number "item" from array "array". Returns NULL if
|
|
Packit Service |
30b792 |
unsuccessful. */
|
|
Packit Service |
30b792 |
extern cJSON *cJSON_GetArrayItem(cJSON *array,int item);
|
|
Packit Service |
30b792 |
|
|
Packit Service |
30b792 |
/* Get item "string" from object. Case insensitive. */
|
|
Packit Service |
30b792 |
extern cJSON *cJSON_GetObjectItem(cJSON *object,const char *string);
|
|
Packit Service |
30b792 |
|
|
Packit Service |
30b792 |
/* These calls create a cJSON item of the appropriate type. */
|
|
Packit Service |
30b792 |
extern cJSON *cJSON_CreateNull(void);
|
|
Packit Service |
30b792 |
extern cJSON *cJSON_CreateTrue(void);
|
|
Packit Service |
30b792 |
extern cJSON *cJSON_CreateFalse(void);
|
|
Packit Service |
30b792 |
extern cJSON *cJSON_CreateBool(int b);
|
|
Packit Service |
30b792 |
extern cJSON *cJSON_CreateNumber(double num);
|
|
Packit Service |
30b792 |
extern cJSON *cJSON_CreateString(const char *string);
|
|
Packit Service |
30b792 |
extern cJSON *cJSON_CreateStringConvey (char *string);
|
|
Packit Service |
30b792 |
extern cJSON *cJSON_CreateArray(void);
|
|
Packit Service |
30b792 |
extern cJSON *cJSON_CreateObject(void);
|
|
Packit Service |
30b792 |
|
|
Packit Service |
30b792 |
/* These utilities create an Array of count items. */
|
|
Packit Service |
30b792 |
extern cJSON *cJSON_CreateIntArray(const int *numbers,int count);
|
|
Packit Service |
30b792 |
extern cJSON *cJSON_CreateFloatArray(const float *numbers,int count);
|
|
Packit Service |
30b792 |
extern cJSON *cJSON_CreateDoubleArray(const double *numbers,int count);
|
|
Packit Service |
30b792 |
extern cJSON *cJSON_CreateStringArray(const char **strings,int count);
|
|
Packit Service |
30b792 |
|
|
Packit Service |
30b792 |
/* Append item to the specified array. */
|
|
Packit Service |
30b792 |
extern void cJSON_AddItemToArray(cJSON *array, cJSON *item);
|
|
Packit Service |
30b792 |
|
|
Packit Service |
30b792 |
/* Append item to the specified object. */
|
|
Packit Service |
30b792 |
extern cJSON *cJSON_AddItemToObject(cJSON *object, const char *name,
|
|
Packit Service |
30b792 |
cJSON *item);
|
|
Packit Service |
30b792 |
extern cJSON *cJSON_AddNullToObject (cJSON *object, const char *name);
|
|
Packit Service |
30b792 |
extern cJSON *cJSON_AddTrueToObject (cJSON *object, const char *name);
|
|
Packit Service |
30b792 |
extern cJSON *cJSON_AddFalseToObject (cJSON *object, const char *name);
|
|
Packit Service |
30b792 |
extern cJSON *cJSON_AddBoolToObject (cJSON *object, const char *name, int b);
|
|
Packit Service |
30b792 |
extern cJSON *cJSON_AddNumberToObject (cJSON *object, const char *name,
|
|
Packit Service |
30b792 |
double num);
|
|
Packit Service |
30b792 |
extern cJSON *cJSON_AddStringToObject (cJSON *object, const char *name,
|
|
Packit Service |
30b792 |
const char *string);
|
|
Packit Service |
30b792 |
|
|
Packit Service |
30b792 |
/* Append reference to item to the specified array/object. Use this
|
|
Packit Service |
30b792 |
when you want to add an existing cJSON to a new cJSON, but don't
|
|
Packit Service |
30b792 |
want to corrupt your existing cJSON. */
|
|
Packit Service |
30b792 |
extern void cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item);
|
|
Packit Service |
30b792 |
extern void cJSON_AddItemReferenceToObject(cJSON *object,
|
|
Packit Service |
30b792 |
const char *string,cJSON *item);
|
|
Packit Service |
30b792 |
|
|
Packit Service |
30b792 |
/* Remove/Detach items from Arrays/Objects. */
|
|
Packit Service |
30b792 |
extern cJSON *cJSON_DetachItemFromArray(cJSON *array,int which);
|
|
Packit Service |
30b792 |
extern void cJSON_DeleteItemFromArray(cJSON *array,int which);
|
|
Packit Service |
30b792 |
extern cJSON *cJSON_DetachItemFromObject(cJSON *object,const char *string);
|
|
Packit Service |
30b792 |
extern void cJSON_DeleteItemFromObject(cJSON *object,const char *string);
|
|
Packit Service |
30b792 |
|
|
Packit Service |
30b792 |
/* Update array items. */
|
|
Packit Service |
30b792 |
extern void cJSON_ReplaceItemInArray(cJSON *array,int which,cJSON *newitem);
|
|
Packit Service |
30b792 |
extern void cJSON_ReplaceItemInObject(cJSON *object,
|
|
Packit Service |
30b792 |
const char *string, cJSON *newitem);
|
|
Packit Service |
30b792 |
|
|
Packit Service |
30b792 |
/* Duplicate a cJSON item */
|
|
Packit Service |
30b792 |
extern cJSON *cJSON_Duplicate(cJSON *item,int recurse);
|
|
Packit Service |
30b792 |
|
|
Packit Service |
30b792 |
/* Duplicate will create a new, identical cJSON item to the one you
|
|
Packit Service |
30b792 |
pass, in new memory that will need to be released. With recurse!=0,
|
|
Packit Service |
30b792 |
it will duplicate any children connected to the item. The
|
|
Packit Service |
30b792 |
item->next and ->prev pointers are always zero on return from
|
|
Packit Service |
30b792 |
Duplicate. */
|
|
Packit Service |
30b792 |
|
|
Packit Service |
30b792 |
/* ParseWithOpts allows you to require (and check) that the JSON is
|
|
Packit Service |
30b792 |
null terminated, and to retrieve the pointer to the final byte
|
|
Packit Service |
30b792 |
parsed. */
|
|
Packit Service |
30b792 |
extern cJSON *cJSON_ParseWithOpts(const char *value,
|
|
Packit Service |
30b792 |
const char **return_parse_end,
|
|
Packit Service |
30b792 |
int require_null_terminated,
|
|
Packit Service |
30b792 |
size_t *r_erroff);
|
|
Packit Service |
30b792 |
|
|
Packit Service |
30b792 |
extern void cJSON_Minify(char *json);
|
|
Packit Service |
30b792 |
|
|
Packit Service |
30b792 |
/* When assigning an integer value, it needs to be propagated to
|
|
Packit Service |
30b792 |
valuedouble too. */
|
|
Packit Service |
30b792 |
#define cJSON_SetIntValue(object,val) \
|
|
Packit Service |
30b792 |
((object)?(object)->valueint=(object)->valuedouble=(val):(val))
|
|
Packit Service |
30b792 |
|
|
Packit Service |
30b792 |
#ifdef __cplusplus
|
|
Packit Service |
30b792 |
}
|
|
Packit Service |
30b792 |
#endif
|
|
Packit Service |
30b792 |
|
|
Packit Service |
30b792 |
#endif /* cJSON_h */
|