Newer
Older
WickedDocs / 3rdParty / rapidjson / main.cpp
@John Ryland John Ryland on 15 Dec 2015 14 KB Make test
// Hello World example
// This example shows basic usage of DOM-style API.

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

#include <iostream>
#include <fstream>
#include <sstream>


// Wrapper around a RapidJSON value
class JsonValue
{
public:
    JsonValue() : m_value(m_null) {}
    JsonValue(rapidjson::Value& a_value) : m_value(a_value) {}

    JsonValue& operator=(const JsonValue& a_other)
    {
        m_value = a_other.m_value;
    }

    enum ValueType {
      // RapidJSON types
      kNullType = 0,      //!< null
      kFalseType = 1,     //!< false
      kTrueType = 2,      //!< true
      kObjectType = 3,    //!< object
      kArrayType = 4,     //!< array 
      kStringType = 5,    //!< string
      kNumberType = 6     //!< number
      // JsonCpp types
      /*
      nullValue,
      intValue,
      uintValue,
      realValue,
      stringValue,
      booleanValue,
      arrayValue,
      objectValue
      */
    };

    ValueType type() const   { return (ValueType)m_value.GetType(); }
    bool isNull() const      { return type() == kObjectType; }
    bool isBool() const      { return type() == kFalseType || type() == kTrueType; }
    bool isNumber() const    { return type() == kNumberType; }
    bool isString() const    { return type() == kStringType; }
    bool isArray() const     { return type() == kArrayType;  }
    bool isObject() const    { return type() == kObjectType; }

    bool        asBool()   const    { return type() == kTrueType; }
    int         asInt()    const    { return (type() == kNumberType) ? m_value.GetInt() : 0; }
    double      asDouble() const    { return (type() == kNumberType) ? m_value.GetDouble() : 0.0; }
    std::string asString() const    { return (type() == kStringType) ? m_value.GetString() : ""; }

    size_t size() const                                  { return (isArray() || isObject()) ? m_value.Size() : 1; }
    JsonValue operator[](size_t a_index) const    { return (type() == kArrayType) ? m_value[a_index] : *this; } // TODO: or JsonValue(m_null)
    JsonValue operator[](const char* a_key) const { return (type() == kObjectType && m_value.HasMember(a_key)) ? m_value[a_key] : *this; }

    std::string prettyPrint() const {
      rapidjson::StringBuffer sb;
      rapidjson::PrettyWriter<rapidjson::StringBuffer> wr(sb);
      m_value.Accept(wr);
      return sb.GetString();
    }
private:
    rapidjson::Value& m_value;
    static rapidjson::Value m_null;
};

rapidjson::Value JsonValue::m_null;


// Wrapper around a RapidJSON document
class JsonReader
{
public:
  JsonReader() {}
  ~JsonReader() {}
  bool ParseString(const char* a_string, JsonValue& a_value)
  {
    rapidjson::Document document;  // Default template parameter uses UTF8 and MemoryPoolAllocator.
    if (document.Parse(a_string).HasParseError())
      return false;
    a_value = JsonValue(document);
    return true;
  }
  bool ParseFile(const char* a_fileName, JsonValue& a_value)
  {
    std::ifstream inFile(a_fileName);
    if (inFile) {
      /*
      std::streambuf *pbuf = inFile.rdbuf();
      std::streamsize size = pbuf->pubseekoff(0, istr.end);
      pbuf->pubseekoff(0, istr.beg);
      char* contents = new char[size]; // Leaked. Need to keep around while any ref to a_value or its children
      pbuf->sgetn(contents, size);
      inFile.close();
      rapidjson::Document document;  // Default template parameter uses UTF8 and MemoryPoolAllocator.
      if (document.ParseInsitu(contents).HasParseError())
        return false;
      a_value = JsonValue(document);
      return true;
      */
      std::stringstream strStream;
      strStream << inFile.rdbuf();
      inFile.close();
      return ParseString(strStream.str().c_str(), a_value);
    }
    return false;
  }
};


int my_tutorial(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);

    JsonReader reader;
    JsonValue value;

    if (!reader.ParseString(json, value))
      return 1;

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

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

    printf("hello = %s\n", value["hello"].asString().c_str());
    printf("t = %s\n", value["t"].asBool() ? "true" : "false");
    printf("f = %s\n", value["f"].asBool() ? "true" : "false");
    printf("n = %s\n", value["n"].isNull() ? "null" : "?");
    printf("i = %d\n", value["i"].asInt()); // Alternative (int)document["i"]
    printf("pi = %g\n", value["pi"].asDouble());

    {
        const JsonValue& a = value["a"]; // Using a reference for consecutive access is handy and faster.
        assert(a.isArray());
        for (size_t i = 0; i < a.size(); i++) // rapidjson uses SizeType instead of size_t.
            printf("a[%d] = %d\n", int(i), a[i].asInt());
        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;
        value["i"] = f20;    // Alternate form: document["i"].SetUint64(f20)
        assert(!value["i"].isInt()); // No longer can be cast as int or uint.
    }

    // Adding values to array.
    {
        JsonValue& a = value["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.
    {
        value["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.
    JsonValue author;
    {
        char buffer[10];
        int len = sprintf(buffer, "%s %s", "Milo", "Yip");  // synthetic example of dynamically created string.

        author.SetString(buffer, static_cast<size_t>(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(buffer, 0, sizeof(buffer)); // 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");
    puts(value.prettyPrint().c_str());

    return 0;
}

using namespace rapidjson;
using namespace std;



int rapid_tutorial(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 buffer[10];
        int len = sprintf(buffer, "%s %s", "Milo", "Yip");  // synthetic example of dynamically created string.

        author.SetString(buffer, static_cast<size_t>(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(buffer, 0, sizeof(buffer)); // 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;
}







int main(int a, char*b[])
{
  rapid_tutorial(a,b);
  return my_tutorial(a,b);
}