diff options
author | Milo Yip <miloyip@gmail.com> | 2014-07-12 19:07:41 +0400 |
---|---|---|
committer | Milo Yip <miloyip@gmail.com> | 2014-07-12 19:07:41 +0400 |
commit | 3c8a9234399f03d1a4e41b0ef136593dc2667b2c (patch) | |
tree | 03b134371d7ec4fd150765666f52fbcc7b6b453c /example | |
parent | 6225092355e3087c3e73928f54cc1073c55067d6 (diff) |
Add simplereader, simplewriter examples.
Also modify premake to add all projects in example folder.
Diffstat (limited to 'example')
-rw-r--r-- | example/simplereader/simplereader.cpp | 32 | ||||
-rw-r--r-- | example/simplewriter/simplewriter.cpp | 33 |
2 files changed, 65 insertions, 0 deletions
diff --git a/example/simplereader/simplereader.cpp b/example/simplereader/simplereader.cpp new file mode 100644 index 00000000..70df6ba5 --- /dev/null +++ b/example/simplereader/simplereader.cpp @@ -0,0 +1,32 @@ +#include "rapidjson/reader.h" +#include <iostream> + +using namespace rapidjson; +using namespace std; + +struct MyHandler { + bool Null() { cout << "Null()" << endl; return true; } + bool Bool(bool b) { cout << "Bool(" << boolalpha << b << ")" << endl; return true; } + bool Int(int i) { cout << "Int(" << i << ")" << endl; return true; } + bool Uint(unsigned u) { cout << "Uint(" << u << ")" << endl; return true; } + bool Int64(int64_t i) { cout << "Int64(" << i << ")" << endl; return true; } + bool Uint64(uint64_t u) { cout << "Uint64(" << u << ")" << endl; return true; } + bool Double(double d) { cout << "Double(" << d << ")" << endl; return true; } + bool String(const char* str, SizeType length, bool copy) { + cout << "String(" << str << ", " << length << ", " << boolalpha << copy << ")" << endl; + return true; + } + bool StartObject() { cout << "StartObject()" << endl; return true; } + bool EndObject(SizeType memberCount) { cout << "EndObject(" << memberCount << ")" << endl; return true; } + bool StartArray() { cout << "StartArray()" << endl; return true; } + bool EndArray(SizeType elementCount) { cout << "EndArray(" << elementCount << ")" << endl; return true; } +}; + +void main() { + const char json[] = " { \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3, 4] } "; + + MyHandler handler; + Reader reader; + StringStream ss(json); + reader.Parse(ss, handler); +} diff --git a/example/simplewriter/simplewriter.cpp b/example/simplewriter/simplewriter.cpp new file mode 100644 index 00000000..98da2cbd --- /dev/null +++ b/example/simplewriter/simplewriter.cpp @@ -0,0 +1,33 @@ +#include "rapidjson/writer.h" +#include "rapidjson/stringbuffer.h" +#include <iostream> + +using namespace rapidjson; +using namespace std; + +void main() { + StringBuffer s; + Writer<StringBuffer> writer(s); + + writer.StartObject(); + writer.String("hello"); + writer.String("world"); + writer.String("t"); + writer.Bool(true); + writer.String("f"); + writer.Bool(false); + writer.String("n"); + writer.Null(); + writer.String("i"); + writer.Uint(123); + writer.String("pi"); + writer.Double(3.1416); + writer.String("a"); + writer.StartArray(); + for (unsigned i = 0; i < 4; i++) + writer.Uint(i); + writer.EndArray(); + writer.EndObject(); + + cout << s.GetString() << endl; +} |