Cpp Libraries

Table of Contents

1 RapidJson

  #include <rapidjson/istreamwrapper.h>
  #include <rapidjson/document.h>
  #include <rapidjson/prettywriter.h>

1.1 Parse Document

  const char* json = "{\"project\":\"rapidjson\",\"stars\":10}";
  Document d;
  d.Parse(json);

  rapidjson::Document document;
  std::ifstream ifs(p.string());
  rapidjson::IStreamWrapper isw(ifs);
  document.ParseStream(isw);
  assert(document.IsArray());

1.2 Create Document

  rapidjson::Document doc;
  doc.SetObject();
  doc.SetArray();

1.3 Access Document

  for (rapidjson::Value &field : v["fields"].GetArray()) {
    Fields.push_back(field.GetString());
   }

1.4 Create Value & Save Document

  // rapidjson::kObjectType
  Value array(kArrayType);
  Document::AllocatorType &allocator = document.GetAllocator();
  array.PushBack("hello", allocator).PushBack("world", allocator);

  document.AddMember("array", array, allocator);

  document.AddMember("Name", "XYZ", allocator);
  document.AddMember("Name", "5", allocator);
  document.AddMember("Name", 5, allocator);

  rapidjson::Value str;
  std::string Name = "hello";
  str.SetString(Name.c_str(), allocator);

1.5 Save

  rapidjson::StringBuffer sb;
  rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
  rapidjson::Writer<rapidjson::StringBuffer> writer(sb);
  v.Accept(writer);
  os << sb.GetString() << "\n";

1.6 Reference

1.6.1 GenericDocument : public GenericValue

  • Allocator GetAllocator()
  • ParseStream
  • Parse

1.6.2 GenericValue

1.6.2.1 Predicate
  • IsNull
  • IsBool
  • IsObject
  • IsArray
  • IsNumber
  • IsInt
  • IsString
1.6.2.2 Object
  • SetObject()
  • GetObject
  • operator[]
  • MemberBegin
  • MemberEnd
  • HasMember
  • FindMember
  • AddMember(name, value, allocator)
  • RemoveMember
1.6.2.3 Array
  • SetArray
  • GetArray
  • Size
  • Empty()
  • Clear
  • operator[](index)
  • Begin
  • End
  • PushBack
  • PopBack
  • Erase
1.6.2.4 Other
  • SetBool()
  • GetBool
  • GetInt
  • SetInt
  • GetString
  • SetString