diff options
author | qkrorlqr <qkrorlqr@yandex-team.ru> | 2022-02-10 16:47:21 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:47:21 +0300 |
commit | eec632e483ae34bc211138c67434b1e0e6054ced (patch) | |
tree | 9814fbd1c3effac9b8377c5d604b367b14e2db55 /library/cpp/json | |
parent | 9b89266638b10d40309e31dcb7caa2fc52b2aefd (diff) | |
download | ydb-eec632e483ae34bc211138c67434b1e0e6054ced.tar.gz |
Restoring authorship annotation for <qkrorlqr@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/json')
-rw-r--r-- | library/cpp/json/easy_parse/json_easy_parser_impl.h | 4 | ||||
-rw-r--r-- | library/cpp/json/json_reader.cpp | 182 | ||||
-rw-r--r-- | library/cpp/json/json_reader.h | 4 | ||||
-rw-r--r-- | library/cpp/json/rapidjson_helpers.cpp | 2 | ||||
-rw-r--r-- | library/cpp/json/rapidjson_helpers.h | 198 | ||||
-rw-r--r-- | library/cpp/json/ut/json_reader_ut.cpp | 26 | ||||
-rw-r--r-- | library/cpp/json/ya.make | 4 |
7 files changed, 210 insertions, 210 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 25016ec94a..ec55d838b3 100644 --- a/library/cpp/json/easy_parse/json_easy_parser_impl.h +++ b/library/cpp/json/easy_parse/json_easy_parser_impl.h @@ -1,7 +1,7 @@ #pragma once -#include <util/generic/string.h> - +#include <util/generic/string.h> + namespace NJson { namespace NImpl { enum EType { diff --git a/library/cpp/json/json_reader.cpp b/library/cpp/json/json_reader.cpp index b50b591e24..072c8deafe 100644 --- a/library/cpp/json/json_reader.cpp +++ b/library/cpp/json/json_reader.cpp @@ -1,13 +1,13 @@ #include "json_reader.h" -#include "rapidjson_helpers.h" - +#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/reader.h> +#include <contrib/libs/rapidjson/include/rapidjson/reader.h> -#include <util/generic/stack.h> -#include <util/string/cast.h> +#include <util/generic/stack.h> +#include <util/string/cast.h> #include <util/system/yassert.h> #include <util/string/builder.h> @@ -155,17 +155,17 @@ namespace NJson { namespace { struct TJsonValueBuilder { -#ifdef NDEBUG +#ifdef NDEBUG using TItem = TJsonValue*; - + inline TJsonValue& Access(TItem& item) const { return *item; } -#else +#else struct TItem { TJsonValue* V; size_t DuplicateKeyCount; - + TItem(TJsonValue* v) : V(v) , DuplicateKeyCount(0) @@ -175,19 +175,19 @@ namespace NJson { inline TJsonValue& Access(TItem& item) const { return *item.V; - } -#endif - + } +#endif + NJson::TJsonValue& V; - + TStack<TItem> S; - + TJsonValueBuilder(NJson::TJsonValue& v) : V(v) { S.emplace(&V); } - + template <class T> void Set(const T& t) { if (Access(S.top()).IsArray()) { @@ -196,23 +196,23 @@ namespace NJson { Access(S.top()) = t; S.pop(); } - } - + } + bool Null() { Set(NJson::JSON_NULL); return true; } - + bool Bool(bool b) { Set(b); return true; } - + bool Int(int i) { Set(i); return true; } - + template <class U> bool ProcessUint(U u) { if (Y_LIKELY(u <= static_cast<ui64>(Max<i64>()))) { @@ -221,26 +221,26 @@ namespace NJson { Set(u); } return true; - } - + } + bool Uint(unsigned u) { return ProcessUint(u); } - + bool Int64(i64 i) { Set(i); return true; } - + bool Uint64(ui64 u) { return ProcessUint(u); } - + bool Double(double d) { Set(d); return true; } - + bool RawNumber(const char* str, rapidjson::SizeType length, bool copy) { Y_ASSERT(false && "this method should never be called"); Y_UNUSED(str); @@ -248,13 +248,13 @@ namespace NJson { Y_UNUSED(copy); return true; } - + bool String(const char* str, rapidjson::SizeType length, bool copy) { Y_ASSERT(copy); Set(TStringBuf(str, length)); return true; } - + bool StartObject() { if (Access(S.top()).IsArray()) { S.emplace(&Access(S.top()).AppendValue(NJson::JSON_MAP)); @@ -262,35 +262,35 @@ namespace NJson { Access(S.top()).SetType(NJson::JSON_MAP); } return true; - } - + } + bool Key(const char* str, rapidjson::SizeType length, bool copy) { Y_ASSERT(copy); auto& value = Access(S.top())[TStringBuf(str, length)]; - if (Y_UNLIKELY(value.GetType() != JSON_UNDEFINED)) { -#ifndef NDEBUG + if (Y_UNLIKELY(value.GetType() != JSON_UNDEFINED)) { +#ifndef NDEBUG ++S.top().DuplicateKeyCount; -#endif +#endif value.SetType(JSON_UNDEFINED); } S.emplace(&value); return true; - } - + } + inline int GetDuplicateKeyCount() const { -#ifdef NDEBUG +#ifdef NDEBUG return 0; -#else +#else return S.top().DuplicateKeyCount; -#endif +#endif } - + bool EndObject(rapidjson::SizeType memberCount) { Y_ASSERT(memberCount == Access(S.top()).GetMap().size() + GetDuplicateKeyCount()); S.pop(); return true; } - + bool StartArray() { if (Access(S.top()).IsArray()) { S.emplace(&Access(S.top()).AppendValue(NJson::JSON_ARRAY)); @@ -344,9 +344,9 @@ namespace NJson { return reader.Parse<rapidjson::kParseEscapedApostropheFlag>(is, handler); default: return reader.Parse<rapidjson::kParseNoFlags>(is, handler); - } - } - + } + } + template <class TRapidJsonCompliantInputStream, class THandler> bool ReadJson(TRapidJsonCompliantInputStream& is, const TJsonReaderConfig* config, THandler& handler, bool throwOnError) { rapidjson::Reader reader; @@ -361,9 +361,9 @@ namespace NJson { } } - return true; - } - + return true; + } + template <class TRapidJsonCompliantInputStream> bool ReadJsonTree(TRapidJsonCompliantInputStream& is, const TJsonReaderConfig* config, TJsonValue* out, bool throwOnError) { out->SetType(NJson::JSON_NULL); @@ -371,57 +371,57 @@ namespace NJson { TJsonValueBuilder handler(*out); return ReadJson(is, config, handler, throwOnError); - } - + } + template <class TData> bool ReadJsonTreeImpl(TData* in, const TJsonReaderConfig* config, TJsonValue* out, bool throwOnError) { std::conditional_t<std::is_same<TData, TStringBuf>::value, TStringBufStreamWrapper, TInputStreamWrapper> is(*in); return ReadJsonTree(is, config, out, throwOnError); } - + template <class TData> bool ReadJsonTreeImpl(TData* in, bool allowComments, TJsonValue* out, bool throwOnError) { TJsonReaderConfig config; config.AllowComments = allowComments; return ReadJsonTreeImpl(in, &config, out, throwOnError); } - + template <class TData> bool ReadJsonTreeImpl(TData* in, TJsonValue* out, bool throwOnError) { return ReadJsonTreeImpl(in, false, out, throwOnError); - } + } } //namespace - + bool ReadJsonTree(TStringBuf in, TJsonValue* out, bool throwOnError) { return ReadJsonTreeImpl(&in, out, throwOnError); - } - + } + bool ReadJsonTree(TStringBuf in, bool allowComments, TJsonValue* out, bool throwOnError) { return ReadJsonTreeImpl(&in, allowComments, out, throwOnError); } - + bool ReadJsonTree(TStringBuf in, const TJsonReaderConfig* config, TJsonValue* out, bool throwOnError) { return ReadJsonTreeImpl(&in, config, out, throwOnError); - } - + } + bool ReadJsonTree(IInputStream* in, TJsonValue* out, bool throwOnError) { return ReadJsonTreeImpl(in, out, throwOnError); - } - + } + bool ReadJsonTree(IInputStream* in, bool allowComments, TJsonValue* out, bool throwOnError) { return ReadJsonTreeImpl(in, allowComments, out, throwOnError); - } - + } + bool ReadJsonTree(IInputStream* in, const TJsonReaderConfig* config, TJsonValue* out, bool throwOnError) { return ReadJsonTreeImpl(in, config, out, throwOnError); - } - + } + bool ReadJsonFastTree(TStringBuf in, TJsonValue* out, bool throwOnError, bool notClosedBracketIsError) { TParserCallbacks cb(*out, throwOnError, notClosedBracketIsError); - + return ReadJsonFast(in, &cb); } - + TJsonValue ReadJsonFastTree(TStringBuf in, bool notClosedBracketIsError) { TJsonValue value; // There is no way to report an error apart from throwing an exception when we return result by value. @@ -432,7 +432,7 @@ namespace NJson { namespace { struct TJsonCallbacksWrapper { TJsonCallbacks& Impl; - + TJsonCallbacksWrapper(TJsonCallbacks& impl) : Impl(impl) { @@ -462,67 +462,67 @@ namespace NJson { bool Uint(unsigned u) { return ProcessUint(u); } - + bool Int64(i64 i) { return Impl.OnInteger(i); } - + bool Uint64(ui64 u) { return ProcessUint(u); } - + bool Double(double d) { return Impl.OnDouble(d); } - + bool RawNumber(const char* str, rapidjson::SizeType length, bool copy) { Y_ASSERT(false && "this method should never be called"); Y_UNUSED(str); Y_UNUSED(length); Y_UNUSED(copy); return true; - } - + } + bool String(const char* str, rapidjson::SizeType length, bool copy) { Y_ASSERT(copy); return Impl.OnString(TStringBuf(str, length)); } - + bool StartObject() { return Impl.OnOpenMap(); } - + bool Key(const char* str, rapidjson::SizeType length, bool copy) { Y_ASSERT(copy); return Impl.OnMapKey(TStringBuf(str, length)); } - + bool EndObject(rapidjson::SizeType memberCount) { Y_UNUSED(memberCount); return Impl.OnCloseMap(); } - + bool StartArray() { return Impl.OnOpenArray(); } - + bool EndArray(rapidjson::SizeType elementCount) { Y_UNUSED(elementCount); return Impl.OnCloseArray(); } }; } - + bool ReadJson(IInputStream* in, TJsonCallbacks* cbs) { return ReadJson(in, false, cbs); } - + bool ReadJson(IInputStream* in, bool allowComments, TJsonCallbacks* cbs) { TJsonReaderConfig config; config.AllowComments = allowComments; return ReadJson(in, &config, cbs); } - + bool ReadJson(IInputStream* in, bool allowComments, bool allowEscapedApostrophe, TJsonCallbacks* cbs) { TJsonReaderConfig config; config.AllowComments = allowComments; @@ -533,35 +533,35 @@ namespace NJson { bool ReadJson(IInputStream* in, const TJsonReaderConfig* config, TJsonCallbacks* cbs) { TJsonCallbacksWrapper wrapper(*cbs); TInputStreamWrapper is(*in); - + rapidjson::Reader reader; auto result = Read(*config, reader, is, wrapper); - + if (result.IsError()) { cbs->OnError(result.Offset(), PrintError(result)); - + return false; - } - + } + return cbs->OnEnd(); } - + TJsonValue ReadJsonTree(IInputStream* in, bool throwOnError) { TJsonValue out; ReadJsonTree(in, &out, throwOnError); return out; } - + TJsonValue ReadJsonTree(IInputStream* in, bool allowComments, bool throwOnError) { TJsonValue out; ReadJsonTree(in, allowComments, &out, throwOnError); return out; } - + TJsonValue ReadJsonTree(IInputStream* in, const TJsonReaderConfig* config, bool throwOnError) { TJsonValue out; ReadJsonTree(in, config, &out, throwOnError); return out; - } - -} + } + +} diff --git a/library/cpp/json/json_reader.h b/library/cpp/json/json_reader.h index 437bf18bad..b673788330 100644 --- a/library/cpp/json/json_reader.h +++ b/library/cpp/json/json_reader.h @@ -64,12 +64,12 @@ namespace NJson { TJsonCallbacks c(throwOnError); return ReadJson(in, config, &c); } - + inline bool ValidateJson(TStringBuf in, const TJsonReaderConfig& config = TJsonReaderConfig(), bool throwOnError = false) { TMemoryInput min(in.data(), in.size()); return ValidateJson(&min, &config, throwOnError); } - + inline bool ValidateJsonThrow(IInputStream* in, const TJsonReaderConfig* config) { return ValidateJson(in, config, true); } diff --git a/library/cpp/json/rapidjson_helpers.cpp b/library/cpp/json/rapidjson_helpers.cpp index 9f42b55b75..2e8159a103 100644 --- a/library/cpp/json/rapidjson_helpers.cpp +++ b/library/cpp/json/rapidjson_helpers.cpp @@ -1 +1 @@ -#include "rapidjson_helpers.h" +#include "rapidjson_helpers.h" diff --git a/library/cpp/json/rapidjson_helpers.h b/library/cpp/json/rapidjson_helpers.h index 314623425e..aeb96ff670 100644 --- a/library/cpp/json/rapidjson_helpers.h +++ b/library/cpp/json/rapidjson_helpers.h @@ -1,104 +1,104 @@ -#pragma once - -#include <util/generic/strbuf.h> -#include <util/stream/input.h> - -namespace NJson { - struct TReadOnlyStreamBase { - using Ch = char; - - Ch* PutBegin() { - Y_ASSERT(false); - return nullptr; - } - - void Put(Ch) { - Y_ASSERT(false); - } - - void Flush() { - Y_ASSERT(false); - } - - size_t PutEnd(Ch*) { - Y_ASSERT(false); - return 0; - } - }; - +#pragma once + +#include <util/generic/strbuf.h> +#include <util/stream/input.h> + +namespace NJson { + struct TReadOnlyStreamBase { + using Ch = char; + + Ch* PutBegin() { + Y_ASSERT(false); + return nullptr; + } + + void Put(Ch) { + Y_ASSERT(false); + } + + void Flush() { + Y_ASSERT(false); + } + + size_t PutEnd(Ch*) { + Y_ASSERT(false); + return 0; + } + }; + struct TInputStreamWrapper : TReadOnlyStreamBase { - Ch Peek() const { - if (!Eof) { - if (Pos >= Sz) { - if (Sz < BUF_SIZE) { - Sz += Helper.Read(Buf + Sz, BUF_SIZE - Sz); - } else { - Sz = Helper.Read(Buf, BUF_SIZE); - Pos = 0; - } - } - - if (Pos < Sz) { - return Buf[Pos]; - } - } - - Eof = true; - return 0; - } - - Ch Take() { - auto c = Peek(); - ++Pos; - ++Count; - return c; - } - - size_t Tell() const { - return Count; - } - + Ch Peek() const { + if (!Eof) { + if (Pos >= Sz) { + if (Sz < BUF_SIZE) { + Sz += Helper.Read(Buf + Sz, BUF_SIZE - Sz); + } else { + Sz = Helper.Read(Buf, BUF_SIZE); + Pos = 0; + } + } + + if (Pos < Sz) { + return Buf[Pos]; + } + } + + Eof = true; + return 0; + } + + Ch Take() { + auto c = Peek(); + ++Pos; + ++Count; + return c; + } + + size_t Tell() const { + return Count; + } + TInputStreamWrapper(IInputStream& helper) - : Helper(helper) - , Eof(false) - , Sz(0) - , Pos(0) - , Count(0) - { - } - - static const size_t BUF_SIZE = 1 << 12; - + : Helper(helper) + , Eof(false) + , Sz(0) + , Pos(0) + , Count(0) + { + } + + static const size_t BUF_SIZE = 1 << 12; + IInputStream& Helper; - mutable char Buf[BUF_SIZE]; - mutable bool Eof; - mutable size_t Sz; - mutable size_t Pos; - size_t Count; - }; - + mutable char Buf[BUF_SIZE]; + mutable bool Eof; + mutable size_t Sz; + mutable size_t Pos; + size_t Count; + }; + struct TStringBufStreamWrapper : TReadOnlyStreamBase { - Ch Peek() const { + Ch Peek() const { return Pos < Data.size() ? Data[Pos] : 0; - } - - Ch Take() { - auto c = Peek(); - ++Pos; - return c; - } - - size_t Tell() const { - return Pos; - } - - TStringBufStreamWrapper(TStringBuf data) - : Data(data) - , Pos(0) - { - } - - TStringBuf Data; - size_t Pos; - }; -} + } + + Ch Take() { + auto c = Peek(); + ++Pos; + return c; + } + + size_t Tell() const { + return Pos; + } + + TStringBufStreamWrapper(TStringBuf data) + : Data(data) + , Pos(0) + { + } + + TStringBuf Data; + size_t Pos; + }; +} diff --git a/library/cpp/json/ut/json_reader_ut.cpp b/library/cpp/json/ut/json_reader_ut.cpp index 48bd0b9bbf..cd31afa0b8 100644 --- a/library/cpp/json/ut/json_reader_ut.cpp +++ b/library/cpp/json/ut/json_reader_ut.cpp @@ -383,19 +383,19 @@ Y_UNIT_TEST_SUITE(TJsonReaderTest) { } catch (...) { } } // TJsonMemoryLeakTest - - Y_UNIT_TEST(TJsonDuplicateKeysWithNullValuesTest) { - const TString json = "{\"\":null,\"\":\"\"}"; - - TStringInput in(json); - NJson::TJsonValue v; - UNIT_ASSERT(ReadJsonTree(&in, &v)); - UNIT_ASSERT(v.IsMap()); - UNIT_ASSERT_VALUES_EQUAL(1, v.GetMap().size()); - UNIT_ASSERT_VALUES_EQUAL("", v.GetMap().begin()->first); - UNIT_ASSERT(v.GetMap().begin()->second.IsString()); - UNIT_ASSERT_VALUES_EQUAL("", v.GetMap().begin()->second.GetString()); - } + + Y_UNIT_TEST(TJsonDuplicateKeysWithNullValuesTest) { + const TString json = "{\"\":null,\"\":\"\"}"; + + TStringInput in(json); + NJson::TJsonValue v; + UNIT_ASSERT(ReadJsonTree(&in, &v)); + UNIT_ASSERT(v.IsMap()); + UNIT_ASSERT_VALUES_EQUAL(1, v.GetMap().size()); + UNIT_ASSERT_VALUES_EQUAL("", v.GetMap().begin()->first); + UNIT_ASSERT(v.GetMap().begin()->second.IsString()); + UNIT_ASSERT_VALUES_EQUAL("", v.GetMap().begin()->second.GetString()); + } } diff --git a/library/cpp/json/ya.make b/library/cpp/json/ya.make index 04fecc5410..d58eead8ec 100644 --- a/library/cpp/json/ya.make +++ b/library/cpp/json/ya.make @@ -9,11 +9,11 @@ SRCS( json_writer.cpp json_reader.cpp json_prettifier.cpp - rapidjson_helpers.cpp + rapidjson_helpers.cpp ) PEERDIR( - contrib/libs/rapidjson + contrib/libs/rapidjson library/cpp/json/common library/cpp/json/fast_sax library/cpp/json/writer |