/** * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved. * 内部基础模块,所有参数合法性由调用者保证 */ #ifndef ASCEND_CJSON_PARSER #define ASCEND_CJSON_PARSER #include #include #include "vector.h" #include "sort_vector.h" #ifdef __cplusplus extern "C" { #endif typedef enum { CJSON_NULL, CJSON_INT, CJSON_DOUBLE, CJSON_BOOL, CJSON_STRING, CJSON_OBJ, CJSON_ARRAY, } CJSON_TYPE; typedef struct { CJSON_TYPE type; union { char *string; bool value; double fNumber; int64_t iNumber; Vector array; SortVector objs; } value; } CJsonObj; CJsonObj *CJsonParse(const char *jsonContent, size_t jsonLen); CJsonObj *CJsonFileParse(const char *filePath); char *CJsonFileParseKey(const char *filePath, const char *key); size_t CJsonParseKeyPosition(const char *jsonContent, size_t jsonLen, const char *key, size_t *offset); void FreeCJsonObj(CJsonObj *obj); CJsonObj *CJsonArrayAt(CJsonObj *obj, size_t i); CJsonObj *GetCJsonSubObj(CJsonObj *obj, const char *key); static inline bool CJsonIsNull(CJsonObj *obj) { return (obj->type == CJSON_NULL); }; static inline bool CJsonIsInt(CJsonObj *obj) { return (obj->type == CJSON_INT); }; static inline bool CJsonIsBool(CJsonObj *obj) { return (obj->type == CJSON_BOOL); }; static inline bool CJsonIsDouble(CJsonObj *obj) { return (obj->type == CJSON_DOUBLE); }; static inline bool CJsonIsString(CJsonObj *obj) { return (obj->type == CJSON_STRING); }; static inline bool CJsonIsObj(CJsonObj *obj) { return (obj->type == CJSON_OBJ); }; static inline bool CJsonIsArray(CJsonObj *obj) { return (obj->type == CJSON_ARRAY); }; static inline bool GetCJsonBool(CJsonObj *obj) { return ((obj->type != CJSON_BOOL)) ? false : obj->value.value; }; static inline int64_t GetCJsonInt(CJsonObj *obj) { return (obj->type != CJSON_INT) ? 0 : obj->value.iNumber; }; static inline double GetCJsonDouble(CJsonObj *obj) { return (obj->type != CJSON_DOUBLE) ? 0 : obj->value.fNumber; }; static inline char *GetCJsonString(CJsonObj *obj) { return (obj->type != CJSON_STRING) ? NULL : obj->value.string; }; static inline size_t GetCJsonArraySize(CJsonObj *obj) { return (obj->type != CJSON_ARRAY) ? 0 : VectorSize(&obj->value.array); }; #ifdef __cplusplus } #endif #endif