yb_arm/rfic/inc/xzJSON.h
2025-05-20 01:51:25 +08:00

301 lines
16 KiB
C

/*
Copyright (c) 2009-2017 Dave Gamble and xzJSON 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.
*/
#ifndef xzJSON__h
#define xzJSON__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 symbol
For *nix builds that support visibility attribute, you can define similar behavior by
setting default visibility to hidden by adding
-fvisibility=hidden (for gcc)
or
-xldscope=hidden (for sun cc)
to CFLAGS
then using the CJSON_API_VISIBILITY flag to "export" the same symbols the way CJSON_EXPORT_SYMBOLS does
*/
#define CJSON_CDECL __cdecl
#define CJSON_STDCALL __stdcall
/* export symbols by default, this is necessary for copy pasting the C and header file */
#if !defined(CJSON_HIDE_SYMBOLS) && !defined(CJSON_IMPORT_SYMBOLS) && !defined(CJSON_EXPORT_SYMBOLS)
#define CJSON_EXPORT_SYMBOLS
#endif
#if defined(CJSON_HIDE_SYMBOLS)
#define XZJSON_PUBLIC(type) type CJSON_STDCALL
#elif defined(CJSON_EXPORT_SYMBOLS)
#define XZJSON_PUBLIC(type) __declspec(dllexport) type CJSON_STDCALL
#elif defined(CJSON_IMPORT_SYMBOLS)
#define XZJSON_PUBLIC(type) __declspec(dllimport) type CJSON_STDCALL
#endif
#else /* !__WINDOWS__ */
#define CJSON_CDECL
#define CJSON_STDCALL
#if (defined(__GNUC__) || defined(__SUNPRO_CC) || defined (__SUNPRO_C)) && defined(CJSON_API_VISIBILITY)
#define XZJSON_PUBLIC(type) __attribute__((visibility("default"))) type
#else
#define XZJSON_PUBLIC(type) type
#endif
#endif
/* project version */
#define CJSON_VERSION_MAJOR 1
#define CJSON_VERSION_MINOR 7
#define CJSON_VERSION_PATCH 16
#include <stddef.h>
/* xzJSON Types: */
#define xzJSON_Invalid (0)
#define xzJSON_False (1 << 0)
#define xzJSON_True (1 << 1)
#define xzJSON_NULL (1 << 2)
#define xzJSON_Number (1 << 3)
#define xzJSON_String (1 << 4)
#define xzJSON_Array (1 << 5)
#define xzJSON_Object (1 << 6)
#define xzJSON_Raw (1 << 7) /* raw json */
#define xzJSON_IsReference 256
#define xzJSON_StringIsConst 512
/* The xzJSON structure: */
typedef struct xzJSON
{
/* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
struct xzJSON *next;
struct xzJSON *prev;
/* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
struct xzJSON *child;
/* The type of the item, as above. */
int type;
/* The item's string, if type==xzJSON_String and type == xzJSON_Raw */
char *valuestring;
/* writing to valueint is DEPRECATED, use xzJSON_SetNumberValue instead */
int valueint;
/* The item's number, if type==xzJSON_Number */
double valuedouble;
/* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
char *string;
} xzJSON;
typedef struct xzJSON_Hooks
{
/* malloc/free are CDECL on Windows regardless of the default calling convention of the compiler, so ensure the hooks allow passing those functions directly. */
void *(CJSON_CDECL *malloc_fn)(size_t sz);
void (CJSON_CDECL *free_fn)(void *ptr);
} xzJSON_Hooks;
typedef int xzJSON_bool;
/* Limits how deeply nested arrays/objects can be before xzJSON rejects to parse them.
* This is to prevent stack overflows. */
#ifndef CJSON_NESTING_LIMIT
#define CJSON_NESTING_LIMIT 1000
#endif
/* returns the version of xzJSON as a string */
XZJSON_PUBLIC(const char*) xzJSON_Version(void);
/* Supply malloc, realloc and free functions to xzJSON */
XZJSON_PUBLIC(void) xzJSON_InitHooks(xzJSON_Hooks* hooks);
/* Memory Management: the caller is always responsible to free the results from all variants of xzJSON_Parse (with xzJSON_Delete) and xzJSON_Print (with stdlib free, xzJSON_Hooks.free_fn, or xzJSON_free as appropriate). The exception is xzJSON_PrintPreallocated, where the caller has full responsibility of the buffer. */
/* Supply a block of JSON, and this returns a xzJSON object you can interrogate. */
XZJSON_PUBLIC(xzJSON *) xzJSON_Parse(const char *value);
XZJSON_PUBLIC(xzJSON *) xzJSON_ParseWithLength(const char *value, size_t buffer_length);
/* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */
/* If you supply a ptr in return_parse_end and parsing fails, then return_parse_end will contain a pointer to the error so will match xzJSON_GetErrorPtr(). */
XZJSON_PUBLIC(xzJSON *) xzJSON_ParseWithOpts(const char *value, const char **return_parse_end, xzJSON_bool require_null_terminated);
XZJSON_PUBLIC(xzJSON *) xzJSON_ParseWithLengthOpts(const char *value, size_t buffer_length, const char **return_parse_end, xzJSON_bool require_null_terminated);
/* Render a xzJSON entity to text for transfer/storage. */
XZJSON_PUBLIC(char *) xzJSON_Print(const xzJSON *item);
/* Render a xzJSON entity to text for transfer/storage without any formatting. */
XZJSON_PUBLIC(char *) xzJSON_PrintUnformatted(const xzJSON *item);
/* Render a xzJSON entity to text using a buffered strategy. prebuffer is a guess at the final size. guessing well reduces reallocation. fmt=0 gives unformatted, =1 gives formatted */
XZJSON_PUBLIC(char *) xzJSON_PrintBuffered(const xzJSON *item, int prebuffer, xzJSON_bool fmt);
/* Render a xzJSON entity to text using a buffer already allocated in memory with given length. Returns 1 on success and 0 on failure. */
/* NOTE: xzJSON is not always 100% accurate in estimating how much memory it will use, so to be safe allocate 5 bytes more than you actually need */
XZJSON_PUBLIC(xzJSON_bool) xzJSON_PrintPreallocated(xzJSON *item, char *buffer, const int length, const xzJSON_bool format);
/* Delete a xzJSON entity and all subentities. */
XZJSON_PUBLIC(void) xzJSON_Delete(xzJSON *item);
/* Returns the number of items in an array (or object). */
XZJSON_PUBLIC(int) xzJSON_GetArraySize(const xzJSON *array);
/* Retrieve item number "index" from array "array". Returns NULL if unsuccessful. */
XZJSON_PUBLIC(xzJSON *) xzJSON_GetArrayItem(const xzJSON *array, int index);
/* Get item "string" from object. Case insensitive. */
XZJSON_PUBLIC(xzJSON *) xzJSON_GetObjectItem(const xzJSON * const object, const char * const string);
XZJSON_PUBLIC(xzJSON *) xzJSON_GetObjectItemCaseSensitive(const xzJSON * const object, const char * const string);
XZJSON_PUBLIC(xzJSON_bool) xzJSON_HasObjectItem(const xzJSON *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 xzJSON_Parse() returns 0. 0 when xzJSON_Parse() succeeds. */
XZJSON_PUBLIC(const char *) xzJSON_GetErrorPtr(void);
/* Check item type and return its value */
XZJSON_PUBLIC(char *) xzJSON_GetStringValue(const xzJSON * const item);
XZJSON_PUBLIC(double) xzJSON_GetNumberValue(const xzJSON * const item);
/* These functions check the type of an item */
XZJSON_PUBLIC(xzJSON_bool) xzJSON_IsInvalid(const xzJSON * const item);
XZJSON_PUBLIC(xzJSON_bool) xzJSON_IsFalse(const xzJSON * const item);
XZJSON_PUBLIC(xzJSON_bool) xzJSON_IsTrue(const xzJSON * const item);
XZJSON_PUBLIC(xzJSON_bool) xzJSON_IsBool(const xzJSON * const item);
XZJSON_PUBLIC(xzJSON_bool) xzJSON_IsNull(const xzJSON * const item);
XZJSON_PUBLIC(xzJSON_bool) xzJSON_IsNumber(const xzJSON * const item);
XZJSON_PUBLIC(xzJSON_bool) xzJSON_IsString(const xzJSON * const item);
XZJSON_PUBLIC(xzJSON_bool) xzJSON_IsArray(const xzJSON * const item);
XZJSON_PUBLIC(xzJSON_bool) xzJSON_IsObject(const xzJSON * const item);
XZJSON_PUBLIC(xzJSON_bool) xzJSON_IsRaw(const xzJSON * const item);
/* These calls create a xzJSON item of the appropriate type. */
XZJSON_PUBLIC(xzJSON *) xzJSON_CreateNull(void);
XZJSON_PUBLIC(xzJSON *) xzJSON_CreateTrue(void);
XZJSON_PUBLIC(xzJSON *) xzJSON_CreateFalse(void);
XZJSON_PUBLIC(xzJSON *) xzJSON_CreateBool(xzJSON_bool boolean);
XZJSON_PUBLIC(xzJSON *) xzJSON_CreateNumber(double num);
XZJSON_PUBLIC(xzJSON *) xzJSON_CreateString(const char *string);
/* raw json */
XZJSON_PUBLIC(xzJSON *) xzJSON_CreateRaw(const char *raw);
XZJSON_PUBLIC(xzJSON *) xzJSON_CreateArray(void);
XZJSON_PUBLIC(xzJSON *) xzJSON_CreateObject(void);
/* Create a string where valuestring references a string so
* it will not be freed by xzJSON_Delete */
XZJSON_PUBLIC(xzJSON *) xzJSON_CreateStringReference(const char *string);
/* Create an object/array that only references it's elements so
* they will not be freed by xzJSON_Delete */
XZJSON_PUBLIC(xzJSON *) xzJSON_CreateObjectReference(const xzJSON *child);
XZJSON_PUBLIC(xzJSON *) xzJSON_CreateArrayReference(const xzJSON *child);
/* These utilities create an Array of count items.
* The parameter count cannot be greater than the number of elements in the number array, otherwise array access will be out of bounds.*/
XZJSON_PUBLIC(xzJSON *) xzJSON_CreateIntArray(const int *numbers, int count);
XZJSON_PUBLIC(xzJSON *) xzJSON_CreateFloatArray(const float *numbers, int count);
XZJSON_PUBLIC(xzJSON *) xzJSON_CreateDoubleArray(const double *numbers, int count);
XZJSON_PUBLIC(xzJSON *) xzJSON_CreateStringArray(const char *const *strings, int count);
/* Append item to the specified array/object. */
XZJSON_PUBLIC(xzJSON_bool) xzJSON_AddItemToArray(xzJSON *array, xzJSON *item);
XZJSON_PUBLIC(xzJSON_bool) xzJSON_AddItemToObject(xzJSON *object, const char *string, xzJSON *item);
/* Use this when string is definitely const (i.e. a literal, or as good as), and will definitely survive the xzJSON object.
* WARNING: When this function was used, make sure to always check that (item->type & xzJSON_StringIsConst) is zero before
* writing to `item->string` */
XZJSON_PUBLIC(xzJSON_bool) xzJSON_AddItemToObjectCS(xzJSON *object, const char *string, xzJSON *item);
/* Append reference to item to the specified array/object. Use this when you want to add an existing xzJSON to a new xzJSON, but don't want to corrupt your existing xzJSON. */
XZJSON_PUBLIC(xzJSON_bool) xzJSON_AddItemReferenceToArray(xzJSON *array, xzJSON *item);
XZJSON_PUBLIC(xzJSON_bool) xzJSON_AddItemReferenceToObject(xzJSON *object, const char *string, xzJSON *item);
/* Remove/Detach items from Arrays/Objects. */
XZJSON_PUBLIC(xzJSON *) xzJSON_DetachItemViaPointer(xzJSON *parent, xzJSON * const item);
XZJSON_PUBLIC(xzJSON *) xzJSON_DetachItemFromArray(xzJSON *array, int which);
XZJSON_PUBLIC(void) xzJSON_DeleteItemFromArray(xzJSON *array, int which);
XZJSON_PUBLIC(xzJSON *) xzJSON_DetachItemFromObject(xzJSON *object, const char *string);
XZJSON_PUBLIC(xzJSON *) xzJSON_DetachItemFromObjectCaseSensitive(xzJSON *object, const char *string);
XZJSON_PUBLIC(void) xzJSON_DeleteItemFromObject(xzJSON *object, const char *string);
XZJSON_PUBLIC(void) xzJSON_DeleteItemFromObjectCaseSensitive(xzJSON *object, const char *string);
/* Update array items. */
XZJSON_PUBLIC(xzJSON_bool) xzJSON_InsertItemInArray(xzJSON *array, int which, xzJSON *newitem); /* Shifts pre-existing items to the right. */
XZJSON_PUBLIC(xzJSON_bool) xzJSON_ReplaceItemViaPointer(xzJSON * const parent, xzJSON * const item, xzJSON * replacement);
XZJSON_PUBLIC(xzJSON_bool) xzJSON_ReplaceItemInArray(xzJSON *array, int which, xzJSON *newitem);
XZJSON_PUBLIC(xzJSON_bool) xzJSON_ReplaceItemInObject(xzJSON *object,const char *string,xzJSON *newitem);
XZJSON_PUBLIC(xzJSON_bool) xzJSON_ReplaceItemInObjectCaseSensitive(xzJSON *object,const char *string,xzJSON *newitem);
/* Duplicate a xzJSON item */
XZJSON_PUBLIC(xzJSON *) xzJSON_Duplicate(const xzJSON *item, xzJSON_bool recurse);
/* Duplicate will create a new, identical xzJSON 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. */
/* Recursively compare two xzJSON items for equality. If either a or b is NULL or invalid, they will be considered unequal.
* case_sensitive determines if object keys are treated case sensitive (1) or case insensitive (0) */
XZJSON_PUBLIC(xzJSON_bool) xzJSON_Compare(const xzJSON * const a, const xzJSON * const b, const xzJSON_bool case_sensitive);
/* Minify a strings, remove blank characters(such as ' ', '\t', '\r', '\n') from strings.
* The input pointer json cannot point to a read-only address area, such as a string constant,
* but should point to a readable and writable address area. */
XZJSON_PUBLIC(void) xzJSON_Minify(char *json);
/* Helper functions for creating and adding items to an object at the same time.
* They return the added item or NULL on failure. */
XZJSON_PUBLIC(xzJSON*) xzJSON_AddNullToObject(xzJSON * const object, const char * const name);
XZJSON_PUBLIC(xzJSON*) xzJSON_AddTrueToObject(xzJSON * const object, const char * const name);
XZJSON_PUBLIC(xzJSON*) xzJSON_AddFalseToObject(xzJSON * const object, const char * const name);
XZJSON_PUBLIC(xzJSON*) xzJSON_AddBoolToObject(xzJSON * const object, const char * const name, const xzJSON_bool boolean);
XZJSON_PUBLIC(xzJSON*) xzJSON_AddNumberToObject(xzJSON * const object, const char * const name, const double number);
XZJSON_PUBLIC(xzJSON*) xzJSON_AddStringToObject(xzJSON * const object, const char * const name, const char * const string);
XZJSON_PUBLIC(xzJSON*) xzJSON_AddRawToObject(xzJSON * const object, const char * const name, const char * const raw);
XZJSON_PUBLIC(xzJSON*) xzJSON_AddObjectToObject(xzJSON * const object, const char * const name);
XZJSON_PUBLIC(xzJSON*) xzJSON_AddArrayToObject(xzJSON * const object, const char * const name);
/* When assigning an integer value, it needs to be propagated to valuedouble too. */
#define xzJSON_SetIntValue(object, number) ((object) ? (object)->valueint = (object)->valuedouble = (number) : (number))
/* helper for the xzJSON_SetNumberValue macro */
XZJSON_PUBLIC(double) xzJSON_SetNumberHelper(xzJSON *object, double number);
#define xzJSON_SetNumberValue(object, number) ((object != NULL) ? xzJSON_SetNumberHelper(object, (double)number) : (number))
/* Change the valuestring of a xzJSON_String object, only takes effect when type of object is xzJSON_String */
XZJSON_PUBLIC(char*) xzJSON_SetValuestring(xzJSON *object, const char *valuestring);
/* If the object is not a boolean type this does nothing and returns xzJSON_Invalid else it returns the new type*/
#define xzJSON_SetBoolValue(object, boolValue) ( \
(object != NULL && ((object)->type & (xzJSON_False|xzJSON_True))) ? \
(object)->type=((object)->type &(~(xzJSON_False|xzJSON_True)))|((boolValue)?xzJSON_True:xzJSON_False) : \
xzJSON_Invalid\
)
/* Macro for iterating over an array or object */
#define xzJSON_ArrayForEach(element, array) for(element = (array != NULL) ? (array)->child : NULL; element != NULL; element = element->next)
/* malloc/free objects using the malloc/free functions that have been set with xzJSON_InitHooks */
XZJSON_PUBLIC(void *) xzJSON_malloc(size_t size);
XZJSON_PUBLIC(void) xzJSON_free(void *object);
#ifdef __cplusplus
}
#endif
#endif