RapidJSON 介绍

前端之家收集整理的这篇文章主要介绍了RapidJSON 介绍前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1. RapidJSON 介绍

RapidJSON 是一个解析和生成 JSON 的 C++ 库。
无需编译,包含头文件即可使用。

2. RapidJSON 使用

2.1 解析

const char* json = "{"
        "\"stringValue\": \"hello world\","
        "\"boolTrue\": true,"
        "\"boolFalse\": false,"
        "\"nullValue\": null,"
        "\"intValue\": 3498,"
        "\"floatValue\": 3.14,"
        "\"arrayValue\": [1,2,3,4]"
    "}";

    // parse json
    Document document;
    document.Parse(json);
    assert(document.IsObject());

2.2 查询

2.2.1 查询字符串

// query string
    const char* STRING_VALUE = "stringValue";
    assert(document.HasMember(STRING_VALUE));
    assert(document[STRING_VALUE].IsString());
    std::cout << STRING_VALUE << " = " << document[STRING_VALUE].GetString() << std::endl;

2.2.2 查询 Bool 值

// query bool
    const char* BOOL_TRUE = "boolTrue";
    assert(document.HasMember(BOOL_TRUE));
    assert(document[BOOL_TRUE].IsBool());
    assert(document[BOOL_TRUE].IsTrue());
    std::cout << BOOL_TRUE << " = " << std::boolalpha << document[BOOL_TRUE].GetBool() << std::endl;
    const char* BOOL_FALSE = "boolFalse";
    assert(document.HasMember(BOOL_FALSE));
    assert(document[BOOL_FALSE].IsBool());
    assert(document[BOOL_FALSE].IsFalse());
    std::cout << BOOL_FALSE << " = " << std::boolalpha << document[BOOL_FALSE].GetBool() << std::endl;

2.2.3 判断是否为 Null 值

// query null value
    const char* NULL_VALUE = "nullValue";
    assert(document.HasMember(NULL_VALUE));
    std::cout << NULL_VALUE << " = " << (document[NULL_VALUE].IsNull() ? "null" : "?") << std::endl;

2.2.4 查询 Int 值

// query int value
    const char* INT_VALUE = "intValue";
    assert(document.HasMember(INT_VALUE));
    assert(document[INT_VALUE].IsNumber());
    assert(document[INT_VALUE].IsInt());
    assert(document[INT_VALUE].IsInt64());
    assert(document[INT_VALUE].IsUint());
    assert(document[INT_VALUE].IsUint64());
    std::cout << INT_VALUE << "(int) = " << document[INT_VALUE].GetInt() << std::endl;
    std::cout << INT_VALUE << "(uint) = " << document[INT_VALUE].GetUint() << std::endl;
    std::cout << INT_VALUE << "(int64) = " << document[INT_VALUE].GetInt64() << std::endl;
    std::cout << INT_VALUE << "(uint64) = " << document[INT_VALUE].GetUint64() << std::endl;

2.2.5 查询 Float 值

// query float value
    const char* FLOAT_VALUE = "floatValue";
    assert(document.HasMember(FLOAT_VALUE));
    assert(document[FLOAT_VALUE].IsNumber());
    assert(document[FLOAT_VALUE].IsFloat());
    assert(document[FLOAT_VALUE].IsDouble());
    std::cout << FLOAT_VALUE << "(float) = " << document[FLOAT_VALUE].GetFloat() << std::endl;
    std::cout << FLOAT_VALUE << "(double) = " << document[FLOAT_VALUE].GetDouble() << std::endl;

2.2.6 查询数组

// query array
    const char* ARRAY_VALUE = "arrayValue";
    const Value& arrayValue = document[ARRAY_VALUE];
    assert(arrayValue.IsArray());
    for (SizeType i = 0; i != arrayValue.Size(); ++i)
    {
        assert(arrayValue[i].IsInt());
        std::cout << ARRAY_VALUE << "[" << i << "] = " << arrayValue[i].GetInt() << std::endl;
    }
    for (Value::ConstValueIterator iter = arrayValue.Begin(); iter != arrayValue.End(); ++iter)
    {
        assert(iter->IsInt());
        std::cout << iter->GetInt() << std::endl;
    }
    // Range-based For Loop,C++11 only
    for (auto& value : arrayValue.GetArray())
    {
        assert(value.IsInt());
        std::cout << value.GetInt() << std::endl;
    }

2.2.7 查询对象

// query object
    static const char* kTypeNames[] = { "Null","False","True","Object","Array","String","Number" };
    for (Value::ConstMemberIterator iter = document.MemberBegin(); iter != document.MemberEnd(); ++iter)
    {
        std::cout << "Type of member " << iter->name.GetString() << " is " << kTypeNames[iter->value.GetType()] << std::endl;
    }
    // Range-based For Loop,C++11 only
    for (auto& member : document.GetObject())
    {
        std::cout << "Type of member " << member.name.GetString() << " is " << kTypeNames[member.value.GetType()] << std::endl;
    }
    // find member
    Value::ConstMemberIterator iter = document.FindMember(STRING_VALUE);
    if (iter != document.MemberEnd())
    {
        std::cout << STRING_VALUE << " = " << iter->value.GetString() << std::endl;
    }

3.完整的例子

#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"
#include <iostream>
#include <string>

