diff options
author | Ruslan Kovalev <ruslan.a.kovalev@gmail.com> | 2022-02-10 16:46:44 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:46:44 +0300 |
commit | 59e19371de37995fcb36beb16cd6ec030af960bc (patch) | |
tree | fa68e36093ebff8b805462e9e6d331fe9d348214 /library/cpp/json | |
parent | 89db6fe2fe2c32d2a832ddfeb04e8d078e301084 (diff) | |
download | ydb-59e19371de37995fcb36beb16cd6ec030af960bc.tar.gz |
Restoring authorship annotation for Ruslan Kovalev <ruslan.a.kovalev@gmail.com>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/json')
-rw-r--r-- | library/cpp/json/easy_parse/json_easy_parser_impl.h | 12 | ||||
-rw-r--r-- | library/cpp/json/fast_sax/parser.rl6 | 566 | ||||
-rw-r--r-- | library/cpp/json/json_prettifier.cpp | 126 | ||||
-rw-r--r-- | library/cpp/json/json_prettifier.h | 32 | ||||
-rw-r--r-- | library/cpp/json/json_reader.cpp | 20 | ||||
-rw-r--r-- | library/cpp/json/json_reader.h | 26 | ||||
-rw-r--r-- | library/cpp/json/json_writer.cpp | 10 | ||||
-rw-r--r-- | library/cpp/json/json_writer.h | 8 | ||||
-rw-r--r-- | library/cpp/json/ut/json_prettifier_ut.cpp | 92 | ||||
-rw-r--r-- | library/cpp/json/ut/json_reader_fast_ut.cpp | 186 | ||||
-rw-r--r-- | library/cpp/json/ut/json_reader_ut.cpp | 2 | ||||
-rw-r--r-- | library/cpp/json/ut/json_writer_ut.cpp | 60 | ||||
-rw-r--r-- | library/cpp/json/writer/json.h | 4 | ||||
-rw-r--r-- | library/cpp/json/writer/json_value.cpp | 78 | ||||
-rw-r--r-- | library/cpp/json/writer/json_value.h | 4 | ||||
-rw-r--r-- | library/cpp/json/ya.make | 2 |
16 files changed, 614 insertions, 614 deletions
diff --git a/library/cpp/json/easy_parse/json_easy_parser_impl.h b/library/cpp/json/easy_parse/json_easy_parser_impl.h index ec55d838b3..f40cbfa2e2 100644 --- a/library/cpp/json/easy_parse/json_easy_parser_impl.h +++ b/library/cpp/json/easy_parse/json_easy_parser_impl.h @@ -17,16 +17,16 @@ namespace NJson { int ArrayCounter; TPathElemImpl(NImpl::EType type) - : Type(type) - , ArrayCounter() - { + : Type(type) + , ArrayCounter() + { } TPathElemImpl(const TStringType& key) : Type(NImpl::MAP_KEY) - , Key(key) - , ArrayCounter() - { + , Key(key) + , ArrayCounter() + { } TPathElemImpl(int arrayCounter) diff --git a/library/cpp/json/fast_sax/parser.rl6 b/library/cpp/json/fast_sax/parser.rl6 index edb4e9ee1b..b7b7900071 100644 --- a/library/cpp/json/fast_sax/parser.rl6 +++ b/library/cpp/json/fast_sax/parser.rl6 @@ -1,215 +1,215 @@ #include <library/cpp/json/fast_sax/unescape.h> #include <library/cpp/json/fast_sax/parser.h> - -#include <util/string/cast.h> + +#include <util/string/cast.h> #include <util/generic/buffer.h> -#include <util/generic/strbuf.h> -#include <util/generic/ymath.h> - -namespace NJson { - -enum EStoredStr { - SS_NONE = 0, SS_NOCOPY, SS_MUSTCOPY -}; - -struct TParserCtx { - TJsonCallbacks& Hndl; - +#include <util/generic/strbuf.h> +#include <util/generic/ymath.h> + +namespace NJson { + +enum EStoredStr { + SS_NONE = 0, SS_NOCOPY, SS_MUSTCOPY +}; + +struct TParserCtx { + TJsonCallbacks& Hndl; + TBuffer Buffer; - TStringBuf String; - EStoredStr Stored = SS_NONE; - bool ExpectValue = true; - - const char* p0 = nullptr; - const char* p = nullptr; - const char* pe = nullptr; - const char* eof = nullptr; - const char* ts = nullptr; - const char* te = nullptr; - int cs = 0; - int act = 0; - - TParserCtx(TJsonCallbacks& h, TStringBuf data) - : Hndl(h) + TStringBuf String; + EStoredStr Stored = SS_NONE; + bool ExpectValue = true; + + const char* p0 = nullptr; + const char* p = nullptr; + const char* pe = nullptr; + const char* eof = nullptr; + const char* ts = nullptr; + const char* te = nullptr; + int cs = 0; + int act = 0; + + TParserCtx(TJsonCallbacks& h, TStringBuf data) + : Hndl(h) , p0(data.data()) , p(data.data()) - , pe(data.end()) - , eof(data.end()) - {} - - static inline bool GoodPtrs(const char* b, const char* e) { - return b && e && b <= e; - } - + , pe(data.end()) + , eof(data.end()) + {} + + static inline bool GoodPtrs(const char* b, const char* e) { + return b && e && b <= e; + } + bool OnError(TStringBuf reason = TStringBuf(""), bool end = false) const { - size_t off = 0; - TStringBuf token; - - if (GoodPtrs(p0, ts)) { - off = ts - p0; - } else if (end && GoodPtrs(p0, pe)) { - off = pe - p0; - } - - if (GoodPtrs(ts, te)) { - token = TStringBuf(ts, te); - } - - if (!token) { - Hndl.OnError(off, reason); - } else { + size_t off = 0; + TStringBuf token; + + if (GoodPtrs(p0, ts)) { + off = ts - p0; + } else if (end && GoodPtrs(p0, pe)) { + off = pe - p0; + } + + if (GoodPtrs(ts, te)) { + token = TStringBuf(ts, te); + } + + if (!token) { + Hndl.OnError(off, reason); + } else { Hndl.OnError(off, TString::Join(reason, " at token: '", token, "'")); - } - - return false; - } - - bool OnVal() { - if (Y_UNLIKELY(!ExpectValue)) { - return false; - } - ExpectValue = false; - return true; - } - - bool OnNull() { - return Y_LIKELY(OnVal()) - && Hndl.OnNull(); - } - - bool OnTrue() { - return Y_LIKELY(OnVal()) - && Hndl.OnBoolean(true); - } - - bool OnFalse() { - return Y_LIKELY(OnVal()) - && Hndl.OnBoolean(false); - } - - bool OnPInt() { - unsigned long long res = 0; - return Y_LIKELY(OnVal()) - && TryFromString<unsigned long long>(TStringBuf(ts, te), res) - && Hndl.OnUInteger(res); - } - - bool OnNInt() { - long long res = 0; - return Y_LIKELY(OnVal()) - && TryFromString<long long>(TStringBuf(ts, te), res) - && Hndl.OnInteger(res); - } - - bool OnFlt() { - double res = 0; - return Y_LIKELY(OnVal()) - && TryFromString<double>(TStringBuf(ts, te), res) - && IsFinite(res) - && Hndl.OnDouble(res); - } - - bool OnMapOpen() { - bool res = Y_LIKELY(OnVal()) - && Hndl.OnOpenMap(); - ExpectValue = true; - return res; - } - - bool OnArrOpen() { - bool res = Y_LIKELY(OnVal()) - && Hndl.OnOpenArray(); - ExpectValue = true; - return res; - } - - bool OnString(TStringBuf s, EStoredStr t) { + } + + return false; + } + + bool OnVal() { + if (Y_UNLIKELY(!ExpectValue)) { + return false; + } + ExpectValue = false; + return true; + } + + bool OnNull() { + return Y_LIKELY(OnVal()) + && Hndl.OnNull(); + } + + bool OnTrue() { + return Y_LIKELY(OnVal()) + && Hndl.OnBoolean(true); + } + + bool OnFalse() { + return Y_LIKELY(OnVal()) + && Hndl.OnBoolean(false); + } + + bool OnPInt() { + unsigned long long res = 0; + return Y_LIKELY(OnVal()) + && TryFromString<unsigned long long>(TStringBuf(ts, te), res) + && Hndl.OnUInteger(res); + } + + bool OnNInt() { + long long res = 0; + return Y_LIKELY(OnVal()) + && TryFromString<long long>(TStringBuf(ts, te), res) + && Hndl.OnInteger(res); + } + + bool OnFlt() { + double res = 0; + return Y_LIKELY(OnVal()) + && TryFromString<double>(TStringBuf(ts, te), res) + && IsFinite(res) + && Hndl.OnDouble(res); + } + + bool OnMapOpen() { + bool res = Y_LIKELY(OnVal()) + && Hndl.OnOpenMap(); + ExpectValue = true; + return res; + } + + bool OnArrOpen() { + bool res = Y_LIKELY(OnVal()) + && Hndl.OnOpenArray(); + ExpectValue = true; + return res; + } + + bool OnString(TStringBuf s, EStoredStr t) { if (Y_LIKELY(OnVal())) { - String = s; - Stored = t; - return true; - } else { - return false; - } - } - - bool OnStrU() { - return OnString(TStringBuf(ts, te), SS_NOCOPY); - } - - bool OnStrQ() { - return OnString(TStringBuf(ts + 1, te - 1), SS_NOCOPY); - } - - bool OnStrE() { + String = s; + Stored = t; + return true; + } else { + return false; + } + } + + bool OnStrU() { + return OnString(TStringBuf(ts, te), SS_NOCOPY); + } + + bool OnStrQ() { + return OnString(TStringBuf(ts + 1, te - 1), SS_NOCOPY); + } + + bool OnStrE() { Buffer.Clear(); Buffer.Reserve(2 * (te - ts)); return OnString(UnescapeJsonUnicode(TStringBuf(ts + 1, te - ts - 2), Buffer.data()), SS_MUSTCOPY); - } - - bool OnMapClose() { - ExpectValue = false; - return Y_LIKELY(OnAfterVal()) - && Hndl.OnCloseMap(); - } - - bool OnArrClose() { - ExpectValue = false; - return Y_LIKELY(OnAfterVal()) - && Hndl.OnCloseArray(); - } - - bool OnColon() { - if (ExpectValue) { - return false; - } - - ExpectValue = true; - const auto stored = Stored; - Stored = SS_NONE; - - switch (stored) { + } + + bool OnMapClose() { + ExpectValue = false; + return Y_LIKELY(OnAfterVal()) + && Hndl.OnCloseMap(); + } + + bool OnArrClose() { + ExpectValue = false; + return Y_LIKELY(OnAfterVal()) + && Hndl.OnCloseArray(); + } + + bool OnColon() { + if (ExpectValue) { + return false; + } + + ExpectValue = true; + const auto stored = Stored; + Stored = SS_NONE; + + switch (stored) { default: return false; - case SS_NOCOPY: - return Hndl.OnMapKeyNoCopy(String); - case SS_MUSTCOPY: - return Hndl.OnMapKey(String); - } - } - - bool OnAfterVal() { - const auto stored = Stored; - Stored = SS_NONE; - - switch (stored) { - default: - return true; - case SS_NOCOPY: - return Hndl.OnStringNoCopy(String); - case SS_MUSTCOPY: - return Hndl.OnString(String); - } - } - - bool OnComma() { - if (Y_UNLIKELY(ExpectValue)) { - return false; - } - ExpectValue = true; - return OnAfterVal(); - } - - bool Parse(); -}; - -#if 0 -%%{ -machine fastjson; - -alphtype char; - + case SS_NOCOPY: + return Hndl.OnMapKeyNoCopy(String); + case SS_MUSTCOPY: + return Hndl.OnMapKey(String); + } + } + + bool OnAfterVal() { + const auto stored = Stored; + Stored = SS_NONE; + + switch (stored) { + default: + return true; + case SS_NOCOPY: + return Hndl.OnStringNoCopy(String); + case SS_MUSTCOPY: + return Hndl.OnString(String); + } + } + + bool OnComma() { + if (Y_UNLIKELY(ExpectValue)) { + return false; + } + ExpectValue = true; + return OnAfterVal(); + } + + bool Parse(); +}; + +#if 0 +%%{ +machine fastjson; + +alphtype char; + action OnNull { if (Y_UNLIKELY(!OnNull())) goto TOKEN_ERROR; } action OnTrue { if (Y_UNLIKELY(!OnTrue())) goto TOKEN_ERROR; } action OnFalse { if (Y_UNLIKELY(!OnFalse())) goto TOKEN_ERROR; } @@ -225,90 +225,90 @@ action OnArrO { if (Y_UNLIKELY(!OnArrOpen())) goto TOKEN_ERROR; } action OnArrC { if (Y_UNLIKELY(!OnArrClose())) goto TOKEN_ERROR; } action OnComma { if (Y_UNLIKELY(!OnComma())) goto TOKEN_ERROR; } action OnColon { if (Y_UNLIKELY(!OnColon())) goto TOKEN_ERROR; } -action OnError { goto TOKEN_ERROR; } - -comment1 = "/*" (any* -- "*/") "*/"; - -pint = [0-9]+; -nint = '-'[0-9]+; -flt = '-'?[0-9.][0-9.eE+\-]+; - -uchar0 = [a-zA-Z_@$] | (0x80 .. 0xFF); -uchar = uchar0 | digit | [.\-]; - -qchar = [^'\\]; #'; -dchar = [^"\\]; #"; - -echar = "\\" any; - -qechar = qchar | echar; -dechar = dchar | echar; - -strq = "'" qchar* "'"; -strd = '"' dchar* '"'; - -strqe = "'" qechar* "'"; -strde = '"' dechar* '"'; - -strU = uchar0 uchar*; -strQ = strq | strd; -strE = strqe | strde; - -ws = (0x00 .. 0x20) | 0x7F; -sp = ws+; - -main := |* - 'null' => OnNull; - 'true' => OnTrue; - 'false' => OnFalse; - - pint => OnPInt; - nint => OnNInt; - flt => OnFlt; - - strU => OnStrU; - strQ => OnStrQ; - strE => OnStrE; - - ',' => OnComma; - ':' => OnColon; - - '{' => OnDictO; - '}' => OnDictC; - '[' => OnArrO; - ']' => OnArrC; - - sp; - comment1; - - (flt | pint | nint) (any - (ws | ',' | ':' | '{' | '}' | '[' | ']')) => OnError; - - any => OnError; - *|; -}%% -#endif - -bool TParserCtx::Parse() { - try { - %%{ - write data noerror nofinal; - write init; - write exec; - }%% - ; +action OnError { goto TOKEN_ERROR; } + +comment1 = "/*" (any* -- "*/") "*/"; + +pint = [0-9]+; +nint = '-'[0-9]+; +flt = '-'?[0-9.][0-9.eE+\-]+; + +uchar0 = [a-zA-Z_@$] | (0x80 .. 0xFF); +uchar = uchar0 | digit | [.\-]; + +qchar = [^'\\]; #'; +dchar = [^"\\]; #"; + +echar = "\\" any; + +qechar = qchar | echar; +dechar = dchar | echar; + +strq = "'" qchar* "'"; +strd = '"' dchar* '"'; + +strqe = "'" qechar* "'"; +strde = '"' dechar* '"'; + +strU = uchar0 uchar*; +strQ = strq | strd; +strE = strqe | strde; + +ws = (0x00 .. 0x20) | 0x7F; +sp = ws+; + +main := |* + 'null' => OnNull; + 'true' => OnTrue; + 'false' => OnFalse; + + pint => OnPInt; + nint => OnNInt; + flt => OnFlt; + + strU => OnStrU; + strQ => OnStrQ; + strE => OnStrE; + + ',' => OnComma; + ':' => OnColon; + + '{' => OnDictO; + '}' => OnDictC; + '[' => OnArrO; + ']' => OnArrC; + + sp; + comment1; + + (flt | pint | nint) (any - (ws | ',' | ':' | '{' | '}' | '[' | ']')) => OnError; + + any => OnError; + *|; +}%% +#endif + +bool TParserCtx::Parse() { + try { + %%{ + write data noerror nofinal; + write init; + write exec; + }%% + ; Y_UNUSED(fastjson_en_main); - } catch (const TFromStringException& e) { - return OnError(e.what()); - } - - return OnAfterVal() && Hndl.OnEnd() || OnError("invalid or truncated", true); - - TOKEN_ERROR: - return OnError("invalid syntax"); -} - -bool ReadJsonFast(TStringBuf data, TJsonCallbacks* h) { - return TParserCtx(*h, data).Parse(); -} - -} + } catch (const TFromStringException& e) { + return OnError(e.what()); + } + + return OnAfterVal() && Hndl.OnEnd() || OnError("invalid or truncated", true); + + TOKEN_ERROR: + return OnError("invalid syntax"); +} + +bool ReadJsonFast(TStringBuf data, TJsonCallbacks* h) { + return TParserCtx(*h, data).Parse(); +} + +} diff --git a/library/cpp/json/json_prettifier.cpp b/library/cpp/json/json_prettifier.cpp index bb16aab44e..2fc85420bc 100644 --- a/library/cpp/json/json_prettifier.cpp +++ b/library/cpp/json/json_prettifier.cpp @@ -1,61 +1,61 @@ -#include "json_prettifier.h" - -#include <util/generic/deque.h> +#include "json_prettifier.h" + +#include <util/generic/deque.h> #include <util/generic/algorithm.h> -#include <util/memory/pool.h> -#include <util/string/util.h> - +#include <util/memory/pool.h> +#include <util/string/util.h> + #include <library/cpp/string_utils/relaxed_escaper/relaxed_escaper.h> - -namespace NJson { + +namespace NJson { struct TRewritableOut { IOutputStream& Slave; - + char Last = 0; bool Dirty = false; - + TRewritableOut(IOutputStream& sl) : Slave(sl) { } - + template <typename T> void Write(const T& t) { Flush(); Slave << t; } - + void Hold(char c) { if (Dirty) Flush(); Last = c; Dirty = true; } - + void Flush() { if (Dirty) { Slave << Last; Dirty = false; } } - + void Revert() { - Dirty = false; - } + Dirty = false; + } }; - + struct TSpaces { char S[256]; - + TSpaces() { memset(&S, ' ', sizeof(S)); } - + TStringBuf Get(ui8 sz) const { return TStringBuf(S, sz); } }; - + bool TJsonPrettifier::MayUnquoteNew(TStringBuf s) { static str_spn alpha("a-zA-Z_@$", true); static str_spn alnum("a-zA-Z_@$0-9.-", true); @@ -64,8 +64,8 @@ namespace NJson { static TStringBuf null0("null"); return !!s && alpha.chars_table[(ui8)s[0]] && alnum.cbrk(s.begin() + 1, s.end()) == s.end() && !EqualToOneOf(s, null0, true0, false0); - } - + } + // to keep arcadia tests happy bool TJsonPrettifier::MayUnquoteOld(TStringBuf s) { static str_spn alpha("a-zA-Z_@$", true); @@ -77,24 +77,24 @@ namespace NJson { static TStringBuf true2("da"); static TStringBuf false2("net"); static TStringBuf null0("null"); - + return !!s && alpha.chars_table[(ui8)s[0]] && alnum.cbrk(s.begin() + 1, s.end()) == s.end() && !EqualToOneOf(s, null0, true0, false0, true1, false1, true2, false2); } - + class TPrettifier: public TJsonCallbacks { TRewritableOut Out; TStringBuf Spaces; TStringBuf Quote; TStringBuf Unsafe; TStringBuf Safe; - + ui32 Level = 0; ui32 MaxPaddingLevel; - + bool Unquote = false; bool Compactify = false; bool NewUnquote = false; - + public: TPrettifier(IOutputStream& out, const TJsonPrettifier& p) : Out(out) @@ -113,7 +113,7 @@ namespace NJson { Safe = "'"; } } - + void Pad(bool close = false) { if (Compactify) { Out.Flush(); @@ -130,23 +130,23 @@ namespace NJson { Out.Write(Spaces); } } - + void WriteSpace(char sp) { if (Compactify) { Out.Flush(); return; } - + Out.Write(sp); - } - + } + void OnVal() { if (Out.Dirty && ':' == Out.Last) { WriteSpace(' '); } else { Pad(); } - } + } void AfterVal() { Out.Hold(','); @@ -158,28 +158,28 @@ namespace NJson { Out.Write(t); AfterVal(); return true; - } + } bool OnNull() override { return WriteVal(TStringBuf("null")); - } - + } + bool OnBoolean(bool v) override { return WriteVal(v ? TStringBuf("true") : TStringBuf("false")); - } - + } + bool OnInteger(long long i) override { return WriteVal(i); } - + bool OnUInteger(unsigned long long i) override { return WriteVal(i); - } - + } + bool OnDouble(double d) override { return WriteVal(d); } - + void WriteString(TStringBuf s) { if (Unquote && (NewUnquote ? TJsonPrettifier::MayUnquoteNew(s) : TJsonPrettifier::MayUnquoteOld(s))) { Out.Slave << s; @@ -189,53 +189,53 @@ namespace NJson { Out.Slave << Quote; } } - + bool OnString(const TStringBuf& s) override { OnVal(); WriteString(s); AfterVal(); return true; } - + bool OnOpen(char c) { OnVal(); Level++; Out.Hold(c); return true; } - + bool OnOpenMap() override { return OnOpen('{'); } - + bool OnOpenArray() override { return OnOpen('['); } - + bool OnMapKey(const TStringBuf& k) override { OnVal(); WriteString(k); WriteSpace(' '); Out.Hold(':'); return true; - } - + } + bool OnClose(char c) { if (!Level) return false; - + Level--; - + if (Out.Dirty && c == Out.Last) { WriteSpace(' '); } else { Out.Revert(); Pad(true); } - + return true; } - + bool OnCloseMap() override { if (!OnClose('{')) return false; @@ -243,7 +243,7 @@ namespace NJson { AfterVal(); return true; } - + bool OnCloseArray() override { if (!OnClose('[')) return false; @@ -251,27 +251,27 @@ namespace NJson { AfterVal(); return true; } - + bool OnEnd() override { return !Level; } }; - + bool TJsonPrettifier::Prettify(TStringBuf in, IOutputStream& out) const { TPrettifier p(out, *this); if (Strict) { TMemoryInput mIn(in.data(), in.size()); return ReadJson(&mIn, &p); - } else { + } else { return ReadJsonFast(in, &p); - } - } - + } + } + TString TJsonPrettifier::Prettify(TStringBuf in) const { TStringStream s; if (Prettify(in, s)) return s.Str(); return TString(); - } - -} + } + +} diff --git a/library/cpp/json/json_prettifier.h b/library/cpp/json/json_prettifier.h index 27d611b0b4..70473ce0e7 100644 --- a/library/cpp/json/json_prettifier.h +++ b/library/cpp/json/json_prettifier.h @@ -1,10 +1,10 @@ -#pragma once - -#include "json_reader.h" - +#pragma once + +#include "json_reader.h" + #include <util/generic/ylimits.h> -namespace NJson { +namespace NJson { struct TJsonPrettifier { bool Unquote = false; ui8 Padding = 4; @@ -13,7 +13,7 @@ namespace NJson { bool Strict = false; bool NewUnquote = false; // use new unquote, may break old tests ui32 MaxPaddingLevel = Max<ui32>(); - + static TJsonPrettifier Prettifier(bool unquote = false, ui8 padding = 4, bool singlequotes = false) { TJsonPrettifier p; p.Unquote = unquote; @@ -21,7 +21,7 @@ namespace NJson { p.SingleQuotes = singlequotes; return p; } - + static TJsonPrettifier Compactifier(bool unquote = false, bool singlequote = false) { TJsonPrettifier p; p.Unquote = unquote; @@ -30,29 +30,29 @@ namespace NJson { p.SingleQuotes = singlequote; return p; } - + bool Prettify(TStringBuf in, IOutputStream& out) const; - + TString Prettify(TStringBuf in) const; - + static bool MayUnquoteNew(TStringBuf in); static bool MayUnquoteOld(TStringBuf in); }; - + inline TString PrettifyJson(TStringBuf in, bool unquote = false, ui8 padding = 4, bool sq = false) { return TJsonPrettifier::Prettifier(unquote, padding, sq).Prettify(in); } - + inline bool PrettifyJson(TStringBuf in, IOutputStream& out, bool unquote = false, ui8 padding = 4, bool sq = false) { return TJsonPrettifier::Prettifier(unquote, padding, sq).Prettify(in, out); } - + inline bool CompactifyJson(TStringBuf in, IOutputStream& out, bool unquote = false, bool sq = false) { return TJsonPrettifier::Compactifier(unquote, sq).Prettify(in, out); } - + inline TString CompactifyJson(TStringBuf in, bool unquote = false, bool sq = false) { return TJsonPrettifier::Compactifier(unquote, sq).Prettify(in); } - -} + +} diff --git a/library/cpp/json/json_reader.cpp b/library/cpp/json/json_reader.cpp index 072c8deafe..5162280d62 100644 --- a/library/cpp/json/json_reader.cpp +++ b/library/cpp/json/json_reader.cpp @@ -2,24 +2,24 @@ #include "rapidjson_helpers.h" -#include <contrib/libs/rapidjson/include/rapidjson/error/en.h> -#include <contrib/libs/rapidjson/include/rapidjson/error/error.h> +#include <contrib/libs/rapidjson/include/rapidjson/error/en.h> +#include <contrib/libs/rapidjson/include/rapidjson/error/error.h> #include <contrib/libs/rapidjson/include/rapidjson/reader.h> #include <util/generic/stack.h> #include <util/string/cast.h> #include <util/system/yassert.h> -#include <util/string/builder.h> +#include <util/string/builder.h> namespace NJson { - namespace { - TString PrintError(const rapidjson::ParseResult& result) { + namespace { + TString PrintError(const rapidjson::ParseResult& result) { return TStringBuilder() << TStringBuf("Offset: ") << result.Offset() << TStringBuf(", Code: ") << (int)result.Code() << TStringBuf(", Error: ") << GetParseError_En(result.Code()); - } - } - + } + } + static const size_t DEFAULT_BUFFER_LEN = 65536; bool TParserCallbacks::OpenComplexValue(EJsonValueType type) { @@ -355,7 +355,7 @@ namespace NJson { if (result.IsError()) { if (throwOnError) { - ythrow TJsonException() << PrintError(result); + ythrow TJsonException() << PrintError(result); } else { return false; } @@ -538,7 +538,7 @@ namespace NJson { auto result = Read(*config, reader, is, wrapper); if (result.IsError()) { - cbs->OnError(result.Offset(), PrintError(result)); + cbs->OnError(result.Offset(), PrintError(result)); return false; } diff --git a/library/cpp/json/json_reader.h b/library/cpp/json/json_reader.h index b673788330..18bfe05982 100644 --- a/library/cpp/json/json_reader.h +++ b/library/cpp/json/json_reader.h @@ -1,15 +1,15 @@ #pragma once -#include "json_value.h" - +#include "json_value.h" + #include <library/cpp/json/common/defs.h> #include <library/cpp/json/fast_sax/parser.h> -#include <util/generic/yexception.h> - +#include <util/generic/yexception.h> + #include <util/stream/input.h> -#include <util/stream/str.h> -#include <util/stream/mem.h> +#include <util/stream/str.h> +#include <util/stream/mem.h> namespace NJson { struct TJsonReaderConfig { @@ -22,7 +22,7 @@ namespace NJson { void SetBufferSize(size_t bufferSize); size_t GetBufferSize() const; - + private: size_t BufferSize; }; @@ -34,7 +34,7 @@ namespace NJson { bool ReadJsonTree(IInputStream* in, TJsonValue* out, bool throwOnError = false); bool ReadJsonTree(IInputStream* in, bool allowComments, TJsonValue* out, bool throwOnError = false); bool ReadJsonTree(IInputStream* in, const TJsonReaderConfig* config, TJsonValue* out, bool throwOnError = false); - + TJsonValue ReadJsonTree(IInputStream* in, bool throwOnError = false); TJsonValue ReadJsonTree(IInputStream* in, bool allowComments, bool throwOnError); TJsonValue ReadJsonTree(IInputStream* in, const TJsonReaderConfig* config, bool throwOnError = false); @@ -73,11 +73,11 @@ namespace NJson { inline bool ValidateJsonThrow(IInputStream* in, const TJsonReaderConfig* config) { return ValidateJson(in, config, true); } - + inline bool ValidateJsonThrow(TStringBuf in, const TJsonReaderConfig& config = TJsonReaderConfig()) { return ValidateJson(in, config, true); } - + class TParserCallbacks: public TJsonCallbacks { public: TParserCallbacks(TJsonValue& value, bool throwOnError = false, bool notClosedBracketIsError = false); @@ -93,13 +93,13 @@ namespace NJson { bool OnCloseMap() override; bool OnMapKey(const TStringBuf& val) override; bool OnEnd() override; - + protected: TJsonValue& Value; TString Key; TVector<TJsonValue*> ValuesStack; bool NotClosedBracketIsError; - + enum { START, AFTER_MAP_KEY, @@ -137,4 +137,4 @@ namespace NJson { //// relaxed json, used in library/cpp/scheme bool ReadJsonFastTree(TStringBuf in, TJsonValue* out, bool throwOnError = false, bool notClosedBracketIsError = false); TJsonValue ReadJsonFastTree(TStringBuf in, bool notClosedBracketIsError = false); -} +} diff --git a/library/cpp/json/json_writer.cpp b/library/cpp/json/json_writer.cpp index 3d058bae36..121ee3a335 100644 --- a/library/cpp/json/json_writer.cpp +++ b/library/cpp/json/json_writer.cpp @@ -1,7 +1,7 @@ #include "json_writer.h" #include <util/charset/utf8.h> -#include <util/generic/algorithm.h> +#include <util/generic/algorithm.h> #include <util/string/cast.h> #include <util/system/yassert.h> @@ -85,7 +85,7 @@ namespace NJson { void TJsonWriter::WriteNull() { Buf.WriteNull(); } - + void TJsonWriter::Write(float value) { Buf.WriteFloat(value, FloatToStringMode, FloatNDigits); } @@ -113,11 +113,11 @@ namespace NJson { } }; } - + void TJsonWriter::Write(const TJsonValue* v) { Buf.WriteJsonValue(v, SortKeys, FloatToStringMode, DoubleNDigits); } - + void TJsonWriter::Write(const TJsonValue& v) { Buf.WriteJsonValue(&v, SortKeys, FloatToStringMode, DoubleNDigits); } @@ -139,7 +139,7 @@ namespace NJson { w.Write(val); w.Flush(); } - + void WriteJson(IOutputStream* out, const TJsonValue* val, const TJsonWriterConfig& config) { TJsonWriter w(out, config, true); w.Write(val); diff --git a/library/cpp/json/json_writer.h b/library/cpp/json/json_writer.h index c7f5c9499a..59398094cc 100644 --- a/library/cpp/json/json_writer.h +++ b/library/cpp/json/json_writer.h @@ -2,8 +2,8 @@ // Deprecated. Use library/cpp/json/writer in new code. -#include "json_value.h" - +#include "json_value.h" + #include <library/cpp/json/writer/json.h> #include <util/stream/output.h> @@ -86,7 +86,7 @@ namespace NJson { void Write(bool value); void Write(const TJsonValue* value); void Write(const TJsonValue& value); - + // must use all variations of integer types since long // and long long are different types but with same size void Write(long long value); @@ -151,7 +151,7 @@ namespace NJson { Write(key, *value); } } - + void WriteOptional(const TStringBuf&, const TNothing&) { // nothing to do } diff --git a/library/cpp/json/ut/json_prettifier_ut.cpp b/library/cpp/json/ut/json_prettifier_ut.cpp index ae5f8dd81a..10946de93a 100644 --- a/library/cpp/json/ut/json_prettifier_ut.cpp +++ b/library/cpp/json/ut/json_prettifier_ut.cpp @@ -1,25 +1,25 @@ #include <library/cpp/json/json_prettifier.h> #include <library/cpp/testing/unittest/registar.h> - + Y_UNIT_TEST_SUITE(JsonPrettifier) { Y_UNIT_TEST(PrettifyJsonShort) { - UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson(""), ""); - UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("null"), "null"); - UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("true"), "true"); - UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("false"), "false"); - UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("1.5"), "1.5"); - UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("test", false, 2, true), "'test'"); - - UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("[]"), "[ ]"); - UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("[a]", false, 2), "[\n \"a\"\n]"); - UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("[a,b]", false, 2, true), "[\n 'a',\n 'b'\n]"); - UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("[{},b]", false, 2, true), "[\n { },\n 'b'\n]"); - UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("[a,{}]", false, 2, true), "[\n 'a',\n { }\n]"); - UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("[{},{}]"), "[\n { },\n { }\n]"); - UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("{}"), "{ }"); - UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("{}"), "{ }"); - UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("{k:v}", false, 2, true), "{\n 'k' : 'v'\n}"); + UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson(""), ""); + UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("null"), "null"); + UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("true"), "true"); + UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("false"), "false"); + UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("1.5"), "1.5"); + UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("test", false, 2, true), "'test'"); + + UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("[]"), "[ ]"); + UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("[a]", false, 2), "[\n \"a\"\n]"); + UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("[a,b]", false, 2, true), "[\n 'a',\n 'b'\n]"); + UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("[{},b]", false, 2, true), "[\n { },\n 'b'\n]"); + UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("[a,{}]", false, 2, true), "[\n 'a',\n { }\n]"); + UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("[{},{}]"), "[\n { },\n { }\n]"); + UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("{}"), "{ }"); + UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("{}"), "{ }"); + UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("{k:v}", false, 2, true), "{\n 'k' : 'v'\n}"); UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("Test545", true, 2), "Test545"); UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("'null'", true, 2, true), "'null'"); @@ -45,7 +45,7 @@ Y_UNIT_TEST_SUITE(JsonPrettifier) { " }\n" "]"); - UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("{k:v,a:b,x:[1,2,3]}", false, 2, true), + UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("{k:v,a:b,x:[1,2,3]}", false, 2, true), "{\n" " 'k' : 'v',\n" " 'a' : 'b',\n" @@ -56,7 +56,7 @@ Y_UNIT_TEST_SUITE(JsonPrettifier) { " ]\n" "}"); - UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("{k:v,a:b,x:[1,{f:b},3],m:n}", false, 2, true), + UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("{k:v,a:b,x:[1,{f:b},3],m:n}", false, 2, true), "{\n" " 'k' : 'v',\n" " 'a' : 'b',\n" @@ -69,7 +69,7 @@ Y_UNIT_TEST_SUITE(JsonPrettifier) { " ],\n" " 'm' : 'n'\n" "}"); - + NJson::TJsonPrettifier prettifierMaxLevel1 = NJson::TJsonPrettifier::Prettifier(false, 2, true); prettifierMaxLevel1.MaxPaddingLevel = 1; UNIT_ASSERT_STRINGS_EQUAL(prettifierMaxLevel1.Prettify("{k:v,a:b,x:[1,{f:b},3],m:n}"), @@ -79,8 +79,8 @@ Y_UNIT_TEST_SUITE(JsonPrettifier) { " 'x' : [ 1, { 'f' : 'b' }, 3 ],\n" " 'm' : 'n'\n" "}"); - - UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("{g:{x:{a:{b:c,e:f},q:{x:y}},y:fff}}", true, 2), + + UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("{g:{x:{a:{b:c,e:f},q:{x:y}},y:fff}}", true, 2), "{\n" " g : {\n" " x : {\n" @@ -108,8 +108,8 @@ Y_UNIT_TEST_SUITE(JsonPrettifier) { " y : fff\n" " }\n" "}"); - } - + } + Y_UNIT_TEST(PrettifyJsonInvalid) { UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("}"), ""); UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("}}"), ""); @@ -124,26 +124,26 @@ Y_UNIT_TEST_SUITE(JsonPrettifier) { } Y_UNIT_TEST(CompactifyJsonShort) { - UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson(""), ""); - UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("null"), "null"); - UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("true"), "true"); - UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("false"), "false"); - UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("1.5"), "1.5"); - UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("test", true), "test"); - UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("test", false), "\"test\""); - - UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("[ ]"), "[]"); - UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("[\n \"a\"\n]", true), "[a]"); - UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("[\n 'a',\n 'b'\n]", true), "[a,b]"); - UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("[\n { },\n 'b'\n]", true), "[{},b]"); - UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("[\n 'a',\n { }\n]", true), "[a,{}]"); - UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("[\n { },\n { }\n]", true), "[{},{}]"); - UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("{ }"), "{}"); - UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("{\n 'k' : 'v'\n}", true), "{k:v}"); + UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson(""), ""); + UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("null"), "null"); + UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("true"), "true"); + UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("false"), "false"); + UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("1.5"), "1.5"); + UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("test", true), "test"); + UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("test", false), "\"test\""); + + UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("[ ]"), "[]"); + UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("[\n \"a\"\n]", true), "[a]"); + UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("[\n 'a',\n 'b'\n]", true), "[a,b]"); + UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("[\n { },\n 'b'\n]", true), "[{},b]"); + UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("[\n 'a',\n { }\n]", true), "[a,{}]"); + UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("[\n { },\n { }\n]", true), "[{},{}]"); + UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("{ }"), "{}"); + UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("{\n 'k' : 'v'\n}", true), "{k:v}"); } Y_UNIT_TEST(CompactifyJsonLong) { - UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson( + UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson( "[\n" " {\n" " 'k' : 'v'\n" @@ -166,8 +166,8 @@ Y_UNIT_TEST_SUITE(JsonPrettifier) { "}", true), "{k:v,a:b,x:[1,2,3]}"); - - UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson( + + UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson( "{\n" " 'k' : 'v',\n" " 'a' : 'b',\n" @@ -182,8 +182,8 @@ Y_UNIT_TEST_SUITE(JsonPrettifier) { "}", true), "{k:v,a:b,x:[1,{f:b},3],m:n}"); - - UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson( + + UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson( "{\n" " g : {\n" " x : {\n" @@ -200,5 +200,5 @@ Y_UNIT_TEST_SUITE(JsonPrettifier) { "}", true), "{g:{x:{a:{b:c,e:f},q:{x:y}},y:fff}}"); - } + } } diff --git a/library/cpp/json/ut/json_reader_fast_ut.cpp b/library/cpp/json/ut/json_reader_fast_ut.cpp index 60dffc91c7..bc1a84b3a6 100644 --- a/library/cpp/json/ut/json_reader_fast_ut.cpp +++ b/library/cpp/json/ut/json_reader_fast_ut.cpp @@ -1,12 +1,12 @@ #include <library/cpp/json/json_reader.h> #include <library/cpp/json/json_prettifier.h> #include <library/cpp/testing/unittest/registar.h> - + #include <library/cpp/string_utils/relaxed_escaper/relaxed_escaper.h> #include <util/string/cast.h> -#include <util/string/printf.h> - -namespace NJson { +#include <util/string/printf.h> + +namespace NJson { namespace NTest { enum ETestEvent { E_NO_EVENT = 0, @@ -23,37 +23,37 @@ namespace NJson { E_STR, E_KEY }; - + struct TEvent { ETestEvent Type = E_NO_EVENT; - + i64 INum = 0; double DNum = 0; TString Str; - + TEvent(ETestEvent e = E_NO_EVENT) : Type(e) { } - + TEvent(double v, ETestEvent e) : Type(e) , DNum(v) { } - + TEvent(i64 v, ETestEvent e) : Type(e) , INum(v) { } - + TEvent(TStringBuf t, ETestEvent e) : Type(e) , Str(NEscJ::EscapeJ<true, false>(t)) { } - + TString ToString() const { switch (Type) { default: @@ -83,74 +83,74 @@ namespace NJson { } } }; - + using TEvents = TVector<TEvent>; - + struct TTestHandler : TJsonCallbacks { TEvents Events; - + bool OnOpenMap() override { Events.push_back(E_DICT_OPEN); return true; } - + bool OnCloseMap() override { Events.push_back(E_DICT_CLOSE); return true; } - + bool OnOpenArray() override { Events.push_back(E_ARR_OPEN); return true; } - + bool OnCloseArray() override { Events.push_back(E_ARR_CLOSE); return true; } - + bool OnNull() override { Events.push_back(E_NULL); return true; } - + bool OnBoolean(bool v) override { Events.push_back(TEvent((i64)v, E_BOOL)); return true; } - + bool OnInteger(long long v) override { Events.push_back(TEvent((i64)v, E_INT)); return true; } - + bool OnUInteger(unsigned long long v) override { return OnInteger(v); } - + bool OnDouble(double v) override { Events.push_back(TEvent(v, E_FLT)); return true; } - + bool OnString(const TStringBuf& v) override { Events.push_back(TEvent(v, E_STR)); return true; } - + bool OnMapKey(const TStringBuf& v) override { Events.push_back(TEvent(v, E_KEY)); return true; } - + void OnError(size_t, TStringBuf token) override { Events.push_back(TEvent(token, E_ERROR)); } - + void Assert(const TEvents& e, TString str) { try { UNIT_ASSERT_VALUES_EQUAL_C(e.size(), Events.size(), str); - + for (ui32 i = 0, sz = e.size(); i < sz; ++i) { UNIT_ASSERT_VALUES_EQUAL_C((int)e[i].Type, (int)Events[i].Type, Sprintf("'%s' %u", str.data(), i)); UNIT_ASSERT_VALUES_EQUAL_C(e[i].INum, Events[i].INum, Sprintf("'%s' %u", str.data(), i)); @@ -162,32 +162,32 @@ namespace NJson { for (const auto& event : Events) { Clog << event.ToString() << Endl; } - + throw; } - } + } }; - } -} - + } +} + class TFastJsonTest: public TTestBase { UNIT_TEST_SUITE(TFastJsonTest) - UNIT_TEST(TestParse) + UNIT_TEST(TestParse) UNIT_TEST(TestReadJsonFastTree) - UNIT_TEST(TestNoInlineComment) + UNIT_TEST(TestNoInlineComment) UNIT_TEST_SUITE_END(); -public: - template <bool accept> - void DoTestParse(TStringBuf json, ui32 amount, ...) { - using namespace NJson::NTest; - TEvents evs; - va_list vl; - va_start(vl, amount); - for (ui32 i = 0; i < amount; i++) { - ETestEvent e = (ETestEvent)va_arg(vl, int); - - switch ((int)e) { +public: + template <bool accept> + void DoTestParse(TStringBuf json, ui32 amount, ...) { + using namespace NJson::NTest; + TEvents evs; + va_list vl; + va_start(vl, amount); + for (ui32 i = 0; i < amount; i++) { + ETestEvent e = (ETestEvent)va_arg(vl, int); + + switch ((int)e) { case E_NO_EVENT: case E_DICT_OPEN: case E_DICT_CLOSE: @@ -227,40 +227,40 @@ public: evs.push_back(TEvent(TStringBuf(s), e)); break; } - } - } - va_end(vl); - - TTestHandler h; + } + } + va_end(vl); + + TTestHandler h; const bool res = ReadJsonFast(json, &h); UNIT_ASSERT_VALUES_EQUAL_C(res, accept, Sprintf("%s (%s)", ToString(json).data(), h.Events.back().Str.data())); h.Assert(evs, ToString(json)); - } - - void TestParse() { - using namespace NJson::NTest; - - DoTestParse<true>("", 0); - DoTestParse<true>(" \t \t ", 0); - DoTestParse<true>("a-b-c@аб_вгд909AБ", 1, E_STR, "a-b-c@аб_вгд909AБ"); - DoTestParse<true>("'я тестовая строка'", 1, E_STR, "я тестовая строка"); - DoTestParse<true>("\"я тестовая строка\"", 1, E_STR, "я тестовая строка"); - DoTestParse<true>("'\\xA\\xA\\xA'", 1, E_STR, "\n\n\n"); - DoTestParse<true>("12.15", 1, E_FLT, 12.15); - DoTestParse<true>("null", 1, E_NULL); - DoTestParse<true>("true", 1, E_BOOL, true); - DoTestParse<true>("false", 1, E_BOOL, false); - DoTestParse<true>("[]", 2, E_ARR_OPEN, E_ARR_CLOSE); - DoTestParse<true>("[ a ]", 3, E_ARR_OPEN, E_STR, "a", E_ARR_CLOSE); - DoTestParse<true>("[ a, b ]", 4, E_ARR_OPEN, E_STR, "a", E_STR, "b", E_ARR_CLOSE); - DoTestParse<true>("[a,b]", 4, E_ARR_OPEN, E_STR, "a", E_STR, "b", E_ARR_CLOSE); - DoTestParse<false>("[a,b][a,b]", 5, E_ARR_OPEN, E_STR, "a", E_STR, "b", E_ARR_CLOSE, E_ERROR, "invalid syntax at token: '['"); - DoTestParse<false>("[a,,b]", 3, E_ARR_OPEN, E_STR, "a", E_ERROR, "invalid syntax at token: ','"); - DoTestParse<true>("{ k : v }", 4, E_DICT_OPEN, E_KEY, "k", E_STR, "v", E_DICT_CLOSE); - DoTestParse<true>("{a:'\\b'/*comment*/, k /*comment*/\n : v }", 6, E_DICT_OPEN, E_KEY, "a", E_STR, "\b", E_KEY, "k", E_STR, "v", E_DICT_CLOSE); + } + + void TestParse() { + using namespace NJson::NTest; + + DoTestParse<true>("", 0); + DoTestParse<true>(" \t \t ", 0); + DoTestParse<true>("a-b-c@аб_вгд909AБ", 1, E_STR, "a-b-c@аб_вгд909AБ"); + DoTestParse<true>("'я тестовая строка'", 1, E_STR, "я тестовая строка"); + DoTestParse<true>("\"я тестовая строка\"", 1, E_STR, "я тестовая строка"); + DoTestParse<true>("'\\xA\\xA\\xA'", 1, E_STR, "\n\n\n"); + DoTestParse<true>("12.15", 1, E_FLT, 12.15); + DoTestParse<true>("null", 1, E_NULL); + DoTestParse<true>("true", 1, E_BOOL, true); + DoTestParse<true>("false", 1, E_BOOL, false); + DoTestParse<true>("[]", 2, E_ARR_OPEN, E_ARR_CLOSE); + DoTestParse<true>("[ a ]", 3, E_ARR_OPEN, E_STR, "a", E_ARR_CLOSE); + DoTestParse<true>("[ a, b ]", 4, E_ARR_OPEN, E_STR, "a", E_STR, "b", E_ARR_CLOSE); + DoTestParse<true>("[a,b]", 4, E_ARR_OPEN, E_STR, "a", E_STR, "b", E_ARR_CLOSE); + DoTestParse<false>("[a,b][a,b]", 5, E_ARR_OPEN, E_STR, "a", E_STR, "b", E_ARR_CLOSE, E_ERROR, "invalid syntax at token: '['"); + DoTestParse<false>("[a,,b]", 3, E_ARR_OPEN, E_STR, "a", E_ERROR, "invalid syntax at token: ','"); + DoTestParse<true>("{ k : v }", 4, E_DICT_OPEN, E_KEY, "k", E_STR, "v", E_DICT_CLOSE); + DoTestParse<true>("{a:'\\b'/*comment*/, k /*comment*/\n : v }", 6, E_DICT_OPEN, E_KEY, "a", E_STR, "\b", E_KEY, "k", E_STR, "v", E_DICT_CLOSE); DoTestParse<true>("{a:.15, k : v }", 6, E_DICT_OPEN, E_KEY, "a", E_FLT, .15, E_KEY, "k", E_STR, "v", E_DICT_CLOSE); DoTestParse<true>("[ a, -.1e+5, 1E-7]", 5, E_ARR_OPEN, E_STR, "a", E_FLT, -.1e+5, E_FLT, 1e-7, E_ARR_CLOSE); - DoTestParse<true>("{}", 2, E_DICT_OPEN, E_DICT_CLOSE); + DoTestParse<true>("{}", 2, E_DICT_OPEN, E_DICT_CLOSE); DoTestParse<true>("{ a : x, b : [ c, d, ] }", 9, E_DICT_OPEN, E_KEY, "a", E_STR, "x", E_KEY, "b", E_ARR_OPEN, E_STR, "c", E_STR, "d", E_ARR_CLOSE, E_DICT_CLOSE); DoTestParse<false>("{ a : x, b : [ c, d,, ] }", 8, E_DICT_OPEN, E_KEY, "a", E_STR, "x", E_KEY, "b", E_ARR_OPEN, E_STR, "c", E_STR, "d", E_ERROR, "invalid syntax at token: ','"); // DoTestParse<false>("{ a : x : y }", 4, E_DICT_OPEN @@ -271,14 +271,14 @@ public: // , E_KEY, "queries", E_DICT_OPEN // , E_KEY, "ref", E_ARR_OPEN, E_ARR_CLOSE // , E_DICT_CLOSE, E_ERROR, ""); - DoTestParse<true>("'100x00'", 1, E_STR, "100x00"); - DoTestParse<true>("-1", 1, E_INT, -1); - DoTestParse<true>("-9223372036854775808", 1, E_LONG_LONG, (long long)Min<i64>()); - DoTestParse<false>("100x00", 1, E_ERROR, "invalid syntax at token: '100x'"); - DoTestParse<false>("100 200", 2, E_INT, 100, E_ERROR, "invalid syntax at token: '200'"); + DoTestParse<true>("'100x00'", 1, E_STR, "100x00"); + DoTestParse<true>("-1", 1, E_INT, -1); + DoTestParse<true>("-9223372036854775808", 1, E_LONG_LONG, (long long)Min<i64>()); + DoTestParse<false>("100x00", 1, E_ERROR, "invalid syntax at token: '100x'"); + DoTestParse<false>("100 200", 2, E_INT, 100, E_ERROR, "invalid syntax at token: '200'"); DoTestParse<true>("{g:{x:{a:{b:c,e:f},q:{x:y}},y:fff}}", 22, E_DICT_OPEN, E_KEY, "g", E_DICT_OPEN, E_KEY, "x", E_DICT_OPEN, E_KEY, "a", E_DICT_OPEN, E_KEY, "b", E_STR, "c", E_KEY, "e", E_STR, "f", E_DICT_CLOSE, E_KEY, "q", E_DICT_OPEN, E_KEY, "x", E_STR, "y", E_DICT_CLOSE, E_DICT_CLOSE, E_KEY, "y", E_STR, "fff", E_DICT_CLOSE, E_DICT_CLOSE); - } - + } + void TestReadJsonFastTree() { const TString json = R"( { @@ -290,15 +290,15 @@ public: NJson::TJsonValue value; UNIT_ASSERT(!ReadJsonFastTree(json, &value)); } - - void TestNoInlineComment() { - using namespace NJson::NTest; - DoTestParse<false>("{\"a\":1}//d{\"b\":2}", 5, E_DICT_OPEN, E_KEY, "a", E_INT, 1, E_DICT_CLOSE, E_ERROR, "invalid syntax at token: '/'"); - DoTestParse<false>("{\"a\":1}//d{\"b\":2}\n", 5, E_DICT_OPEN, E_KEY, "a", E_INT, 1, E_DICT_CLOSE, E_ERROR, "invalid syntax at token: '/'"); - DoTestParse<false>("{\"a\":{//d{\"b\":2}\n}}", 4, E_DICT_OPEN, E_KEY, "a", E_DICT_OPEN, E_ERROR, "invalid syntax at token: '/'"); - DoTestParse<false>("{\"a\":{//d{\"b\":2}}}\n", 4, E_DICT_OPEN, E_KEY, "a", E_DICT_OPEN, E_ERROR, "invalid syntax at token: '/'"); - DoTestParse<false>("{\"a\":{//d{\"b\":2}}}", 4, E_DICT_OPEN, E_KEY, "a", E_DICT_OPEN, E_ERROR, "invalid syntax at token: '/'"); - } -}; - -UNIT_TEST_SUITE_REGISTRATION(TFastJsonTest) + + void TestNoInlineComment() { + using namespace NJson::NTest; + DoTestParse<false>("{\"a\":1}//d{\"b\":2}", 5, E_DICT_OPEN, E_KEY, "a", E_INT, 1, E_DICT_CLOSE, E_ERROR, "invalid syntax at token: '/'"); + DoTestParse<false>("{\"a\":1}//d{\"b\":2}\n", 5, E_DICT_OPEN, E_KEY, "a", E_INT, 1, E_DICT_CLOSE, E_ERROR, "invalid syntax at token: '/'"); + DoTestParse<false>("{\"a\":{//d{\"b\":2}\n}}", 4, E_DICT_OPEN, E_KEY, "a", E_DICT_OPEN, E_ERROR, "invalid syntax at token: '/'"); + DoTestParse<false>("{\"a\":{//d{\"b\":2}}}\n", 4, E_DICT_OPEN, E_KEY, "a", E_DICT_OPEN, E_ERROR, "invalid syntax at token: '/'"); + DoTestParse<false>("{\"a\":{//d{\"b\":2}}}", 4, E_DICT_OPEN, E_KEY, "a", E_DICT_OPEN, E_ERROR, "invalid syntax at token: '/'"); + } +}; + +UNIT_TEST_SUITE_REGISTRATION(TFastJsonTest) diff --git a/library/cpp/json/ut/json_reader_ut.cpp b/library/cpp/json/ut/json_reader_ut.cpp index cd31afa0b8..92435e52a4 100644 --- a/library/cpp/json/ut/json_reader_ut.cpp +++ b/library/cpp/json/ut/json_reader_ut.cpp @@ -230,7 +230,7 @@ Y_UNIT_TEST_SUITE(TJsonReaderTest) { UNIT_ASSERT(value.Has("test")); UNIT_ASSERT(!value["test"].IsInteger()); UNIT_ASSERT(value["test"].IsUInteger()); - UNIT_ASSERT_EQUAL(value["test"].GetIntegerRobust(), (i64)(Max<i64>() + 1ull)); + UNIT_ASSERT_EQUAL(value["test"].GetIntegerRobust(), (i64)(Max<i64>() + 1ull)); } // Max<i64>() + 1 } diff --git a/library/cpp/json/ut/json_writer_ut.cpp b/library/cpp/json/ut/json_writer_ut.cpp index ca11d34dad..9bbd4e3f77 100644 --- a/library/cpp/json/ut/json_writer_ut.cpp +++ b/library/cpp/json/ut/json_writer_ut.cpp @@ -11,48 +11,48 @@ Y_UNIT_TEST_SUITE(TJsonWriterTest) { TString expected2 = expected1 + ",\"array\":[\"stroka\",false]"; TString expected3 = expected2 + "}"; - TStringStream out; + TStringStream out; - TJsonWriter json(&out, false); - json.OpenMap(); - json.Write("key1", (ui16)1); + TJsonWriter json(&out, false); + json.OpenMap(); + json.Write("key1", (ui16)1); json.WriteKey("key2"); json.Write((i32)2); - json.Write("key3", (ui64)3); + json.Write("key3", (ui64)3); UNIT_ASSERT(out.Empty()); - json.Flush(); - UNIT_ASSERT_VALUES_EQUAL(out.Str(), expected1); + json.Flush(); + UNIT_ASSERT_VALUES_EQUAL(out.Str(), expected1); - json.Write("array"); - json.OpenArray(); - json.Write("stroka"); - json.Write(false); - json.CloseArray(); + json.Write("array"); + json.OpenArray(); + json.Write("stroka"); + json.Write(false); + json.CloseArray(); - UNIT_ASSERT_VALUES_EQUAL(out.Str(), expected1); - json.Flush(); - UNIT_ASSERT_VALUES_EQUAL(out.Str(), expected2); + UNIT_ASSERT_VALUES_EQUAL(out.Str(), expected1); + json.Flush(); + UNIT_ASSERT_VALUES_EQUAL(out.Str(), expected2); - json.CloseMap(); + json.CloseMap(); - UNIT_ASSERT_VALUES_EQUAL(out.Str(), expected2); - json.Flush(); - UNIT_ASSERT_VALUES_EQUAL(out.Str(), expected3); - } + UNIT_ASSERT_VALUES_EQUAL(out.Str(), expected2); + json.Flush(); + UNIT_ASSERT_VALUES_EQUAL(out.Str(), expected3); + } Y_UNIT_TEST(SimpleWriteValueTest) { TString expected = "{\"key1\":null,\"key2\":{\"subkey1\":[1,{\"subsubkey\":\"test2\"},null,true],\"subkey2\":\"test\"}}"; - TJsonValue v; - v["key1"] = JSON_NULL; - v["key2"]["subkey1"].AppendValue(1); - v["key2"]["subkey1"].AppendValue(JSON_MAP)["subsubkey"] = "test2"; - v["key2"]["subkey1"].AppendValue(JSON_NULL); - v["key2"]["subkey1"].AppendValue(true); - v["key2"]["subkey2"] = "test"; - TStringStream out; - WriteJson(&out, &v); - UNIT_ASSERT_VALUES_EQUAL(out.Str(), expected); + TJsonValue v; + v["key1"] = JSON_NULL; + v["key2"]["subkey1"].AppendValue(1); + v["key2"]["subkey1"].AppendValue(JSON_MAP)["subsubkey"] = "test2"; + v["key2"]["subkey1"].AppendValue(JSON_NULL); + v["key2"]["subkey1"].AppendValue(true); + v["key2"]["subkey2"] = "test"; + TStringStream out; + WriteJson(&out, &v); + UNIT_ASSERT_VALUES_EQUAL(out.Str(), expected); } Y_UNIT_TEST(FormatOutput) { diff --git a/library/cpp/json/writer/json.h b/library/cpp/json/writer/json.h index 0aae2531b9..e8639713f6 100644 --- a/library/cpp/json/writer/json.h +++ b/library/cpp/json/writer/json.h @@ -148,8 +148,8 @@ namespace NJsonWriter { IOutputStream* Stream; THolder<TStringStream> StringStream; typedef TVector<const TString*> TKeys; - TKeys Keys; - + TKeys Keys; + TVector<EJsonEntity> Stack; bool NeedComma; bool NeedNewline; diff --git a/library/cpp/json/writer/json_value.cpp b/library/cpp/json/writer/json_value.cpp index c61e8d1dc4..64ba408bce 100644 --- a/library/cpp/json/writer/json_value.cpp +++ b/library/cpp/json/writer/json_value.cpp @@ -5,15 +5,15 @@ #include <util/generic/ylimits.h> #include <util/generic/utility.h> #include <util/generic/singleton.h> -#include <util/stream/str.h> +#include <util/stream/str.h> #include <util/stream/output.h> -#include <util/string/cast.h> -#include <util/string/type.h> +#include <util/string/cast.h> +#include <util/string/type.h> #include <util/string/vector.h> #include <util/system/yassert.h> #include <util/ysaveload.h> #include <util/generic/bt_exception.h> - + static bool AreJsonMapsEqual(const NJson::TJsonValue& lhs, const NJson::TJsonValue& rhs) { using namespace NJson; @@ -120,7 +120,7 @@ namespace NJson { tmp.Swap(*this); return *this; } - + TJsonValue::TJsonValue(const bool value) noexcept { SetType(JSON_BOOLEAN); Value.Boolean = value; @@ -160,7 +160,7 @@ namespace NJson { SetType(JSON_DOUBLE); Value.Double = value; } - + TJsonValue::TJsonValue(TString value) { SetType(JSON_STRING); Value.String = std::move(value); @@ -170,12 +170,12 @@ namespace NJson { SetType(JSON_STRING); Value.String = value; } - + TJsonValue::TJsonValue(const char* value) { SetType(JSON_STRING); Value.String = value; } - + EJsonValueType TJsonValue::GetType() const noexcept { return Type; } @@ -183,7 +183,7 @@ namespace NJson { TJsonValue& TJsonValue::SetType(const EJsonValueType type) { if (Type == type) return *this; - + Clear(); Type = type; @@ -237,7 +237,7 @@ namespace NJson { SetType(JSON_MAP); return (*Value.Map)[key] = std::move(value); } - + TJsonValue& TJsonValue::InsertValue(const TStringBuf key, TJsonValue&& value) { SetType(JSON_MAP); return (*Value.Map)[key] = std::move(value); @@ -351,7 +351,7 @@ namespace NJson { bool TJsonValue::GetBoolean() const { return Type != JSON_BOOLEAN ? false : Value.Boolean; } - + long long TJsonValue::GetInteger() const { if (!IsInteger()) return 0; @@ -371,7 +371,7 @@ namespace NJson { return 0; } } - + unsigned long long TJsonValue::GetUInteger() const { if (!IsUInteger()) return 0; @@ -411,19 +411,19 @@ namespace NJson { return 0.0; } } - + const TString& TJsonValue::GetString() const { return Type != JSON_STRING ? Singleton<TDefaultsHolder>()->String : Value.String; } - + const TJsonValue::TMapType& TJsonValue::GetMap() const { return Type != JSON_MAP ? Singleton<TDefaultsHolder>()->Map : *Value.Map; } - + const TJsonValue::TArray& TJsonValue::GetArray() const { return (Type != JSON_ARRAY) ? Singleton<TDefaultsHolder>()->Array : *Value.Array; } - + bool TJsonValue::GetBooleanSafe() const { if (Type != JSON_BOOLEAN) ythrow TJsonException() << "Not a boolean"; @@ -535,8 +535,8 @@ namespace NJson { case JSON_BOOLEAN: return Value.Boolean; } - } - + } + long long TJsonValue::GetIntegerRobust() const noexcept { switch (Type) { case JSON_ARRAY: @@ -564,8 +564,8 @@ namespace NJson { case JSON_UINTEGER: return Value.Integer; } - } - + } + unsigned long long TJsonValue::GetUIntegerRobust() const noexcept { switch (Type) { case JSON_ARRAY: @@ -623,8 +623,8 @@ namespace NJson { case JSON_DOUBLE: return Value.Double; } - } - + } + TString TJsonValue::GetStringRobust() const { switch (Type) { case JSON_ARRAY: @@ -643,12 +643,12 @@ namespace NJson { case JSON_STRING: return Value.String; } - } - + } + bool TJsonValue::GetBoolean(bool* value) const noexcept { if (Type != JSON_BOOLEAN) return false; - + *value = Value.Boolean; return true; } @@ -656,7 +656,7 @@ namespace NJson { bool TJsonValue::GetInteger(long long* value) const noexcept { if (!IsInteger()) return false; - + *value = GetInteger(); return true; } @@ -672,7 +672,7 @@ namespace NJson { bool TJsonValue::GetDouble(double* value) const noexcept { if (!IsDouble()) return false; - + *value = GetDouble(); return true; } @@ -680,7 +680,7 @@ namespace NJson { bool TJsonValue::GetString(TString* value) const { if (Type != JSON_STRING) return false; - + *value = Value.String; return true; } @@ -688,7 +688,7 @@ namespace NJson { bool TJsonValue::GetMap(TJsonValue::TMapType* value) const { if (Type != JSON_MAP) return false; - + *value = *Value.Map; return true; } @@ -696,7 +696,7 @@ namespace NJson { bool TJsonValue::GetArray(TJsonValue::TArray* value) const { if (Type != JSON_ARRAY) return false; - + *value = *Value.Array; return true; } @@ -712,7 +712,7 @@ namespace NJson { bool TJsonValue::GetArrayPointer(const TJsonValue::TArray** value) const noexcept { if (Type != JSON_ARRAY) return false; - + *value = Value.Array; return true; } @@ -806,10 +806,10 @@ namespace NJson { return true; case JSON_INTEGER: - return (1ll << std::numeric_limits<double>::digits) >= Abs(Value.Integer); + return (1ll << std::numeric_limits<double>::digits) >= Abs(Value.Integer); case JSON_UINTEGER: - return (1ull << std::numeric_limits<double>::digits) >= Value.UInteger; + return (1ull << std::numeric_limits<double>::digits) >= Value.UInteger; default: return false; @@ -1052,12 +1052,12 @@ namespace NJson { } //**************************************************************** - + bool GetMapPointer(const TJsonValue& jv, const size_t index, const TJsonValue::TMapType** value) { const TJsonValue* v; if (!jv.GetValuePointer(index, &v) || !v->IsMap()) return false; - + *value = &v->GetMap(); return true; } @@ -1066,7 +1066,7 @@ namespace NJson { const TJsonValue* v; if (!jv.GetValuePointer(index, &v) || !v->IsArray()) return false; - + *value = &v->GetArray(); return true; } @@ -1075,11 +1075,11 @@ namespace NJson { const TJsonValue* v; if (!jv.GetValuePointer(key, &v) || !v->IsMap()) return false; - + *value = &v->GetMap(); return true; } - + bool GetArrayPointer(const TJsonValue& jv, const TStringBuf key, const TJsonValue::TArray** value) { const TJsonValue* v; if (!jv.GetValuePointer(key, &v) || !v->IsArray()) @@ -1088,7 +1088,7 @@ namespace NJson { *value = &v->GetArray(); return true; } - + void TJsonValue::BackChecks() const { if (Type != JSON_ARRAY) ythrow TJsonException() << "Not an array"; diff --git a/library/cpp/json/writer/json_value.h b/library/cpp/json/writer/json_value.h index 3f0f50bc4c..1646f4297a 100644 --- a/library/cpp/json/writer/json_value.h +++ b/library/cpp/json/writer/json_value.h @@ -68,7 +68,7 @@ namespace NJson { ~TJsonValue() { Clear(); } - + EJsonValueType GetType() const noexcept; TJsonValue& SetType(EJsonValueType type); @@ -143,7 +143,7 @@ namespace NJson { bool GetDouble(double* value) const noexcept; bool GetMapPointer(const TMapType** value) const noexcept; bool GetArrayPointer(const TArray** value) const noexcept; - + bool GetString(TString* value) const; bool GetMap(TMapType* value) const; bool GetArray(TArray* value) const; diff --git a/library/cpp/json/ya.make b/library/cpp/json/ya.make index d58eead8ec..df9deb651c 100644 --- a/library/cpp/json/ya.make +++ b/library/cpp/json/ya.make @@ -4,7 +4,7 @@ OWNER( pg velavokr ) - + SRCS( json_writer.cpp json_reader.cpp |