filterkey.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // JSON filterkey example with SAX-style API.
  2. // This example parses JSON text from stdin with validation.
  3. // During parsing, specified key will be filtered using a SAX handler.
  4. // It re-output the JSON content to stdout without whitespace.
  5. #include "rapidjson/reader.h"
  6. #include "rapidjson/writer.h"
  7. #include "rapidjson/filereadstream.h"
  8. #include "rapidjson/filewritestream.h"
  9. #include "rapidjson/error/en.h"
  10. #include <stack>
  11. using namespace rapidjson;
  12. // This handler forwards event into an output handler, with filtering the descendent events of specified key.
  13. template <typename OutputHandler>
  14. class FilterKeyHandler {
  15. public:
  16. typedef char Ch;
  17. FilterKeyHandler(OutputHandler& outputHandler, const Ch* keyString, SizeType keyLength) :
  18. outputHandler_(outputHandler), keyString_(keyString), keyLength_(keyLength), filterValueDepth_(), filteredKeyCount_()
  19. {}
  20. bool Null() { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.Null() && EndValue(); }
  21. bool Bool(bool b) { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.Bool(b) && EndValue(); }
  22. bool Int(int i) { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.Int(i) && EndValue(); }
  23. bool Uint(unsigned u) { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.Uint(u) && EndValue(); }
  24. bool Int64(int64_t i) { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.Int64(i) && EndValue(); }
  25. bool Uint64(uint64_t u) { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.Uint64(u) && EndValue(); }
  26. bool Double(double d) { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.Double(d) && EndValue(); }
  27. bool RawNumber(const Ch* str, SizeType len, bool copy) { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.RawNumber(str, len, copy) && EndValue(); }
  28. bool String (const Ch* str, SizeType len, bool copy) { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.String (str, len, copy) && EndValue(); }
  29. bool StartObject() {
  30. if (filterValueDepth_ > 0) {
  31. filterValueDepth_++;
  32. return true;
  33. }
  34. else {
  35. filteredKeyCount_.push(0);
  36. return outputHandler_.StartObject();
  37. }
  38. }
  39. bool Key(const Ch* str, SizeType len, bool copy) {
  40. if (filterValueDepth_ > 0)
  41. return true;
  42. else if (len == keyLength_ && std::memcmp(str, keyString_, len) == 0) {
  43. filterValueDepth_ = 1;
  44. return true;
  45. }
  46. else {
  47. ++filteredKeyCount_.top();
  48. return outputHandler_.Key(str, len, copy);
  49. }
  50. }
  51. bool EndObject(SizeType) {
  52. if (filterValueDepth_ > 0) {
  53. filterValueDepth_--;
  54. return EndValue();
  55. }
  56. else {
  57. // Use our own filtered memberCount
  58. SizeType memberCount = filteredKeyCount_.top();
  59. filteredKeyCount_.pop();
  60. return outputHandler_.EndObject(memberCount) && EndValue();
  61. }
  62. }
  63. bool StartArray() {
  64. if (filterValueDepth_ > 0) {
  65. filterValueDepth_++;
  66. return true;
  67. }
  68. else
  69. return outputHandler_.StartArray();
  70. }
  71. bool EndArray(SizeType elementCount) {
  72. if (filterValueDepth_ > 0) {
  73. filterValueDepth_--;
  74. return EndValue();
  75. }
  76. else
  77. return outputHandler_.EndArray(elementCount) && EndValue();
  78. }
  79. private:
  80. FilterKeyHandler(const FilterKeyHandler&);
  81. FilterKeyHandler& operator=(const FilterKeyHandler&);
  82. bool EndValue() {
  83. if (filterValueDepth_ == 1) // Just at the end of value after filtered key
  84. filterValueDepth_ = 0;
  85. return true;
  86. }
  87. OutputHandler& outputHandler_;
  88. const char* keyString_;
  89. const SizeType keyLength_;
  90. unsigned filterValueDepth_;
  91. std::stack<SizeType> filteredKeyCount_;
  92. };
  93. int main(int argc, char* argv[]) {
  94. if (argc != 2) {
  95. fprintf(stderr, "filterkey key < input.json > output.json\n");
  96. return 1;
  97. }
  98. // Prepare JSON reader and input stream.
  99. Reader reader;
  100. char readBuffer[65536];
  101. FileReadStream is(stdin, readBuffer, sizeof(readBuffer));
  102. // Prepare JSON writer and output stream.
  103. char writeBuffer[65536];
  104. FileWriteStream os(stdout, writeBuffer, sizeof(writeBuffer));
  105. Writer<FileWriteStream> writer(os);
  106. // Prepare Filter
  107. FilterKeyHandler<Writer<FileWriteStream> > filter(writer, argv[1], static_cast<SizeType>(strlen(argv[1])));
  108. // JSON reader parse from the input stream, filter handler filters the events, and forward to writer.
  109. // i.e. the events flow is: reader -> filter -> writer
  110. if (!reader.Parse(is, filter)) {
  111. fprintf(stderr, "\nError(%u): %s\n", static_cast<unsigned>(reader.GetErrorOffset()), GetParseError_En(reader.GetParseErrorCode()));
  112. return 1;
  113. }
  114. return 0;
  115. }