17 template <
typename OutputHandler>
23 outputHandler_(outputHandler), keyString_(keyString), keyLength_(keyLength), filterValueDepth_(), filteredKeyCount_()
26 bool Null() {
return filterValueDepth_ > 0 ? EndValue() : outputHandler_.Null() && EndValue(); }
27 bool Bool(
bool b) {
return filterValueDepth_ > 0 ? EndValue() : outputHandler_.Bool(b) && EndValue(); }
28 bool Int(
int i) {
return filterValueDepth_ > 0 ? EndValue() : outputHandler_.Int(i) && EndValue(); }
29 bool Uint(
unsigned u) {
return filterValueDepth_ > 0 ? EndValue() : outputHandler_.Uint(u) && EndValue(); }
30 bool Int64(
int64_t i) {
return filterValueDepth_ > 0 ? EndValue() : outputHandler_.Int64(i) && EndValue(); }
31 bool Uint64(
uint64_t u) {
return filterValueDepth_ > 0 ? EndValue() : outputHandler_.Uint64(u) && EndValue(); }
32 bool Double(
double d) {
return filterValueDepth_ > 0 ? EndValue() : outputHandler_.Double(d) && EndValue(); }
33 bool RawNumber(
const Ch* str,
SizeType len,
bool copy) {
return filterValueDepth_ > 0 ? EndValue() : outputHandler_.RawNumber(str, len, copy) && EndValue(); }
34 bool String (
const Ch* str,
SizeType len,
bool copy) {
return filterValueDepth_ > 0 ? EndValue() : outputHandler_.String (str, len, copy) && EndValue(); }
37 if (filterValueDepth_ > 0) {
42 filteredKeyCount_.push(0);
43 return outputHandler_.StartObject();
48 if (filterValueDepth_ > 0)
50 else if (len == keyLength_ && std::memcmp(str, keyString_, len) == 0) {
51 filterValueDepth_ = 1;
55 ++filteredKeyCount_.top();
56 return outputHandler_.Key(str, len, copy);
61 if (filterValueDepth_ > 0) {
67 SizeType memberCount = filteredKeyCount_.top();
68 filteredKeyCount_.pop();
69 return outputHandler_.EndObject(memberCount) && EndValue();
74 if (filterValueDepth_ > 0) {
79 return outputHandler_.StartArray();
83 if (filterValueDepth_ > 0) {
88 return outputHandler_.EndArray(elementCount) && EndValue();
96 if (filterValueDepth_ == 1)
97 filterValueDepth_ = 0;
108 int main(
int argc,
char* argv[]) {
110 fprintf(stderr,
"filterkey key < input.json > output.json\n");
116 char readBuffer[65536];
120 char writeBuffer[65536];
129 if (!reader.
Parse(is, filter)) {
ParseResult Parse(InputStream &is, Handler &handler)
Parse JSON text.
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.)
std::stack< SizeType > filteredKeyCount_
ParseErrorCode GetParseErrorCode() const
Get the ParseErrorCode of last parsing.
bool String(const Ch *str, SizeType len, bool copy)
bool RawNumber(const Ch *str, SizeType len, bool copy)
Wrapper of C file stream for input using fread().
int main(int argc, char *argv[])
bool Key(const Ch *str, SizeType len, bool copy)
bool EndArray(SizeType elementCount)
const SizeType keyLength_
unsigned __int64 uint64_t
File byte stream for input using fread().
size_t GetErrorOffset() const
Get the position of last parsing error in input, 0 otherwise.
unsigned filterValueDepth_
OutputHandler & outputHandler_
RAPIDJSON_NAMESPACE_BEGIN const RAPIDJSON_ERROR_CHARTYPE * GetParseError_En(ParseErrorCode parseErrorCode)
Maps error code of parsing into error message.
FilterKeyHandler(OutputHandler &outputHandler, const Ch *keyString, SizeType keyLength)