204 lines
8.7 KiB
C
204 lines
8.7 KiB
C
/*
|
|
Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
|
|
|
|
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.
|
|
*/
|
|
|
|
/* cJSON */
|
|
/* JSON parser in C. */
|
|
|
|
#ifndef CJSON_H
|
|
#define CJSON_H
|
|
|
|
#ifdef __cplusplus
|
|
extern "C"
|
|
{
|
|
#endif
|
|
|
|
#if !defined(__WINDOWS__) && (defined(WIN32) || defined(WIN64) || defined(_MSC_VER) || defined(_WIN32))
|
|
#define __WINDOWS__
|
|
#endif
|
|
|
|
#ifdef __WINDOWS__
|
|
|
|
/* When compiling for windows, we specify a specific calling convention to avoid issues where we are being called from a project with a different default calling convention. For windows you have 3 define options:
|
|
|
|
CJSON_HIDE_SYMBOLS - Define this in the case where you don't want to ever dllexport symbols
|
|
CJSON_EXPORT_SYMBOLS - Define this on library build when you want to dllexport symbols (default)
|
|
CJSON_IMPORT_SYMBOLS - Define this if you want to dllimport symbols
|
|
*/
|
|
#ifndef CJSON_HIDE_SYMBOLS
|
|
#define CJSON_EXPORT_SYMBOLS
|
|
#endif
|
|
#endif /* __WINDOWS__ */
|
|
|
|
#ifdef CJSON_EXPORT_SYMBOLS
|
|
#define CJSON_PUBLIC(type) __declspec(dllexport) type
|
|
#elif defined(CJSON_IMPORT_SYMBOLS)
|
|
#define CJSON_PUBLIC(type) __declspec(dllimport) type
|
|
#else
|
|
#define CJSON_PUBLIC(type) type
|
|
#endif
|
|
|
|
#include <stddef.h>
|
|
|
|
/* project version */
|
|
#define CJSON_VERSION_MAJOR 1
|
|
#define CJSON_VERSION_MINOR 7
|
|
#define CJSON_VERSION_PATCH 18
|
|
|
|
/* cJSON Types: */
|
|
#define cJSON_Invalid (0)
|
|
#define cJSON_False (1 << 0)
|
|
#define cJSON_True (1 << 1)
|
|
#define cJSON_Bool (cJSON_True | cJSON_False)
|
|
#define cJSON_Number (1 << 2)
|
|
#define cJSON_String (1 << 3)
|
|
#define cJSON_Array (1 << 4)
|
|
#define cJSON_Object (1 << 5)
|
|
#define cJSON_Raw (1 << 6)
|
|
#define cJSON_NULL (1 << 7)
|
|
|
|
#define cJSON_IsReference 256
|
|
#define cJSON_StringIsConst 512
|
|
|
|
/* The cJSON structure: */
|
|
typedef struct cJSON
|
|
{
|
|
/* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
|
|
struct cJSON *next;
|
|
struct cJSON *prev;
|
|
/* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
|
|
struct cJSON *child;
|
|
|
|
/* The type of the item, as above. */
|
|
int type;
|
|
|
|
/* The item's string, if type==cJSON_String and type == cJSON_Raw */
|
|
char *valuestring;
|
|
/* The item's name string, if this item is the child of an object */
|
|
char *string;
|
|
/* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */
|
|
int valueint;
|
|
/* The item's number, if type==cJSON_Number */
|
|
double valuedouble;
|
|
} cJSON;
|
|
|
|
typedef struct cJSON_Hooks
|
|
{
|
|
void *(*malloc_fn)(size_t sz);
|
|
void (*free_fn)(void *ptr);
|
|
} cJSON_Hooks;
|
|
|
|
typedef int cJSON_bool;
|
|
|
|
/* Limits how deeply nested a structure can be */
|
|
#define CJSON_NESTING_LIMIT 1000
|
|
|
|
/* String comparison, case insensitive */
|
|
CJSON_PUBLIC(int) cJSON_Strcasecmp(const char *a, const char *b);
|
|
|
|
/* Supply a block of JSON, and this returns a cJSON object you can interrogate. */
|
|
CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value);
|
|
/* Render a cJSON entity to text for transfer/storage. */
|
|
CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item);
|
|
/* Render a cJSON entity to text for transfer/storage without any formatting. */
|
|
CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item);
|
|
/* Delete a cJSON entity and all subentities. */
|
|
CJSON_PUBLIC(void) cJSON_Delete(cJSON *item);
|
|
|
|
/* Returns the number of items in an array (or object). */
|
|
CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array);
|
|
/* Retrieve item number "index" from array "array". Returns NULL if unsuccessful. */
|
|
CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index);
|
|
/* Get item "string" from object. Case insensitive. */
|
|
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string);
|
|
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string);
|
|
CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const 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 cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */
|
|
CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void);
|
|
|
|
/* These calls create a cJSON item of the appropriate type. */
|
|
CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void);
|
|
CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void);
|
|
CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void);
|
|
CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean);
|
|
CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num);
|
|
CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string);
|
|
/* raw json */
|
|
CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw);
|
|
CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void);
|
|
CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void);
|
|
|
|
/* These utilities create an Array of count items. */
|
|
CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count);
|
|
CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count);
|
|
CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count);
|
|
CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char **strings, int count);
|
|
|
|
/* Append item to the specified array/object. */
|
|
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item);
|
|
/* Append item to the specified object. */
|
|
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item);
|
|
|
|
/* These functions check the type of an item */
|
|
CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item);
|
|
CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item);
|
|
CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item);
|
|
CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item);
|
|
CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item);
|
|
CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item);
|
|
CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item);
|
|
CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item);
|
|
CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item);
|
|
|
|
/* These calls create a cJSON item of the appropriate type and add it to the specified object. */
|
|
CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * const name);
|
|
CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * const name);
|
|
CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * const name);
|
|
CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, const char * const name, const cJSON_bool boolean);
|
|
CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number);
|
|
CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string);
|
|
|
|
/* Assign a raw string value to an object. */
|
|
CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * const name, const char * const raw);
|
|
|
|
/* Memory management */
|
|
CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks *hooks);
|
|
CJSON_PUBLIC(void) cJSON_free(void *ptr);
|
|
|
|
/* Macros for creating things quickly. */
|
|
#define cJSON_AddNullToObject(object,name) cJSON_AddNullToObject(object, name)
|
|
#define cJSON_AddTrueToObject(object,name) cJSON_AddTrueToObject(object, name)
|
|
#define cJSON_AddFalseToObject(object,name) cJSON_AddFalseToObject(object, name)
|
|
#define cJSON_AddBoolToObject(object,name,b) cJSON_AddBoolToObject(object, name, b)
|
|
#define cJSON_AddNumberToObject(object,name,n) cJSON_AddNumberToObject(object, name, n)
|
|
#define cJSON_AddStringToObject(object,name,s) cJSON_AddStringToObject(object, name, s)
|
|
|
|
/* When assigning a string value, cJSON will copy the string, so you don't need to worry about it being freed.
|
|
* When assigning a raw value, cJSON will NOT copy the string, so it MUST be persistent. */
|
|
CJSON_PUBLIC(void) cJSON_SetValuestring(cJSON *object, const char *valuestring);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif
|