nlohmann/json——NLOHMANN_JSON_SERIALIZE_ENUM

目录

源码如下:

源码分析:

使用示例:


源码如下:

/*!
@brief macro to briefly define a mapping between an enum and JSON
@def NLOHMANN_JSON_SERIALIZE_ENUM
@since version 3.4.0
*/
#define NLOHMANN_JSON_SERIALIZE_ENUM(ENUM_TYPE, ...)                                    \
    template<typename BasicJsonType>                                                    \
    inline void to_json(BasicJsonType& j, const ENUM_TYPE& e)                           \
    {                                                                                   \
        static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!");  \
        static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__;             \
        auto it = std::find_if(std::begin(m), std::end(m),                              \
            [e](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool             \
        {                                                                               \
            return ej_pair.first == e;                                                  \
        });                                                                             \
        j = ((it != std::end(m)) ? it : std::begin(m))->second;                         \
    }                                                                                   \
    template<typename BasicJsonType>                                                    \
    inline void from_json(const BasicJsonType& j, ENUM_TYPE& e)                         \
    {                                                                                   \
        static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!");  \
        static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__;             \
        auto it = std::find_if(std::begin(m), std::end(m),                              \
            [&j](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool            \
        {                                                                               \
            return ej_pair.second == j;                                                 \
        });                                                                             \
        e = ((it != std::end(m)) ? it : std::begin(m))->first;                          \
    }

源码分析:

从注释看,NLOHMANN_JSON_SERIALIZE_ENUM宏是将用户定义的枚举类型与JSON进行映射。其实际上是定义了两个模板函数,to_json和from_json,宏的第一个参数ENUM_TYPE是用户定义的枚举类型,后面携带了可变参数,即...符号。

// asset检查,看ENUM_TYPE是否是枚举,其中#符号为宏中的特殊运算符,在预编译时期,用于将宏参数转换为字符串

static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!");

// __VA_ARGS__为系统自定义变量,指向宏中的可变参数

static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__;

// 在可变参数指定的map中查找

