diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:15 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:15 +0300 |
commit | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (patch) | |
tree | da2c34829458c7d4e74bdfbdf85dff449e9e7fb8 /library/cpp/json/flex_buffers/cvt.cpp | |
parent | 778e51ba091dc39e7b7fcab2b9cf4dbedfb6f2b5 (diff) | |
download | ydb-72cb13b4aff9bc9cf22e49251bc8fd143f82538f.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/json/flex_buffers/cvt.cpp')
-rw-r--r-- | library/cpp/json/flex_buffers/cvt.cpp | 236 |
1 files changed, 118 insertions, 118 deletions
diff --git a/library/cpp/json/flex_buffers/cvt.cpp b/library/cpp/json/flex_buffers/cvt.cpp index fee0cea0b8..9f7d630373 100644 --- a/library/cpp/json/flex_buffers/cvt.cpp +++ b/library/cpp/json/flex_buffers/cvt.cpp @@ -1,139 +1,139 @@ -#include "cvt.h" - -#include <flatbuffers/flexbuffers.h> - +#include "cvt.h" + +#include <flatbuffers/flexbuffers.h> + #include <library/cpp/json/fast_sax/parser.h> #include <library/cpp/json/json_reader.h> - -#include <util/generic/vector.h> -#include <util/stream/output.h> -#include <util/stream/input.h> -#include <util/memory/pool.h> - -using namespace NJson; - -namespace { - struct TJsonToFlexCallbacks: public TJsonCallbacks { - inline TJsonToFlexCallbacks() - : P(8192) - { - } - + +#include <util/generic/vector.h> +#include <util/stream/output.h> +#include <util/stream/input.h> +#include <util/memory/pool.h> + +using namespace NJson; + +namespace { + struct TJsonToFlexCallbacks: public TJsonCallbacks { + inline TJsonToFlexCallbacks() + : P(8192) + { + } + bool OnNull() override { - B.Null(); - - return true; - } - + B.Null(); + + return true; + } + bool OnBoolean(bool v) override { - B.Bool(v); - - return true; - } - + B.Bool(v); + + return true; + } + bool OnInteger(long long v) override { - B.Int(v); - - return true; - } - + B.Int(v); + + return true; + } + bool OnUInteger(unsigned long long v) override { - B.UInt(v); - - return true; - } - + B.UInt(v); + + return true; + } + bool OnDouble(double v) override { - B.Double(v); - - return true; - } - + B.Double(v); + + return true; + } + bool OnString(const TStringBuf& v) override { B.String(v.data(), v.size()); - - return true; - } - + + return true; + } + bool OnOpenMap() override { - S.push_back(B.StartMap()); - - return true; - } - + S.push_back(B.StartMap()); + + return true; + } + bool OnMapKey(const TStringBuf& v) override { auto iv = P.AppendCString(v); - + B.Key(iv.data(), iv.size()); - - return true; - } - + + return true; + } + bool OnCloseMap() override { - B.EndMap(PopOffset()); - - return true; - } - + B.EndMap(PopOffset()); + + return true; + } + bool OnOpenArray() override { - S.push_back(B.StartVector()); - - return true; - } - + S.push_back(B.StartVector()); + + return true; + } + bool OnCloseArray() override { - B.EndVector(PopOffset(), false, false); - - return true; - } - + B.EndVector(PopOffset(), false, false); + + return true; + } + bool OnStringNoCopy(const TStringBuf& s) override { - return OnString(s); - } - + return OnString(s); + } + bool OnMapKeyNoCopy(const TStringBuf& s) override { - return OnMapKey(s); - } - + return OnMapKey(s); + } + bool OnEnd() override { - B.Finish(); - - Y_ENSURE(S.empty()); - - return true; - } - + B.Finish(); + + Y_ENSURE(S.empty()); + + return true; + } + void OnError(size_t, TStringBuf reason) override { - ythrow yexception() << reason; - } - - inline size_t PopOffset() { - auto res = S.back(); - - S.pop_back(); - - return res; - } - - inline auto& Buffer() { - return B.GetBuffer(); - } - - flexbuffers::Builder B; - TVector<size_t> S; - TMemoryPool P; - }; -} - -void NJson::ConvertJsonToFlexBuffers(TStringBuf input, TFlexBuffersData& result) { - TJsonToFlexCallbacks cb; - - ReadJsonFast(input, &cb); - result.swap(const_cast<std::vector<ui8>&>(cb.Buffer())); -} - -TString NJson::FlexToString(const TFlexBuffersData& v) { + ythrow yexception() << reason; + } + + inline size_t PopOffset() { + auto res = S.back(); + + S.pop_back(); + + return res; + } + + inline auto& Buffer() { + return B.GetBuffer(); + } + + flexbuffers::Builder B; + TVector<size_t> S; + TMemoryPool P; + }; +} + +void NJson::ConvertJsonToFlexBuffers(TStringBuf input, TFlexBuffersData& result) { + TJsonToFlexCallbacks cb; + + ReadJsonFast(input, &cb); + result.swap(const_cast<std::vector<ui8>&>(cb.Buffer())); +} + +TString NJson::FlexToString(const TFlexBuffersData& v) { auto root = flexbuffers::GetRoot(v.data(), v.size()); - - return TString(root.ToString()); -} + + return TString(root.ToString()); +} |