diff options
author | Ruslan Kovalev <ruslan.a.kovalev@gmail.com> | 2022-02-10 16:46:45 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:46:45 +0300 |
commit | 9123176b341b6f2658cff5132482b8237c1416c8 (patch) | |
tree | 49e222ea1c5804306084bb3ae065bb702625360f /library/cpp/json/json_prettifier.cpp | |
parent | 59e19371de37995fcb36beb16cd6ec030af960bc (diff) | |
download | ydb-9123176b341b6f2658cff5132482b8237c1416c8.tar.gz |
Restoring authorship annotation for Ruslan Kovalev <ruslan.a.kovalev@gmail.com>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/json/json_prettifier.cpp')
-rw-r--r-- | library/cpp/json/json_prettifier.cpp | 126 |
1 files changed, 63 insertions, 63 deletions
diff --git a/library/cpp/json/json_prettifier.cpp b/library/cpp/json/json_prettifier.cpp index 2fc85420bc..bb16aab44e 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(); - } - -} + } + +} |