auto it = std::find_if(std::begin(m), std::end(m),

    [e](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool

{

    return ej_pair.first == e;

});

j = ((it != std::end(m)) ? it : std::begin(m))->second;

使用示例:

enum class Type
{
    kType1 = 0,
    kType2 = 1,
    kType3 = 2,
    kType4 = 3,
    kType5 = 4
};

NLOHMANN_JSON_SERIALIZE_ENUM(Type, {
    {Type::kType1, nullptr},
    {Type::kType2, "stopped"},
    {Type::kType3, "running"},
    {Type::kType4, "completed"},
    {Type::kType5, "completed"},
});

int main(int argc, char *argv[])
{
    Type t = Type::kType4;
    nlohmann::json j = t;
    std::cout << "dump:" << j.dump() << std::endl;
    return 0;
}

运行结果如下:

dump:"completed"

static int _ecsQosRateGetEventInforms(ECS_SESS *pSess, ECS_s8 *rptName, JSON_Value **pOutVal) { QOS_DEBUG(); int ret = ECS_OK; JSON_Value *rootVal = NULL; JSON_Object *rootObj = NULL; JSON_Value *dataVal = NULL; JSON_Object *dataObj = NULL; JSON_Value *portsVal = NULL; JSON_Array *portsArray = NULL; JSON_Value *pStormVal = NULL; JSON_Object *pStormObj = NULL; JSON_Value *occursVal = NULL; JSON_Array *occursArray = NULL; int upIdx = 0; user_port up = {}; valid_uplist validUplist = NULL; int statusBc = 0, statusMc = 0, loop = 0; valid_uplist_init(&validUplist, 0); PFM_IF_FAIL_DONE_RET(ret, valid_uplist_get(&validUplist, 0), ERR_NOT_FOUND); rootVal = json_value_init_object(); rootObj = json_value_get_object(rootVal); dataVal = json_value_init_object(); dataObj = json_value_get_object(dataVal); portsVal = json_value_init_array(); portsArray = json_value_get_array(portsVal); json_object_set_string(rootObj, "eid", QOS_STORM_CONTROL_EID); json_object_set_number(rootObj, "timestamp", getTimeInMilliSecond()); UP_ITER_VALID(validUplist, upIdx, up) { libQosPortStormOccursGetAndSetDisable(up, QOS_RATE_STORM_MODE_BC, &statusBc); libQosPortStormOccursGetAndSetDisable(up, QOS_RATE_STORM_MODE_MC, &statusMc); if(!statusBc && !statusMc) { continue; } QOS_DEBUG("up:%d,mode:%d,status:%d", UP_INDEX(up), QOS_RATE_STORM_MODE_BC, statusBc); QOS_DEBUG("up:%d,mode:%d,status:%d", UP_INDEX(up), QOS_RATE_STORM_MODE_MC, statusMc); occursVal = json_value_init_array(); occursArray = json_value_get_array(occursVal); json_array_append_number(occursArray,statusBc); json_array_append_number(occursArray,statusMc); pStormVal = json_value_init_object(); pStormObj = json_value_get_object(pStormVal); ecsJsonObjAddUp(pStormObj, NULL, NULL, &up); json_object_set_value(pStormObj, "stormOccurs", occursVal); json_array_append_value(portsArray, pStormVal); loop++; statusBc = 0; statusMc = 0; } json_object_set_value(dataObj, "ports", portsVal); json_object_set_value(rootObj, "data", dataVal); if (0 != loop) { *pOutVal = rootVal; } else { json_value_free(rootVal); } valid_uplist_free(&validUplist); return ret; done: valid_uplist_free(&validUplist); if (rootVal) { json_value_free(rootVal); } return ret; } /* Parson ( http://kgabis.github.com/parson/ ) Copyright (c) 2012 - 2016 Krzysztof Gabis 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 parson_parson_h #define parson_parson_h #ifdef __cplusplus extern "C" { #endif #include <stddef.h> /* size_t */ #include "pal/alloc.h" /* Types and enums */ typedef struct json_object_t JSON_Object; typedef struct json_array_t JSON_Array; typedef struct json_value_t JSON_Value; enum json_value_type { JSONError = -1, JSONNull = 1, JSONString = 2, JSONNumber = 3, JSONObject = 4, JSONArray = 5, JSONBoolean = 6 }; typedef int JSON_Value_Type; /* Type definitions */ typedef union json_value_value { char *string; double number; JSON_Object *object; JSON_Array *array; int boolean; int null; } JSON_Value_Value; struct json_value_t { JSON_Value_Type type; JSON_Value_Value value; }; enum json_result_t { JSONSuccess = 0, JSONFailure = -1 }; typedef int JSON_Status; typedef void * (*JSON_Malloc_Function)(size_t); typedef void (*JSON_Free_Function)(void *); #if PAL_MEMLEAK_ENABLE /* Call only once, before calling any other function from parson API. If not called, malloc and free from stdlib will be used for all allocations */ void json_set_allocation_functions(JSON_Malloc_Function malloc_fun, JSON_Free_Function free_fun); /* Parses first JSON value in a file, returns NULL in case of error */ #define json_parse_file(filename) json_parse_file_desc(filename, PAL_ALLOC_GENERAL_TAG_DESC("parson")) JSON_Value * json_parse_file_desc(const char *filename, char *desc); /* Parses first JSON value in a file and ignores comments (/ * * / and //), returns NULL in case of error */ #define json_parse_file_with_comments(filename) json_parse_file_with_comments_desc(filename, PAL_ALLOC_GENERAL_TAG_DESC("parson")) JSON_Value * json_parse_file_with_comments_desc(const char *filename, char *desc); /* Parses first JSON value in a string, returns NULL in case of error */ #define json_parse_string(string) json_parse_string_desc(string, PAL_ALLOC_GENERAL_TAG_DESC("parson")) JSON_Value * json_parse_string_desc(const char *string, char *desc); /* Parses first JSON value in a string and ignores comments (/ * * / and //), returns NULL in case of error */ #define json_parse_string_with_comments(string) json_parse_string_with_comments_desc(string, PAL_ALLOC_GENERAL_TAG_DESC("parson")) JSON_Value * json_parse_string_with_comments_desc(const char *string, char *desc); /* Serialization */ size_t json_serialization_size(const JSON_Value *value); /* returns 0 on fail */ JSON_Status json_serialize_to_buffer(const JSON_Value *value, char *buf, size_t buf_size_in_bytes); #define json_serialize_to_file(value, filename) json_serialize_to_file_desc(value, filename, PAL_ALLOC_GENERAL_TAG_DESC("parson")) JSON_Status json_serialize_to_file_desc(const JSON_Value *value, const char *filename, char *desc); #define json_serialize_to_string(value) json_serialize_to_string_desc(value, PAL_ALLOC_GENERAL_TAG_DESC("parson")) char * json_serialize_to_string_desc(const JSON_Value *value, char *desc); /* Pretty serialization */ size_t json_serialization_size_pretty(const JSON_Value *value); /* returns 0 on fail */ JSON_Status json_serialize_to_buffer_pretty(const JSON_Value *value, char *buf, size_t buf_size_in_bytes); #define json_serialize_to_file_pretty(value, filename) json_serialize_to_file_pretty_desc(value, filename, PAL_ALLOC_GENERAL_TAG_DESC("parson")) JSON_Status json_serialize_to_file_pretty_desc(const JSON_Value *value, const char *filename, char *desc); #define json_serialize_to_string_pretty(value) json_serialize_to_string_pretty_desc(value, PAL_ALLOC_GENERAL_TAG_DESC("parson")) char *json_serialize_to_string_pretty_desc(const JSON_Value *value, char *desc); void json_free_serialized_string(char *string); /* frees string from json_serialize_to_string and json_serialize_to_string_pretty */ /* Comparing */ int json_value_equals(const JSON_Value *a, const JSON_Value *b); /* Validation This is *NOT* JSON Schema. It validates json by checking if object have identically named fields with matching types. For example schema {"name":"", "age":0} will validate {"name":"Joe", "age":25} and {"name":"Joe", "age":25, "gender":"m"}, but not {"name":"Joe"} or {"name":"Joe", "age":"Cucumber"}. In case of arrays, only first value in schema is checked against all values in tested array. Empty objects ({}) validate all objects, empty arrays ([]) validate all arrays, null validates values of every type. */ JSON_Status json_validate(const JSON_Value *schema, const JSON_Value *value); /* * JSON Object */ JSON_Value * json_object_get_value (const JSON_Object *object, const char *name); const char * json_object_get_string (const JSON_Object *object, const char *name); JSON_Object * json_object_get_object (const JSON_Object *object, const char *name); JSON_Array * json_object_get_array (const JSON_Object *object, const char *name); double json_object_get_number (const JSON_Object *object, const char *name); /* returns 0 on fail */ int json_object_get_boolean(const JSON_Object *object, const char *name); /* returns -1 on fail */ /* dotget functions enable addressing values with dot notation in nested objects, just like in structs or c++/java/c# objects (e.g. objectA.objectB.value). Because valid names in JSON can contain dots, some values may be inaccessible this way. */ JSON_Value * json_object_dotget_value (const JSON_Object *object, const char *name); const char * json_object_dotget_string (const JSON_Object *object, const char *name); JSON_Object * json_object_dotget_object (const JSON_Object *object, const char *name); JSON_Array * json_object_dotget_array (const JSON_Object *object, const char *name); double json_object_dotget_number (const JSON_Object *object, const char *name); /* returns 0 on fail */ int json_object_dotget_boolean(const JSON_Object *object, const char *name); /* returns -1 on fail */ /* Functions to get available names */ size_t json_object_get_count (const JSON_Object *object); const char * json_object_get_name (const JSON_Object *object, size_t index); JSON_Value * json_object_get_value_at(const JSON_Object *object, size_t index); /* Functions to check if object has a value with a specific name. Returned value is 1 if object has * a value and 0 if it doesn't. dothas functions behave exactly like dotget functions. */ int json_object_has_value (const JSON_Object *object, const char *name); int json_object_has_value_of_type(const JSON_Object *object, const char *name, JSON_Value_Type type); int json_object_dothas_value (const JSON_Object *object, const char *name); int json_object_dothas_value_of_type(const JSON_Object *object, const char *name, JSON_Value_Type type); /* Creates new name-value pair or frees and replaces old value with a new one. * json_object_set_value does not copy passed value so it shouldn't be freed afterwards. */ #define json_object_set_value(object, name, value) json_object_set_value_desc(object, name, value, PAL_ALLOC_GENERAL_TAG_DESC("parson")) JSON_Status json_object_set_value_desc(JSON_Object *object, const char *name, JSON_Value *value, char *desc); #define json_object_set_string(object, name, string) json_object_set_string_desc(object, name, string, PAL_ALLOC_GENERAL_TAG_DESC("parson")) JSON_Status json_object_set_string_desc(JSON_Object *object, const char *name, const char *string, char *desc); #define json_object_set_number(object, name, number) json_object_set_number_desc(object, name, number, PAL_ALLOC_GENERAL_TAG_DESC("parson")) JSON_Status json_object_set_number_desc(JSON_Object *object, const char *name, double number, char *desc); #define json_object_set_boolean(object, name, boolean) json_object_set_boolean_desc(object, name, boolean, PAL_ALLOC_GENERAL_TAG_DESC("parson")) JSON_Status json_object_set_boolean_desc(JSON_Object *object, const char *name, int boolean, char *desc); #define json_object_set_null(object, name) json_object_set_null_desc(object, name, PAL_ALLOC_GENERAL_TAG_DESC("parson")) JSON_Status json_object_set_null_desc(JSON_Object *object, const char *name, char *desc); /* Works like dotget functions, but creates whole hierarchy if necessary. * json_object_dotset_value does not copy passed value so it shouldn't be freed afterwards. */ #define json_object_dotset_value(object, name, value) json_object_dotset_value_desc(object, name, value, PAL_ALLOC_GENERAL_TAG_DESC("parson")) JSON_Status json_object_dotset_value_desc(JSON_Object *object, const char *name, JSON_Value *value, char *desc); #define json_object_dotset_string(object, name, string) json_object_dotset_string_desc(object, name, string, PAL_ALLOC_GENERAL_TAG_DESC("parson")) JSON_Status json_object_dotset_string_desc(JSON_Object *object, const char *name, const char *string, char *desc); #define json_object_dotset_number(object, name, number) json_object_dotset_number_desc(object, name, number, PAL_ALLOC_GENERAL_TAG_DESC("parson")) JSON_Status json_object_dotset_number_desc(JSON_Object *object, const char *name, double number, char *desc); #define json_object_dotset_boolean(object, name, boolean) json_object_dotset_boolean_desc(object, name, boolean, PAL_ALLOC_GENERAL_TAG_DESC("parson")) JSON_Status json_object_dotset_boolean_desc(JSON_Object *object, const char *name, int boolean, char *desc); #define json_object_dotset_null(object, name) json_object_dotset_null_desc(object, name, PAL_ALLOC_GENERAL_TAG_DESC("parson")) JSON_Status json_object_dotset_null_desc(JSON_Object *object, const char *name, char *desc); /* Frees and removes name-value pair */ JSON_Status json_object_remove(JSON_Object *object, const char *name); /* Works like dotget function, but removes name-value pair only on exact match. */ #define json_object_dotremove(object, key) json_object_dotremove_desc(object, key, PAL_ALLOC_GENERAL_TAG_DESC("parson")) JSON_Status json_object_dotremove_desc(JSON_Object *object, const char *key, char *desc); /* Removes all name-value pairs in object */ JSON_Status json_object_clear(JSON_Object *object); /* *JSON Array */ JSON_Value * json_array_get_value (const JSON_Array *array, size_t index); const char * json_array_get_string (const JSON_Array *array, size_t index); JSON_Object * json_array_get_object (const JSON_Array *array, size_t index); JSON_Array * json_array_get_array (const JSON_Array *array, size_t index); double json_array_get_number (const JSON_Array *array, size_t index); /* returns 0 on fail */ int json_array_get_boolean(const JSON_Array *array, size_t index); /* returns -1 on fail */ size_t json_array_get_count (const JSON_Array *array); /* Frees and removes value at given index, does nothing and returns JSONFailure if index doesn't exist. * Order of values in array may change during execution. */ JSON_Status json_array_remove(JSON_Array *array, size_t i); /* Frees and removes from array value at given index and replaces it with given one. * Does nothing and returns JSONFailure if index doesn't exist. * json_array_replace_value does not copy passed value so it shouldn't be freed afterwards. */ JSON_Status json_array_replace_value(JSON_Array *array, size_t i, JSON_Value *value); #define json_array_replace_string(array, i, string) json_array_replace_string_desc(array, i, string, key, PAL_ALLOC_GENERAL_TAG_DESC("parson")) JSON_Status json_array_replace_string_desc(JSON_Array *array, size_t i, const char *string, char *desc); #define json_array_replace_number(array, i, number) json_array_replace_number_desc(array, i, number, key, PAL_ALLOC_GENERAL_TAG_DESC("parson")) JSON_Status json_array_replace_number_desc(JSON_Array *array, size_t i, double number, char *desc); #define json_array_replace_boolean(array, i, boolean) json_array_replace_boolean_desc(array, i, boolean, key, PAL_ALLOC_GENERAL_TAG_DESC("parson")) JSON_Status json_array_replace_boolean_desc(JSON_Array *array, size_t i, int boolean, char *desc); #define json_array_replace_null(array, i) json_array_replace_null_desc(array, i, boolean, PAL_ALLOC_GENERAL_TAG_DESC("parson")) JSON_Status json_array_replace_null_desc(JSON_Array *array, size_t i, char *desc); /* Frees and removes all values from array */ JSON_Status json_array_clear(JSON_Array *array); /* Appends new value at the end of array. * json_array_append_value does not copy passed value so it shouldn't be freed afterwards. */ #define json_array_append_value(array, value) json_array_append_value_desc(array, value, PAL_ALLOC_GENERAL_TAG_DESC("parson")) JSON_Status json_array_append_value_desc(JSON_Array *array, JSON_Value *value, char *desc); #define json_array_append_string(array, string) json_array_append_string_desc(array, string, PAL_ALLOC_GENERAL_TAG_DESC("parson")) JSON_Status json_array_append_string_desc(JSON_Array *array, const char *string, char *desc); #define json_array_append_number(array, number) json_array_append_number_desc(array, number, PAL_ALLOC_GENERAL_TAG_DESC("parson")) JSON_Status json_array_append_number_desc(JSON_Array *array, double number, char *desc); #define json_array_append_boolean(array, boolean) json_array_append_boolean_desc(array, boolean, PAL_ALLOC_GENERAL_TAG_DESC("parson")) JSON_Status json_array_append_boolean_desc(JSON_Array *array, int boolean, char *desc); #define json_array_append_null(array) json_array_append_null_desc(array, PAL_ALLOC_GENERAL_TAG_DESC("parson")) JSON_Status json_array_append_null_desc(JSON_Array *array, char *desc); /* *JSON Value */ #define json_value_init_object() json_value_init_object_desc(PAL_ALLOC_GENERAL_TAG_DESC("parson")) JSON_Value * json_value_init_object_desc (char *desc); #define json_value_init_array() json_value_init_array_desc(PAL_ALLOC_GENERAL_TAG_DESC("parson")) JSON_Value * json_value_init_array_desc (char *desc); #define json_value_init_string(string) json_value_init_string_desc(string, PAL_ALLOC_GENERAL_TAG_DESC("parson")) JSON_Value * json_value_init_string_desc (const char *string, char *desc); /* copies passed string */ #define json_value_init_number(number) json_value_init_number_desc(number, PAL_ALLOC_GENERAL_TAG_DESC("parson")) JSON_Value * json_value_init_number_desc (double number, char *desc); #define json_value_init_boolean(boolean) json_value_init_boolean_desc(boolean, PAL_ALLOC_GENERAL_TAG_DESC("parson")) JSON_Value * json_value_init_boolean_desc(int boolean, char *desc); #define json_value_init_null() json_value_init_null_desc(PAL_ALLOC_GENERAL_TAG_DESC("parson")) JSON_Value * json_value_init_null_desc (char *desc); #define json_value_deep_copy(value) json_value_deep_copy_desc(value, PAL_ALLOC_GENERAL_TAG_DESC("parson")) JSON_Value * json_value_deep_copy_desc (const JSON_Value *value, char *desc); void json_value_free (JSON_Value *value); JSON_Value_Type json_value_get_type (const JSON_Value *value); JSON_Object * json_value_get_object (const JSON_Value *value); JSON_Array * json_value_get_array (const JSON_Value *value); const char * json_value_get_string (const JSON_Value *value); double json_value_get_number (const JSON_Value *value); int json_value_get_boolean(const JSON_Value *value); /* Same as above, but shorter */ JSON_Value_Type json_type (const JSON_Value *value); JSON_Object * json_object (const JSON_Value *value); JSON_Array * json_array (const JSON_Value *value); const char * json_string (const JSON_Value *value); double json_number (const JSON_Value *value); int json_boolean(const JSON_Value *value); #ifdef __cplusplus } #endif #else /* caoliao: PAL_MEMLEAK_ENABLE (23Jan10) */ /* Call only once, before calling any other function from parson API. If not called, malloc and free from stdlib will be used for all allocations */ void json_set_allocation_functions(JSON_Malloc_Function malloc_fun, JSON_Free_Function free_fun); /* Parses first JSON value in a file, returns NULL in case of error */ JSON_Value * json_parse_file(const char *filename); /* Parses first JSON value in a file and ignores comments (/ * * / and //), returns NULL in case of error */ JSON_Value * json_parse_file_with_comments(const char *filename); /* Parses first JSON value in a string, returns NULL in case of error */ JSON_Value * json_parse_string(const char *string); /* Parses first JSON value in a string and ignores comments (/ * * / and //), returns NULL in case of error */ JSON_Value * json_parse_string_with_comments(const char *string); /* Serialization */ size_t json_serialization_size(const JSON_Value *value); /* returns 0 on fail */ JSON_Status json_serialize_to_buffer(const JSON_Value *value, char *buf, size_t buf_size_in_bytes); JSON_Status json_serialize_to_file(const JSON_Value *value, const char *filename); char * json_serialize_to_string(const JSON_Value *value); /* Pretty serialization */ size_t json_serialization_size_pretty(const JSON_Value *value); /* returns 0 on fail */ JSON_Status json_serialize_to_buffer_pretty(const JSON_Value *value, char *buf, size_t buf_size_in_bytes); JSON_Status json_serialize_to_file_pretty(const JSON_Value *value, const char *filename); char * json_serialize_to_string_pretty(const JSON_Value *value); void json_free_serialized_string(char *string); /* frees string from json_serialize_to_string and json_serialize_to_string_pretty */ /* Comparing */ int json_value_equals(const JSON_Value *a, const JSON_Value *b); /* Validation This is *NOT* JSON Schema. It validates json by checking if object have identically named fields with matching types. For example schema {"name":"", "age":0} will validate {"name":"Joe", "age":25} and {"name":"Joe", "age":25, "gender":"m"}, but not {"name":"Joe"} or {"name":"Joe", "age":"Cucumber"}. In case of arrays, only first value in schema is checked against all values in tested array. Empty objects ({}) validate all objects, empty arrays ([]) validate all arrays, null validates values of every type. */ JSON_Status json_validate(const JSON_Value *schema, const JSON_Value *value); /* * JSON Object */ JSON_Value * json_object_get_value (const JSON_Object *object, const char *name); const char * json_object_get_string (const JSON_Object *object, const char *name); JSON_Object * json_object_get_object (const JSON_Object *object, const char *name); JSON_Array * json_object_get_array (const JSON_Object *object, const char *name); double json_object_get_number (const JSON_Object *object, const char *name); /* returns 0 on fail */ int json_object_get_boolean(const JSON_Object *object, const char *name); /* returns -1 on fail */ /* dotget functions enable addressing values with dot notation in nested objects, just like in structs or c++/java/c# objects (e.g. objectA.objectB.value). Because valid names in JSON can contain dots, some values may be inaccessible this way. */ JSON_Value * json_object_dotget_value (const JSON_Object *object, const char *name); const char * json_object_dotget_string (const JSON_Object *object, const char *name); JSON_Object * json_object_dotget_object (const JSON_Object *object, const char *name); JSON_Array * json_object_dotget_array (const JSON_Object *object, const char *name); double json_object_dotget_number (const JSON_Object *object, const char *name); /* returns 0 on fail */ int json_object_dotget_boolean(const JSON_Object *object, const char *name); /* returns -1 on fail */ /* Functions to get available names */ size_t json_object_get_count (const JSON_Object *object); const char * json_object_get_name (const JSON_Object *object, size_t index); JSON_Value * json_object_get_value_at(const JSON_Object *object, size_t index); /* Functions to check if object has a value with a specific name. Returned value is 1 if object has * a value and 0 if it doesn't. dothas functions behave exactly like dotget functions. */ int json_object_has_value (const JSON_Object *object, const char *name); int json_object_has_value_of_type(const JSON_Object *object, const char *name, JSON_Value_Type type); int json_object_dothas_value (const JSON_Object *object, const char *name); int json_object_dothas_value_of_type(const JSON_Object *object, const char *name, JSON_Value_Type type); /* Creates new name-value pair or frees and replaces old value with a new one. * json_object_set_value does not copy passed value so it shouldn't be freed afterwards. */ JSON_Status json_object_set_value(JSON_Object *object, const char *name, JSON_Value *value); JSON_Status json_object_set_string(JSON_Object *object, const char *name, const char *string); JSON_Status json_object_set_number(JSON_Object *object, const char *name, double number); JSON_Status json_object_set_boolean(JSON_Object *object, const char *name, int boolean); JSON_Status json_object_set_null(JSON_Object *object, const char *name); /* Works like dotget functions, but creates whole hierarchy if necessary. * json_object_dotset_value does not copy passed value so it shouldn't be freed afterwards. */ JSON_Status json_object_dotset_value(JSON_Object *object, const char *name, JSON_Value *value); JSON_Status json_object_dotset_string(JSON_Object *object, const char *name, const char *string); JSON_Status json_object_dotset_number(JSON_Object *object, const char *name, double number); JSON_Status json_object_dotset_boolean(JSON_Object *object, const char *name, int boolean); JSON_Status json_object_dotset_null(JSON_Object *object, const char *name); /* Frees and removes name-value pair */ JSON_Status json_object_remove(JSON_Object *object, const char *name); /* Works like dotget function, but removes name-value pair only on exact match. */ JSON_Status json_object_dotremove(JSON_Object *object, const char *key); /* Removes all name-value pairs in object */ JSON_Status json_object_clear(JSON_Object *object); void json_object_free(JSON_Object *object); JSON_Object *json_object_deep_copy(JSON_Object *object); /* *JSON Array */ JSON_Value * json_array_get_value (const JSON_Array *array, size_t index); const char * json_array_get_string (const JSON_Array *array, size_t index); JSON_Object * json_array_get_object (const JSON_Array *array, size_t index); JSON_Array * json_array_get_array (const JSON_Array *array, size_t index); double json_array_get_number (const JSON_Array *array, size_t index); /* returns 0 on fail */ int json_array_get_boolean(const JSON_Array *array, size_t index); /* returns -1 on fail */ size_t json_array_get_count (const JSON_Array *array); /* Frees and removes value at given index, does nothing and returns JSONFailure if index doesn't exist. * Order of values in array may change during execution. */ JSON_Status json_array_remove(JSON_Array *array, size_t i); /* Frees and removes from array value at given index and replaces it with given one. * Does nothing and returns JSONFailure if index doesn't exist. * json_array_replace_value does not copy passed value so it shouldn't be freed afterwards. */ JSON_Status json_array_replace_value(JSON_Array *array, size_t i, JSON_Value *value); JSON_Status json_array_replace_string(JSON_Array *array, size_t i, const char* string); JSON_Status json_array_replace_number(JSON_Array *array, size_t i, double number); JSON_Status json_array_replace_boolean(JSON_Array *array, size_t i, int boolean); JSON_Status json_array_replace_null(JSON_Array *array, size_t i); /* Frees and removes all values from array */ JSON_Status json_array_clear(JSON_Array *array); /* Appends new value at the end of array. * json_array_append_value does not copy passed value so it shouldn't be freed afterwards. */ JSON_Status json_array_append_value(JSON_Array *array, JSON_Value *value); JSON_Status json_array_append_string(JSON_Array *array, const char *string); JSON_Status json_array_append_number(JSON_Array *array, double number); JSON_Status json_array_append_boolean(JSON_Array *array, int boolean); JSON_Status json_array_append_null(JSON_Array *array); void json_array_free(JSON_Array *array); JSON_Array *json_array_deep_copy(JSON_Array *array); /* *JSON Value */ JSON_Value * json_value_init_object (void); JSON_Value * json_value_init_array (void); JSON_Value * json_value_init_string (const char *string); /* copies passed string */ JSON_Value * json_value_init_number (double number); JSON_Value * json_value_init_boolean(int boolean); JSON_Value * json_value_init_null (void); JSON_Value * json_value_deep_copy (const JSON_Value *value); void json_value_free (JSON_Value *value); JSON_Value_Type json_value_get_type (const JSON_Value *value); JSON_Object * json_value_get_object (const JSON_Value *value); JSON_Array * json_value_get_array (const JSON_Value *value); const char * json_value_get_string (const JSON_Value *value); double json_value_get_number (const JSON_Value *value); int json_value_get_boolean(const JSON_Value *value); /* Same as above, but shorter */ JSON_Value_Type json_type (const JSON_Value *value); JSON_Object * json_object (const JSON_Value *value); JSON_Array * json_array (const JSON_Value *value); const char * json_string (const JSON_Value *value); double json_number (const JSON_Value *value); int json_boolean(const JSON_Value *value); #ifdef __cplusplus } #endif #endif /* caoliao: PAL_MEMLEAK_ENABLE (23Jan10) */ #endif /* caoliao: parson_parson_h (23Jan10) */ 能否根据以上内容推导出包含待上传数据pOutVal的json文件格式
最新发布
09-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值