diff options
author | Devtools Arcadia <arcadia-devtools@yandex-team.ru> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/json/easy_parse/json_easy_parser.h | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/json/easy_parse/json_easy_parser.h')
-rw-r--r-- | library/cpp/json/easy_parse/json_easy_parser.h | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/library/cpp/json/easy_parse/json_easy_parser.h b/library/cpp/json/easy_parse/json_easy_parser.h new file mode 100644 index 0000000000..59d7791ab1 --- /dev/null +++ b/library/cpp/json/easy_parse/json_easy_parser.h @@ -0,0 +1,46 @@ +#pragma once + +#include <util/generic/string.h> +#include <util/generic/vector.h> +#include <util/stream/input.h> +#include <util/stream/output.h> +#include "json_easy_parser_impl.h" + +namespace NJson { + /* This class filters out nodes from a source JSON by a xpath-style description. It represent these nodes as a tab-delimited string (or a vector). + * It is useful if you need to parse a data which comes into JSON in a known and fixed format. + * Fields are set as a list of keys separated by slash, for example: + * Field x/y/z in JSON { "x" : { "y" : { "w" : 1, "z" : 2 } } contains number 2. + * In a path to a field you can also provide a special array identifier "[]", identifier of a particular field in an array (for example "[4]") or wildcard "*". + * + * The parser of the class supports parsing of several fields. Each of them could be marked as mandatory or as optional. + * If a mandatory field is not found in JSON, then Parse() returns false and ConvertToTabDelimited() returns an empty string. + * If an optional field is not found in JSON, then it's value in Parse()/ConvertToTabDelimited() is an empty string. + * In particular ConvertToTabDelimited() always returns either an empty string, or a string of the same number of tab-delimited fields starting from the same Prefix. + * + * NB! Library can not extract values of not a simple type (namely it doesn't support the case when a result is a vocabulary or an array) from JSON. + * If you expect such a case, please check json_value.h. + */ + + class TJsonParser { + TString Prefix; + + struct TField { + TVector<TPathElem> Path; + bool NonEmpty; + }; + TVector<TField> Fields; + + friend class TRewriteJsonImpl; + + void ConvertToTabDelimited(IInputStream& in, IOutputStream& out) const; + + public: + void SetPrefix(const TString& prefix) { + Prefix = prefix; + } + void AddField(const TString& path, bool mustExist); + TString ConvertToTabDelimited(const TString& json) const; + bool Parse(const TString& json, TVector<TString>* res) const; + }; +} |