simpledom.cpp 643 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// JSON simple example
// This example does not handle errors.

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

using namespace rapidjson;

int main() {
	// 1. Parse a JSON string into DOM.
	const char* json = "{\"project\":\"rapidjson\",\"stars\":10}";
	Document d;
15
	d.Parse(json);
16 17

	// 2. Modify it by DOM.
18 19
	Value& s = d["stars"];
	s.SetInt(s.GetInt() + 1);
20 21 22 23 24 25

	// 3. Stringify the DOM
	StringBuffer buffer;
	Writer<StringBuffer> writer(buffer);
	d.Accept(writer);

Milo Yip's avatar
Milo Yip committed
26
	// Output {"project":"rapidjson","stars":11}
27 28 29
	std::cout << buffer.GetString() << std::endl;
	return 0;
}