diff options
author | monster <monster@ydb.tech> | 2022-07-07 14:41:37 +0300 |
---|---|---|
committer | monster <monster@ydb.tech> | 2022-07-07 14:41:37 +0300 |
commit | 06e5c21a835c0e923506c4ff27929f34e00761c2 (patch) | |
tree | 75efcbc6854ef9bd476eb8bf00cc5c900da436a2 /library/cpp/json | |
parent | 03f024c4412e3aa613bb543cf1660176320ba8f4 (diff) | |
download | ydb-06e5c21a835c0e923506c4ff27929f34e00761c2.tar.gz |
fix ya.make
Diffstat (limited to 'library/cpp/json')
-rw-r--r-- | library/cpp/json/domscheme_traits.h | 216 | ||||
-rw-r--r-- | library/cpp/json/flex_buffers/cvt.cpp | 139 | ||||
-rw-r--r-- | library/cpp/json/flex_buffers/cvt.h | 20 | ||||
-rw-r--r-- | library/cpp/json/flex_buffers/ut/cvt_ut.cpp | 21 |
4 files changed, 0 insertions, 396 deletions
diff --git a/library/cpp/json/domscheme_traits.h b/library/cpp/json/domscheme_traits.h deleted file mode 100644 index a5a99cd8cf..0000000000 --- a/library/cpp/json/domscheme_traits.h +++ /dev/null @@ -1,216 +0,0 @@ -#pragma once - -#include "json_value.h" -#include "json_reader.h" -#include "json_writer.h" -#include <util/generic/algorithm.h> - -struct TJsonTraits { - using TValue = NJson::TJsonValue; - using TValueRef = TValue*; - using TConstValueRef = const TValue*; - using TStringType = TStringBuf; - - // anyvalue defaults - template <class T> - static inline TValue Value(T&& t) { - return TValue(std::forward<T>(t)); - } - - template <class T> - static inline TValue Value(std::initializer_list<T> t) { - TValue result(NJson::JSON_ARRAY); - result.GetArraySafe() = NJson::TJsonValue::TArray(t.begin(), t.end()); - return result; - } - - static inline TValueRef Ref(TValue& v) { - return &v; - } - - static inline TConstValueRef Ref(const TValue& v) { - return &v; - } - - // common ops - static inline bool IsNull(TConstValueRef v) { - return v->GetType() == NJson::JSON_UNDEFINED || v->IsNull(); - } - - static inline TString ToJson(TConstValueRef v) { - return NJson::WriteJson(v, false); - } - - // struct ops - static inline TValueRef GetField(TValueRef v, const TStringBuf& name) { - return &(*v)[name]; - } - - static inline TConstValueRef GetField(TConstValueRef v, const TStringBuf& name) { - return &(*v)[name]; - } - - // array ops - static bool IsArray(TConstValueRef v) { - return v->IsArray(); - } - - static inline void ArrayClear(TValueRef v) { - v->SetType(NJson::JSON_NULL); - v->SetType(NJson::JSON_ARRAY); - } - - using TArrayIterator = size_t; - - static inline TValueRef ArrayElement(TValueRef v, TArrayIterator n) { - return &(*v)[n]; - } - - static inline TConstValueRef ArrayElement(TConstValueRef v, TArrayIterator n) { - return &(*v)[n]; - } - - static inline size_t ArraySize(TConstValueRef v) { - return v->GetArray().size(); - } - - static inline TArrayIterator ArrayBegin(TConstValueRef) { - return 0; - } - - static inline TArrayIterator ArrayEnd(TConstValueRef v) { - return ArraySize(v); - } - - // dict ops - static bool IsDict(TConstValueRef v) { - return v->IsMap(); - } - - static inline void DictClear(TValueRef v) { - v->SetType(NJson::JSON_NULL); - v->SetType(NJson::JSON_MAP); - } - - static inline TValueRef DictElement(TValueRef v, TStringBuf key) { - return &(*v)[key]; - } - - static inline TConstValueRef DictElement(TConstValueRef v, TStringBuf key) { - return &(*v)[key]; - } - - static inline size_t DictSize(TConstValueRef v) { - return v->GetMap().size(); - } - - using TDictIterator = NJson::TJsonValue::TMapType::const_iterator; - - static inline TDictIterator DictBegin(TConstValueRef v) { - return v->GetMap().begin(); - } - - static inline TDictIterator DictEnd(TConstValueRef v) { - return v->GetMap().end(); - } - - static inline TStringBuf DictIteratorKey(TConstValueRef /*dict*/, const TDictIterator& it) { - return it->first; - } - - static inline TConstValueRef DictIteratorValue(TConstValueRef /*dict*/, const TDictIterator& it) { - return &it->second; - } - - // boolean ops - static inline void Get(TConstValueRef v, bool def, bool& b) { - b = - v->GetType() == NJson::JSON_UNDEFINED ? def : v->IsNull() ? def : v->GetBooleanRobust(); - } - - static inline void Get(TConstValueRef v, bool& b) { - Get(v, false, b); - } - - static inline bool IsValidPrimitive(const bool&, TConstValueRef v) { - return v->IsBoolean(); - } - -#define INTEGER_OPS(type, checkOp, getOp) \ - static inline void Get(TConstValueRef v, type def, type& i) { \ - i = v->checkOp() ? v->getOp() : def; \ - } \ - static inline void Get(TConstValueRef v, type& i) { \ - i = v->getOp(); \ - } \ - static inline bool IsValidPrimitive(const type&, TConstValueRef v) { \ - return v->checkOp() && v->getOp() >= Min<type>() && v->getOp() <= Max<type>(); \ - } - - INTEGER_OPS(i8, IsInteger, GetInteger) - INTEGER_OPS(i16, IsInteger, GetInteger) - INTEGER_OPS(i32, IsInteger, GetInteger) - INTEGER_OPS(i64, IsInteger, GetInteger) - INTEGER_OPS(ui8, IsUInteger, GetUInteger) - INTEGER_OPS(ui16, IsUInteger, GetUInteger) - INTEGER_OPS(ui32, IsUInteger, GetUInteger) - INTEGER_OPS(ui64, IsUInteger, GetUInteger) - -#undef INTEGER_OPS - - // double ops - static inline bool Get(TConstValueRef v, double def, double& d) { - if (v->IsDouble()) { - d = v->GetDouble(); - return true; - } - d = def; - return false; - } - - static inline void Get(TConstValueRef v, double& d) { - d = v->GetDouble(); - } - - static inline bool IsValidPrimitive(const double&, TConstValueRef v) { - return v->IsDouble(); - } - - // string ops - static inline void Get(TConstValueRef v, TStringBuf def, TStringBuf& s) { - s = v->IsString() ? v->GetString() : def; - } - - static inline void Get(TConstValueRef v, TStringBuf& s) { - s = v->GetString(); - } - - static inline bool IsValidPrimitive(const TStringBuf&, TConstValueRef v) { - return v->IsString(); - } - - // generic set - template <class T> - static inline void Set(TValueRef v, T&& t) { - v->SetValue(t); - } - - static inline void Clear(TValueRef v) { - v->SetType(NJson::JSON_NULL); - } - - // validation ops - static inline TVector<TString> GetKeys(TConstValueRef v) { - TVector<TString> res; - for (const auto& it : v->GetMap()) { - res.push_back(it.first); - } - Sort(res.begin(), res.end()); - return res; - } - - template <typename T> - static inline bool IsValidPrimitive(const T&, TConstValueRef) { - return false; - } -}; diff --git a/library/cpp/json/flex_buffers/cvt.cpp b/library/cpp/json/flex_buffers/cvt.cpp deleted file mode 100644 index fee0cea0b8..0000000000 --- a/library/cpp/json/flex_buffers/cvt.cpp +++ /dev/null @@ -1,139 +0,0 @@ -#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) - { - } - - bool OnNull() override { - B.Null(); - - return true; - } - - bool OnBoolean(bool v) override { - B.Bool(v); - - return true; - } - - bool OnInteger(long long v) override { - B.Int(v); - - return true; - } - - bool OnUInteger(unsigned long long v) override { - B.UInt(v); - - return true; - } - - bool OnDouble(double v) override { - B.Double(v); - - return true; - } - - bool OnString(const TStringBuf& v) override { - B.String(v.data(), v.size()); - - return true; - } - - bool OnOpenMap() override { - 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; - } - - bool OnCloseMap() override { - B.EndMap(PopOffset()); - - return true; - } - - bool OnOpenArray() override { - S.push_back(B.StartVector()); - - return true; - } - - bool OnCloseArray() override { - B.EndVector(PopOffset(), false, false); - - return true; - } - - bool OnStringNoCopy(const TStringBuf& s) override { - return OnString(s); - } - - bool OnMapKeyNoCopy(const TStringBuf& s) override { - return OnMapKey(s); - } - - bool OnEnd() override { - 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) { - auto root = flexbuffers::GetRoot(v.data(), v.size()); - - return TString(root.ToString()); -} diff --git a/library/cpp/json/flex_buffers/cvt.h b/library/cpp/json/flex_buffers/cvt.h deleted file mode 100644 index 82d2874268..0000000000 --- a/library/cpp/json/flex_buffers/cvt.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once - -#include <util/generic/vector.h> -#include <util/generic/strbuf.h> -#include <util/generic/string.h> - -namespace NJson { - using TFlexBuffersData = TVector<ui8>; - - TString FlexToString(const TFlexBuffersData& v); - void ConvertJsonToFlexBuffers(TStringBuf input, TFlexBuffersData& result); - - inline TFlexBuffersData ConvertJsonToFlexBuffers(TStringBuf input) { - TFlexBuffersData result; - - ConvertJsonToFlexBuffers(input, result); - - return result; - } -} diff --git a/library/cpp/json/flex_buffers/ut/cvt_ut.cpp b/library/cpp/json/flex_buffers/ut/cvt_ut.cpp deleted file mode 100644 index 9fffef4d38..0000000000 --- a/library/cpp/json/flex_buffers/ut/cvt_ut.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include <library/cpp/testing/unittest/registar.h> -#include <library/cpp/json/flex_buffers/cvt.h> - -using namespace NJson; - -static auto JSON = R"({ - "a": { - "b": [1, 2, 3], - "c": ["x", "y", 3, "z"] - } -})"; - -static auto RES = R"({ a: { b: [ 1, 2, 3 ], c: [ "x", "y", 3, "z" ] } })"; - -Y_UNIT_TEST_SUITE(JsonToFlex) { - Y_UNIT_TEST(Test1) { - auto buf = ConvertJsonToFlexBuffers(JSON); - - UNIT_ASSERT_VALUES_EQUAL(FlexToString(buf), RES); - } -} |