RapidJson custom stream for reading JSON stored in multiple buffers http://rapidjson.org/md_doc_stream.html#CustomStream
| 55 | /// RapidJson custom stream for reading JSON stored in multiple buffers |
| 56 | /// http://rapidjson.org/md_doc_stream.html#CustomStream |
| 57 | class MultiStringStream { |
| 58 | public: |
| 59 | using Ch = char; |
| 60 | explicit MultiStringStream(std::vector<string_view> strings) |
| 61 | : strings_(std::move(strings)) { |
| 62 | std::reverse(strings_.begin(), strings_.end()); |
| 63 | } |
| 64 | explicit MultiStringStream(const BufferVector& buffers) : strings_(buffers.size()) { |
| 65 | for (size_t i = 0; i < buffers.size(); ++i) { |
| 66 | strings_[i] = string_view(*buffers[i]); |
| 67 | } |
| 68 | std::reverse(strings_.begin(), strings_.end()); |
| 69 | } |
| 70 | char Peek() const { |
| 71 | if (strings_.size() == 0) return '\0'; |
| 72 | return strings_.back()[0]; |
| 73 | } |
| 74 | char Take() { |
| 75 | if (strings_.size() == 0) return '\0'; |
| 76 | char taken = strings_.back()[0]; |
| 77 | if (strings_.back().size() == 1) { |
| 78 | strings_.pop_back(); |
| 79 | } else { |
| 80 | strings_.back() = strings_.back().substr(1); |
| 81 | } |
| 82 | ++index_; |
| 83 | return taken; |
| 84 | } |
| 85 | size_t Tell() { return index_; } |
| 86 | void Put(char) { ARROW_LOG(FATAL) << "not implemented"; } |
| 87 | void Flush() { ARROW_LOG(FATAL) << "not implemented"; } |
| 88 | char* PutBegin() { |
| 89 | ARROW_LOG(FATAL) << "not implemented"; |
| 90 | return nullptr; |
| 91 | } |
| 92 | size_t PutEnd(char*) { |
| 93 | ARROW_LOG(FATAL) << "not implemented"; |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | private: |
| 98 | size_t index_ = 0; |
| 99 | std::vector<string_view> strings_; |
| 100 | }; |
| 101 | |
| 102 | template <typename Stream> |
| 103 | static size_t ConsumeWholeObject(Stream&& stream) { |