void testQuery()
{
    using namespace rapidjson;

    const char* json = "{"
        "\"stringValue\": \"hello world\","
        "\"boolTrue\": true,"
        "\"boolFalse\": false,"
        "\"nullValue\": null,"
        "\"intValue\": 3498,"
        "\"floatValue\": 3.14,"
        "\"arrayValue\": [1,4]"
    "}";

    // parse json
    Document document;
    document.Parse(json);
    assert(document.IsObject());

    // query string
    const char* STRING_VALUE = "stringValue";
    assert(document.HasMember(STRING_VALUE));
    assert(document[STRING_VALUE].IsString());
    std::cout << STRING_VALUE << " = " << document[STRING_VALUE].GetString() << std::endl;

    // query bool
    const char* BOOL_TRUE = "boolTrue";
    assert(document.HasMember(BOOL_TRUE));
    assert(document[BOOL_TRUE].IsBool());
    assert(document[BOOL_TRUE].IsTrue());
    std::cout << BOOL_TRUE << " = " << std::boolalpha << document[BOOL_TRUE].GetBool() << std::endl;
    const char* BOOL_FALSE = "boolFalse";
    assert(document.HasMember(BOOL_FALSE));
    assert(document[BOOL_FALSE].IsBool());
    assert(document[BOOL_FALSE].IsFalse());
    std::cout << BOOL_FALSE << " = " << std::boolalpha << document[BOOL_FALSE].GetBool() << std::endl;

    // query null value
    const char* NULL_VALUE = "nullValue";
    assert(document.HasMember(NULL_VALUE));
    std::cout << NULL_VALUE << " = " << (document[NULL_VALUE].IsNull() ? "null" : "?") << std::endl;

    // query int value
    const char* INT_VALUE = "intValue";
    assert(document.HasMember(INT_VALUE));
    assert(document[INT_VALUE].IsNumber());
    assert(document[INT_VALUE].IsInt());
    assert(document[INT_VALUE].IsInt64());
    assert(document[INT_VALUE].IsUint());
    assert(document[INT_VALUE].IsUint64());
    std::cout << INT_VALUE << "(int) = " << document[INT_VALUE].GetInt() << std::endl;
    std::cout << INT_VALUE << "(uint) = " << document[INT_VALUE].GetUint() << std::endl;
    std::cout << INT_VALUE << "(int64) = " << document[INT_VALUE].GetInt64() << std::endl;
    std::cout << INT_VALUE << "(uint64) = " << document[INT_VALUE].GetUint64() << std::endl;

    // query float value
    const char* FLOAT_VALUE = "floatValue";
    assert(document.HasMember(FLOAT_VALUE));
    assert(document[FLOAT_VALUE].IsNumber());
    assert(document[FLOAT_VALUE].IsFloat());
    assert(document[FLOAT_VALUE].IsDouble());
    std::cout << FLOAT_VALUE << "(float) = " << document[FLOAT_VALUE].GetFloat() << std::endl;
    std::cout << FLOAT_VALUE << "(double) = " << document[FLOAT_VALUE].GetDouble() << std::endl;

    // query array
    const char* ARRAY_VALUE = "arrayValue";
    const Value& arrayValue = document[ARRAY_VALUE];
    assert(arrayValue.IsArray());
    for (SizeType i = 0; i != arrayValue.Size(); ++i)
    {
        assert(arrayValue[i].IsInt());
        std::cout << ARRAY_VALUE << "[" << i << "] = " << arrayValue[i].GetInt() << std::endl;
    }
    for (Value::ConstValueIterator iter = arrayValue.Begin(); iter != arrayValue.End(); ++iter)
    {
        assert(iter->IsInt());
        std::cout << iter->GetInt() << std::endl;
    }
    // Range-based For Loop,C++11 only
    for (auto& value : arrayValue.GetArray())
    {
        assert(value.IsInt());
        std::cout << value.GetInt() << std::endl;
    }

    // query object
    static const char* kTypeNames[] = { "Null",C++11 only
    for (auto& member : document.GetObject())
    {
        std::cout << "Type of member " << member.name.GetString() << " is " << kTypeNames[member.value.GetType()] << std::endl;
    }
    // find member
    Value::ConstMemberIterator iter = document.FindMember(STRING_VALUE);
    if (iter != document.MemberEnd())
    {
        std::cout << STRING_VALUE << " = " << iter->value.GetString() << std::endl;
    }
}

void test1()
{
    // 解析json
    const char* json = "{\"project\":\"rapidjson\",\"stars\":10}";
    rapidjson::Document d;
    d.Parse(json);
    // 获取
    rapidjson::Value& s = d["stars"];  
    std::cout << "stars: " << s.GetInt() << std::endl;

    // 设置值
    s.SetInt(s.GetInt() + 1);

    // 获取json
    rapidjson::StringBuffer buffer;
    rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
    d.Accept(writer);
    std::cout << buffer.GetString() << std::endl;
}

void test2()
{
    // 创建json
    rapidjson::Document document;
    rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
    rapidjson::Value root(rapidjson::kObjectType);
    root.AddMember("project","rapidjson",allocator);
    root.AddMember("stars",12,allocator);
    rapidjson::StringBuffer buffer;
    rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
    root.Accept(writer);
    std::string reststring = buffer.GetString();
    std::cout << reststring << std::endl;
}

int main(void)
{
    testQuery();
    test1();
    test2();   

    return 0;
}
原文链接:https://www.f2er.com/json/289027.html

猜你在找的Json相关文章