-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonbuffet.h
55 lines (46 loc) · 2.1 KB
/
jsonbuffet.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#pragma once
#include <cstdlib>
#define RAPIDJSON_NO_SIZETYPEDEFINE
namespace rapidjson { typedef ::std::size_t SizeType; }
#include <rapidjson/document.h>
#include <rapidjson/istreamwrapper.h>
#include <string>
#include <vector>
#include <functional>
#include <iostream>
typedef rapidjson::GenericValue<rapidjson::UTF8<char>> RapidJsonValue;
typedef std::function<bool(rapidjson::SizeType, rapidjson::SizeType, const std::string&, const RapidJsonValue&)> Callback;
std::ostream& operator<<(std::ostream &stream, const std::vector<std::string>& vec);
std::ostream& operator<<(std::ostream &stream, const RapidJsonValue& value);
class JsonBuffet: public rapidjson::BaseReaderHandler<rapidjson::UTF8<>, JsonBuffet> {
public:
JsonBuffet(Callback callback, std::vector<std::string> targetPath = {})
: mCallback(callback),
mTargetPath(targetPath)
{
}
template <typename InputStream>
rapidjson::ParseResult Consume(InputStream& isw) {
return mReader.Parse<rapidjson::kParseIterativeFlag | rapidjson::kParseNumbersAsStringsFlag>(isw, *this);
}
public:
// Annoying that this is part of our public API but we can live with this for now
bool Key(rapidjson::SizeType offset, const char* str, rapidjson::SizeType length, bool copy);
bool String(rapidjson::SizeType offset, const Ch* str, rapidjson::SizeType length, bool copy);
bool RawNumber(rapidjson::SizeType offset, const Ch* str, rapidjson::SizeType length, bool copy);
bool StartObject(rapidjson::SizeType offset);
bool EndObject(rapidjson::SizeType offset, rapidjson::SizeType memberCount);
bool StartArray(rapidjson::SizeType offset);
bool EndArray(rapidjson::SizeType offset, rapidjson::SizeType elementCount);
private:
bool isCurrentPathTarget() const;
bool isCollectingResult() const;
bool collectResult(const RapidJsonValue& value);
private:
Callback mCallback;
std::vector<std::string> mTargetPath;
rapidjson::Reader mReader;
std::vector<std::string> mCurrentPath;
std::vector<rapidjson::SizeType> mCurrentPathOffsets;
std::vector<rapidjson::Document> mCurrentResult;
};