simplereader.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #include "rapidjson/reader.h"
  2. #include <iostream>
  3. using namespace rapidjson;
  4. using namespace std;
  5. struct MyHandler {
  6. bool Null() { cout << "Null()" << endl; return true; }
  7. bool Bool(bool b) { cout << "Bool(" << boolalpha << b << ")" << endl; return true; }
  8. bool Int(int i) { cout << "Int(" << i << ")" << endl; return true; }
  9. bool Uint(unsigned u) { cout << "Uint(" << u << ")" << endl; return true; }
  10. bool Int64(int64_t i) { cout << "Int64(" << i << ")" << endl; return true; }
  11. bool Uint64(uint64_t u) { cout << "Uint64(" << u << ")" << endl; return true; }
  12. bool Double(double d) { cout << "Double(" << d << ")" << endl; return true; }
  13. bool RawNumber(const char* str, SizeType length, bool copy) {
  14. cout << "Number(" << str << ", " << length << ", " << boolalpha << copy << ")" << endl;
  15. return true;
  16. }
  17. bool String(const char* str, SizeType length, bool copy) {
  18. cout << "String(" << str << ", " << length << ", " << boolalpha << copy << ")" << endl;
  19. return true;
  20. }
  21. bool StartObject() { cout << "StartObject()" << endl; return true; }
  22. bool Key(const char* str, SizeType length, bool copy) {
  23. cout << "Key(" << str << ", " << length << ", " << boolalpha << copy << ")" << endl;
  24. return true;
  25. }
  26. bool EndObject(SizeType memberCount) { cout << "EndObject(" << memberCount << ")" << endl; return true; }
  27. bool StartArray() { cout << "StartArray()" << endl; return true; }
  28. bool EndArray(SizeType elementCount) { cout << "EndArray(" << elementCount << ")" << endl; return true; }
  29. };
  30. int main() {
  31. const char json[] = " { \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3, 4] } ";
  32. MyHandler handler;
  33. Reader reader;
  34. StringStream ss(json);
  35. reader.Parse(ss, handler);
  36. return 0;
  37. }