代码改变世界

rapidjson 使用

2016-11-15 23:13  Loull  阅读(16046)  评论(0编辑  收藏  举报

生成数组集合的字符串

#include <stdio.h>
#include <string>
#include <iostream>

#include "rapidjson/document.h"
#include "rapidjson/prettywriter.h"
#include "rapidjson/filestream.h"
#include "rapidjson/stringbuffer.h"

using namespace std;
using namespace rapidjson;

int main(int argc, char *argv[])
{
    printf("Lu//a\"\n");
    Document document;
    
    Document::AllocatorType& allocator = document.GetAllocator();
    Value contact(kArrayType);
    Value contact2(kArrayType);
    Value root(kArrayType);
    contact.PushBack("Lu//a\"", allocator).PushBack("Mio", allocator).PushBack("", allocator);
    contact2.PushBack("Lu// a", allocator).PushBack("Mio", allocator).PushBack("", allocator);
    root.PushBack(contact, allocator);
    root.PushBack(contact2, allocator);

    StringBuffer buffer;
    Writer<StringBuffer> writer(buffer);
    root.Accept(writer);
    string reststring = buffer.GetString();
    cout << reststring << endl;
    return 0;
}

输出:

Lu//a"
[["Lu//a\"","Mio",""],["Lu// a","Mio",""]]

对象json

void TestJson2()
{
    Document document;
    Document::AllocatorType& allocator = document.GetAllocator();
    Value root(kObjectType);

    Value storage_photo_count(kStringType);
    std::string storage_photo_count_str("49");
    storage_photo_count.SetString(storage_photo_count_str.c_str(),
        storage_photo_count_str.size(),allocator);

    Value storage_music_count(kStringType);
    std::string storage_music_count_str("48");
    storage_music_count.SetString(storage_music_count_str.c_str(),
        storage_music_count_str.size(),allocator);

    root.AddMember("storage.photo.count",storage_photo_count,allocator);
    root.AddMember("storage.music.count",storage_music_count,allocator);

    StringBuffer buffer;
    Writer<StringBuffer> writer(buffer);
    root.Accept(writer);
    std::string result = buffer.GetString();
    cout << "result: " << result << "..........:" << result.size()<< endl;
}

枚举Object

Document::MemberIterator ite = document.MemberBegin();
    for(; ite != document.MemberEnd(); ++ite)
    {
        const char* name = ite->name.GetString();
        const char* value = ite->value.GetString();
        cout << name << ":" << value << endl;
    }

 

/// 添加一个String对象;              
              rapidjson::Document::AllocatorType&allocator = doc.GetAllocator();   ///< 获取最初数据的分配器
              rapidjson::Value strObject(rapidjson::kStringType);   ///<添加字符串方法1
              strObject.SetString("love");
              doc.AddMember("hello1", strObject,allocator);
/*          doc.AddMember("hello1","love you", allocator);    ///<添加字符串方法2:往分配器中添加一个对象*/
 
              /// 添加一个null对象
              rapidjson::Value nullObject(rapidjson::kNullType);
              doc.AddMember("null", nullObject,allocator);         ///<往分配器中添加一个对象
 
              /// 添加一个数组对象
              rapidjson::Value array(rapidjson::kArrayType);                 ///< 创建一个数组对象
              rapidjson::Value object(rapidjson::kObjectType);               ///<创建数组里面对象。
              object.AddMember("id", 1,allocator);
              object.AddMember("name","lai", allocator);
              object.AddMember("age", "12",allocator);
              object.AddMember("low", true,allocator);
              array.PushBack(object, allocator);
              doc.AddMember("player", array,allocator);    ///<将上述的数组内容添加到一个名为“player”的数组中
 
              /// 在已有的数组中添加一个成员对象
              rapidjson::Value& aArray1 = doc["a"];
              aArray1.PushBack(2.0, allocator);

gitlab上例子

// Hello World example
// This example shows basic usage of DOM-style API.

#include "rapidjson/document.h"     // rapidjson's DOM-style API
#include "rapidjson/prettywriter.h" // for stringify JSON
#include <cstdio>

using namespace rapidjson;
using namespace std;

int main(int, char*[]) {
    ////////////////////////////////////////////////////////////////////////////
    // 1. Parse a JSON text string to a document.

    const char json[] = " { \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3, 4] } ";
    printf("Original JSON:\n %s\n", json);

    Document document;  // Default template parameter uses UTF8 and MemoryPoolAllocator.

#if 0
    // "normal" parsing, decode strings to new buffers. Can use other input stream via ParseStream().
    if (document.Parse(json).HasParseError())
        return 1;
#else
    // In-situ parsing, decode strings directly in the source string. Source must be string.
    char buffer[sizeof(json)];
    memcpy(buffer, json, sizeof(json));
    if (document.ParseInsitu(buffer).HasParseError())
        return 1;
#endif

    printf("\nParsing to document succeeded.\n");

    ////////////////////////////////////////////////////////////////////////////
    // 2. Access values in document. 

    printf("\nAccess values in document:\n");
    assert(document.IsObject());    // Document is a JSON value represents the root of DOM. Root can be either an object or array.

    assert(document.HasMember("hello"));
    assert(document["hello"].IsString());
    printf("hello = %s\n", document["hello"].GetString());

    // Since version 0.2, you can use single lookup to check the existing of member and its value:
    Value::MemberIterator hello = document.FindMember("hello");
    assert(hello != document.MemberEnd());
    assert(hello->value.IsString());
    assert(strcmp("world", hello->value.GetString()) == 0);
    (void)hello;

    assert(document["t"].IsBool());     // JSON true/false are bool. Can also uses more specific function IsTrue().
    printf("t = %s\n", document["t"].GetBool() ? "true" : "false");

    assert(document["f"].IsBool());
    printf("f = %s\n", document["f"].GetBool() ? "true" : "false");

    printf("n = %s\n", document["n"].IsNull() ? "null" : "?");

    assert(document["i"].IsNumber());   // Number is a JSON type, but C++ needs more specific type.
    assert(document["i"].IsInt());      // In this case, IsUint()/IsInt64()/IsUInt64() also return true.
    printf("i = %d\n", document["i"].GetInt()); // Alternative (int)document["i"]

    assert(document["pi"].IsNumber());
    assert(document["pi"].IsDouble());
    printf("pi = %g\n", document["pi"].GetDouble());

    {
        const Value& a = document["a"]; // Using a reference for consecutive access is handy and faster.
        assert(a.IsArray());
        for (SizeType i = 0; i < a.Size(); i++) // rapidjson uses SizeType instead of size_t.
            printf("a[%d] = %d\n", i, a[i].GetInt());
        
        int y = a[0].GetInt();
        (void)y;

        // Iterating array with iterators
        printf("a = ");
        for (Value::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr)
            printf("%d ", itr->GetInt());
        printf("\n");
    }

    // Iterating object members
    static const char* kTypeNames[] = { "Null", "False", "True", "Object", "Array", "String", "Number" };
    for (Value::ConstMemberIterator itr = document.MemberBegin(); itr != document.MemberEnd(); ++itr)
        printf("Type of member %s is %s\n", itr->name.GetString(), kTypeNames[itr->value.GetType()]);

    ////////////////////////////////////////////////////////////////////////////
    // 3. Modify values in document.

    // Change i to a bigger number
    {
        uint64_t f20 = 1;   // compute factorial of 20
        for (uint64_t j = 1; j <= 20; j++)
            f20 *= j;
        document["i"] = f20;    // Alternate form: document["i"].SetUint64(f20)
        assert(!document["i"].IsInt()); // No longer can be cast as int or uint.
    }

    // Adding values to array.
    {
        Value& a = document["a"];   // This time we uses non-const reference.
        Document::AllocatorType& allocator = document.GetAllocator();
        for (int i = 5; i <= 10; i++)
            a.PushBack(i, allocator);   // May look a bit strange, allocator is needed for potentially realloc. We normally uses the document's.

        // Fluent API
        a.PushBack("Lua", allocator).PushBack("Mio", allocator);
    }

    // Making string values.

    // This version of SetString() just store the pointer to the string.
    // So it is for literal and string that exists within value's life-cycle.
    {
        document["hello"] = "rapidjson";    // This will invoke strlen()
        // Faster version:
        // document["hello"].SetString("rapidjson", 9);
    }

    // This version of SetString() needs an allocator, which means it will allocate a new buffer and copy the the string into the buffer.
    Value author;
    {
        char buffer2[10];
        int len = sprintf(buffer2, "%s %s", "Milo", "Yip");  // synthetic example of dynamically created string.

        author.SetString(buffer2, static_cast<SizeType>(len), document.GetAllocator());
        // Shorter but slower version:
        // document["hello"].SetString(buffer, document.GetAllocator());

        // Constructor version: 
        // Value author(buffer, len, document.GetAllocator());
        // Value author(buffer, document.GetAllocator());
        memset(buffer2, 0, sizeof(buffer2)); // For demonstration purpose.
    }
    // Variable 'buffer' is unusable now but 'author' has already made a copy.
    document.AddMember("author", author, document.GetAllocator());

    assert(author.IsNull());        // Move semantic for assignment. After this variable is assigned as a member, the variable becomes null.

    ////////////////////////////////////////////////////////////////////////////
    // 4. Stringify JSON

    printf("\nModified JSON with reformatting:\n");
    StringBuffer sb;
    PrettyWriter<StringBuffer> writer(sb);
    document.Accept(writer);    // Accept() traverses the DOM and generates Handler events.
    puts(sb.GetString());

    return 0;
}

 

 

 

官网:http://rapidjson.org/zh-cn/

参考:http://blog.csdn.net/infoworld/article/details/9625129

http://www.bkjia.com/Androidjc/884053.html

https://github.com/miloyip/rapidjson/blob/master/example/tutorial/tutorial.cpp