diff options
author | aligus <aligus@yandex-team.ru> | 2022-02-10 16:49:24 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:24 +0300 |
commit | c2bb7445128368226bd179d54d6a238c9fd3065e (patch) | |
tree | 5d5cb817648f650d76cf1076100726fd9b8448e8 /library/cpp | |
parent | 3f79772915172a9f5407c1a815806a8c8ba67c60 (diff) | |
download | ydb-c2bb7445128368226bd179d54d6a238c9fd3065e.tar.gz |
Restoring authorship annotation for <aligus@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp')
-rw-r--r-- | library/cpp/binsaver/class_factory.h | 4 | ||||
-rw-r--r-- | library/cpp/json/json_reader.cpp | 48 | ||||
-rw-r--r-- | library/cpp/json/json_reader.h | 12 | ||||
-rw-r--r-- | library/cpp/json/json_writer.cpp | 34 | ||||
-rw-r--r-- | library/cpp/json/json_writer.h | 36 | ||||
-rw-r--r-- | library/cpp/json/ut/json_reader_ut.cpp | 190 | ||||
-rw-r--r-- | library/cpp/json/ut/json_writer_ut.cpp | 30 | ||||
-rw-r--r-- | library/cpp/json/writer/json_value.cpp | 108 | ||||
-rw-r--r-- | library/cpp/json/writer/json_value.h | 48 | ||||
-rw-r--r-- | library/cpp/json/ya.make | 20 |
10 files changed, 265 insertions, 265 deletions
diff --git a/library/cpp/binsaver/class_factory.h b/library/cpp/binsaver/class_factory.h index 657df90429..e83512331b 100644 --- a/library/cpp/binsaver/class_factory.h +++ b/library/cpp/binsaver/class_factory.h @@ -88,13 +88,13 @@ void TClassFactory<T>::RegisterTypeBase(int nTypeID, newFunc func, VFT vft) { fprintf(stderr, "IBinSaver: Type ID 0x%08X has been already used\n", nTypeID); abort(); } - } + } CTypeIndexHash::iterator typeIndexIt = typeIndex.find(vft); if (typeIndexIt != typeIndex.end() && nTypeID != typeIndexIt->second) { fprintf(stderr, "IBinSaver: class (Type ID 0x%08X) has been already registered (Type ID 0x%08X)\n", nTypeID, typeIndexIt->second); abort(); - } + } typeIndex[vft] = nTypeID; typeInfo[nTypeID] = func; } diff --git a/library/cpp/json/json_reader.cpp b/library/cpp/json/json_reader.cpp index c74581e240..072c8deafe 100644 --- a/library/cpp/json/json_reader.cpp +++ b/library/cpp/json/json_reader.cpp @@ -5,13 +5,13 @@ #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> - -namespace NJson { + +namespace NJson { namespace { TString PrintError(const rapidjson::ParseResult& result) { return TStringBuilder() << TStringBuf("Offset: ") << result.Offset() @@ -21,7 +21,7 @@ namespace NJson { } static const size_t DEFAULT_BUFFER_LEN = 65536; - + bool TParserCallbacks::OpenComplexValue(EJsonValueType type) { TJsonValue* pvalue; switch (CurrentState) { @@ -42,12 +42,12 @@ namespace NJson { return false; } return true; - } - + } + bool TParserCallbacks::CloseComplexValue() { if (ValuesStack.empty()) { - return false; - } + return false; + } ValuesStack.pop_back(); if (!ValuesStack.empty()) { @@ -65,8 +65,8 @@ namespace NJson { CurrentState = FINISH; } return true; - } - + } + TParserCallbacks::TParserCallbacks(TJsonValue& value, bool throwOnError, bool notClosedBracketIsError) : TJsonCallbacks(throwOnError) , Value(value) @@ -74,23 +74,23 @@ namespace NJson { , CurrentState(START) { } - + bool TParserCallbacks::OnNull() { return SetValue(JSON_NULL); } - + bool TParserCallbacks::OnBoolean(bool val) { return SetValue(val); } - + bool TParserCallbacks::OnInteger(long long val) { return SetValue(val); } - + bool TParserCallbacks::OnUInteger(unsigned long long val) { return SetValue(val); } - + bool TParserCallbacks::OnString(const TStringBuf& val) { return SetValue(val); } @@ -98,29 +98,29 @@ namespace NJson { bool TParserCallbacks::OnDouble(double val) { return SetValue(val); } - + bool TParserCallbacks::OnOpenArray() { bool res = OpenComplexValue(JSON_ARRAY); if (res) CurrentState = IN_ARRAY; return res; } - + bool TParserCallbacks::OnCloseArray() { return CloseComplexValue(); } - + bool TParserCallbacks::OnOpenMap() { bool res = OpenComplexValue(JSON_MAP); if (res) CurrentState = IN_MAP; return res; } - + bool TParserCallbacks::OnCloseMap() { return CloseComplexValue(); } - + bool TParserCallbacks::OnMapKey(const TStringBuf& val) { switch (CurrentState) { case IN_MAP: @@ -131,8 +131,8 @@ namespace NJson { return false; } return true; - } - + } + bool TParserCallbacks::OnEnd() { if (NotClosedBracketIsError){ return ValuesStack.empty(); @@ -144,7 +144,7 @@ namespace NJson { : BufferSize(DEFAULT_BUFFER_LEN) { } - + void TJsonReaderConfig::SetBufferSize(size_t bufferSize) { BufferSize = Max((size_t)1, Min(bufferSize, DEFAULT_BUFFER_LEN)); } @@ -441,7 +441,7 @@ namespace NJson { bool Null() { return Impl.OnNull(); } - + bool Bool(bool b) { return Impl.OnBoolean(b); } diff --git a/library/cpp/json/json_reader.h b/library/cpp/json/json_reader.h index 10276c068c..b673788330 100644 --- a/library/cpp/json/json_reader.h +++ b/library/cpp/json/json_reader.h @@ -1,5 +1,5 @@ -#pragma once - +#pragma once + #include "json_value.h" #include <library/cpp/json/common/defs.h> @@ -7,14 +7,14 @@ #include <util/generic/yexception.h> -#include <util/stream/input.h> +#include <util/stream/input.h> #include <util/stream/str.h> #include <util/stream/mem.h> - -namespace NJson { + +namespace NJson { struct TJsonReaderConfig { TJsonReaderConfig(); - + // js-style comments (both // and /**/) bool AllowComments = false; bool DontValidateUtf8 = false; diff --git a/library/cpp/json/json_writer.cpp b/library/cpp/json/json_writer.cpp index a95a53891d..3d058bae36 100644 --- a/library/cpp/json/json_writer.cpp +++ b/library/cpp/json/json_writer.cpp @@ -1,11 +1,11 @@ -#include "json_writer.h" - +#include "json_writer.h" + #include <util/charset/utf8.h> #include <util/generic/algorithm.h> -#include <util/string/cast.h> -#include <util/system/yassert.h> - -namespace NJson { +#include <util/string/cast.h> +#include <util/system/yassert.h> + +namespace NJson { TJsonWriter::TJsonWriter(IOutputStream* out, bool formatOutput, bool sortkeys, bool validateUtf8) : Out(out) , Buf(NJsonWriter::HEM_UNSAFE) @@ -19,7 +19,7 @@ namespace NJson { { Buf.SetIndentSpaces(formatOutput ? 2 : 0); } - + TJsonWriter::TJsonWriter(IOutputStream* out, const TJsonWriterConfig& config, bool DFID) : Out(config.Unbuffered ? nullptr : out) , Buf(NJsonWriter::HEM_UNSAFE, config.Unbuffered ? out : nullptr) @@ -34,7 +34,7 @@ namespace NJson { Buf.SetIndentSpaces(config.FormatOutput ? 2 : 0); Buf.SetWriteNanAsString(config.WriteNanAsString); } - + TJsonWriter::~TJsonWriter() { // if we write to socket it's possible to get exception here // don't use exceptions in destructors @@ -51,23 +51,23 @@ namespace NJson { Buf.FlushTo(Out); } } - + void TJsonWriter::OpenMap() { Buf.BeginObject(); } - + void TJsonWriter::CloseMap() { Buf.EndObject(); } - + void TJsonWriter::OpenArray() { Buf.BeginList(); } - + void TJsonWriter::CloseArray() { Buf.EndList(); } - + void TJsonWriter::Write(const TStringBuf& value) { if (ValidateUtf8 && !IsUtf(value)) throw yexception() << "JSON writer: invalid UTF-8"; @@ -81,7 +81,7 @@ namespace NJson { } } } - + void TJsonWriter::WriteNull() { Buf.WriteNull(); } @@ -93,11 +93,11 @@ namespace NJson { void TJsonWriter::Write(double value) { Buf.WriteDouble(value, FloatToStringMode, DoubleNDigits); } - + void TJsonWriter::Write(long long value) { Buf.WriteLongLong(value); } - + void TJsonWriter::Write(unsigned long long value) { Buf.WriteULongLong(value); } @@ -105,7 +105,7 @@ namespace NJson { void TJsonWriter::Write(bool value) { Buf.WriteBool(value); } - + namespace { struct TLessStrPtr { bool operator()(const TString* a, const TString* b) const { diff --git a/library/cpp/json/json_writer.h b/library/cpp/json/json_writer.h index 0b5b1039c1..c7f5c9499a 100644 --- a/library/cpp/json/json_writer.h +++ b/library/cpp/json/json_writer.h @@ -1,17 +1,17 @@ -#pragma once - +#pragma once + // Deprecated. Use library/cpp/json/writer in new code. #include "json_value.h" #include <library/cpp/json/writer/json.h> -#include <util/stream/output.h> -#include <util/generic/hash.h> +#include <util/stream/output.h> +#include <util/generic/hash.h> #include <util/generic/maybe.h> -#include <util/generic/strbuf.h> - -namespace NJson { +#include <util/generic/strbuf.h> + +namespace NJson { struct TJsonWriterConfig { constexpr static ui32 DefaultDoubleNDigits = 10; constexpr static ui32 DefaultFloatNDigits = 6; @@ -19,7 +19,7 @@ namespace NJson { inline TJsonWriterConfig& SetUnbuffered(bool v) noexcept { Unbuffered = v; - + return *this; } @@ -61,25 +61,25 @@ namespace NJson { TJsonWriter(IOutputStream* out, bool formatOutput, bool sortkeys = false, bool validateUtf8 = true); TJsonWriter(IOutputStream* out, const TJsonWriterConfig& config, bool DontFlushInDestructor = false); ~TJsonWriter(); - + void Flush(); - + void OpenMap(); void OpenMap(const TStringBuf& key) { Buf.WriteKey(key); OpenMap(); } void CloseMap(); - + void OpenArray(); void OpenArray(const TStringBuf& key) { Buf.WriteKey(key); OpenArray(); } void CloseArray(); - + void WriteNull(); - + void Write(const TStringBuf& value); void Write(float value); void Write(double value); @@ -109,7 +109,7 @@ namespace NJson { void Write(unsigned short value) { Write((unsigned long long)value); } - + void Write(const unsigned char* value) { Write((const char*)value); } @@ -122,12 +122,12 @@ namespace NJson { void Write(const std::string& value) { Write(TStringBuf(value)); } - + // write raw json without checks void UnsafeWrite(const TStringBuf& value) { Buf.UnsafeWriteValue(value); } - + template <typename T> void Write(const TStringBuf& key, const T& value) { Buf.WriteKey(key); @@ -139,7 +139,7 @@ namespace NJson { Buf.WriteKey(key); UnsafeWrite(value); } - + void WriteNull(const TStringBuf& key) { Buf.WriteKey(key); WriteNull(); @@ -193,4 +193,4 @@ namespace NJson { TString WriteJson(const TJsonValue*, bool formatOutput = true, bool sortkeys = false, bool validateUtf8 = false); TString WriteJson(const TJsonValue&, bool formatOutput = true, bool sortkeys = false, bool validateUtf8 = false); void WriteJson(IOutputStream*, const TJsonValue*, const TJsonWriterConfig& config); -} +} diff --git a/library/cpp/json/ut/json_reader_ut.cpp b/library/cpp/json/ut/json_reader_ut.cpp index 5868bc6427..cd31afa0b8 100644 --- a/library/cpp/json/ut/json_reader_ut.cpp +++ b/library/cpp/json/ut/json_reader_ut.cpp @@ -1,101 +1,101 @@ #include <library/cpp/json/json_reader.h> #include <library/cpp/json/json_writer.h> - + #include <library/cpp/testing/unittest/registar.h> -#include <util/stream/str.h> - -using namespace NJson; - -class TReformatCallbacks: public TJsonCallbacks { +#include <util/stream/str.h> + +using namespace NJson; + +class TReformatCallbacks: public TJsonCallbacks { TJsonWriter& Writer; -public: +public: TReformatCallbacks(TJsonWriter& writer) : Writer(writer) { - } - + } + bool OnBoolean(bool val) override { - Writer.Write(val); - return true; - } - + Writer.Write(val); + return true; + } + bool OnInteger(long long val) override { - Writer.Write(val); - return true; - } - + Writer.Write(val); + return true; + } + bool OnUInteger(unsigned long long val) override { Writer.Write(val); return true; } bool OnString(const TStringBuf& val) override { - Writer.Write(val); - return true; - } - + Writer.Write(val); + return true; + } + bool OnDouble(double val) override { - Writer.Write(val); - return true; - } - + Writer.Write(val); + return true; + } + bool OnOpenArray() override { - Writer.OpenArray(); - return true; - } - + Writer.OpenArray(); + return true; + } + bool OnCloseArray() override { - Writer.CloseArray(); - return true; - } - + Writer.CloseArray(); + return true; + } + bool OnOpenMap() override { - Writer.OpenArray(); - return true; - } - + Writer.OpenArray(); + return true; + } + bool OnCloseMap() override { - Writer.CloseArray(); - return true; - } - + Writer.CloseArray(); + return true; + } + bool OnMapKey(const TStringBuf& val) override { - Writer.Write(val); - return true; - } -}; - + Writer.Write(val); + return true; + } +}; + Y_UNIT_TEST_SUITE(TJsonReaderTest) { Y_UNIT_TEST(JsonReformatTest) { TString data = "{\"null value\": null, \"intkey\": 10, \"double key\": 11.11, \"string key\": \"string\", \"array\": [1,2,3,\"TString\"], \"bool key\": true}"; - + TString result1, result2; - { - TStringStream in; - in << data; - TStringStream out; - TJsonWriter writer(&out, false); - TReformatCallbacks cb(writer); - ReadJson(&in, &cb); - writer.Flush(); - result1 = out.Str(); - } - - { - TStringStream in; - in << result1; - TStringStream out; - TJsonWriter writer(&out, false); - TReformatCallbacks cb(writer); - ReadJson(&in, &cb); - writer.Flush(); + { + TStringStream in; + in << data; + TStringStream out; + TJsonWriter writer(&out, false); + TReformatCallbacks cb(writer); + ReadJson(&in, &cb); + writer.Flush(); + result1 = out.Str(); + } + + { + TStringStream in; + in << result1; + TStringStream out; + TJsonWriter writer(&out, false); + TReformatCallbacks cb(writer); + ReadJson(&in, &cb); + writer.Flush(); result2 = out.Str(); - } - - UNIT_ASSERT_VALUES_EQUAL(result1, result2); - } - + } + + UNIT_ASSERT_VALUES_EQUAL(result1, result2); + } + Y_UNIT_TEST(TJsonEscapedApostrophe) { TString jsonString = "{ \"foo\" : \"bar\\'buzz\" }"; { @@ -121,22 +121,22 @@ Y_UNIT_TEST_SUITE(TJsonReaderTest) { Y_UNIT_TEST(TJsonTreeTest) { TString data = "{\"intkey\": 10, \"double key\": 11.11, \"null value\":null, \"string key\": \"string\", \"array\": [1,2,3,\"TString\"], \"bool key\": true}"; - TStringStream in; - in << data; - TJsonValue value; - ReadJsonTree(&in, &value); - + TStringStream in; + in << data; + TJsonValue value; + ReadJsonTree(&in, &value); + UNIT_ASSERT_VALUES_EQUAL(value["intkey"].GetInteger(), 10); - UNIT_ASSERT_DOUBLES_EQUAL(value["double key"].GetDouble(), 11.11, 0.001); - UNIT_ASSERT_VALUES_EQUAL(value["bool key"].GetBoolean(), true); + UNIT_ASSERT_DOUBLES_EQUAL(value["double key"].GetDouble(), 11.11, 0.001); + UNIT_ASSERT_VALUES_EQUAL(value["bool key"].GetBoolean(), true); UNIT_ASSERT_VALUES_EQUAL(value["absent string key"].GetString(), TString("")); UNIT_ASSERT_VALUES_EQUAL(value["string key"].GetString(), TString("string")); - UNIT_ASSERT_VALUES_EQUAL(value["array"][0].GetInteger(), 1); - UNIT_ASSERT_VALUES_EQUAL(value["array"][1].GetInteger(), 2); - UNIT_ASSERT_VALUES_EQUAL(value["array"][2].GetInteger(), 3); + UNIT_ASSERT_VALUES_EQUAL(value["array"][0].GetInteger(), 1); + UNIT_ASSERT_VALUES_EQUAL(value["array"][1].GetInteger(), 2); + UNIT_ASSERT_VALUES_EQUAL(value["array"][2].GetInteger(), 3); UNIT_ASSERT_VALUES_EQUAL(value["array"][3].GetString(), TString("TString")); UNIT_ASSERT(value["null value"].IsNull()); - + // AsString UNIT_ASSERT_VALUES_EQUAL(value["intkey"].GetStringRobust(), "10"); UNIT_ASSERT_VALUES_EQUAL(value["double key"].GetStringRobust(), "11.11"); @@ -149,22 +149,22 @@ Y_UNIT_TEST_SUITE(TJsonReaderTest) { UNIT_ASSERT(GetArrayPointer(value, "array", &array)); UNIT_ASSERT_VALUES_EQUAL(value["array"].GetArray().size(), array->size()); UNIT_ASSERT_VALUES_EQUAL(value["array"][0].GetInteger(), (*array)[0].GetInteger()); - UNIT_ASSERT_VALUES_EQUAL(value["array"][1].GetInteger(), (*array)[1].GetInteger()); - UNIT_ASSERT_VALUES_EQUAL(value["array"][2].GetInteger(), (*array)[2].GetInteger()); + UNIT_ASSERT_VALUES_EQUAL(value["array"][1].GetInteger(), (*array)[1].GetInteger()); + UNIT_ASSERT_VALUES_EQUAL(value["array"][2].GetInteger(), (*array)[2].GetInteger()); UNIT_ASSERT_VALUES_EQUAL(value["array"][3].GetString(), (*array)[3].GetString()); - } - + } + Y_UNIT_TEST(TJsonRomaTest) { TString data = "{\"test\": [ {\"name\": \"A\"} ]}"; - - TStringStream in; - in << data; - TJsonValue value; - ReadJsonTree(&in, &value); - + + TStringStream in; + in << data; + TJsonValue value; + ReadJsonTree(&in, &value); + UNIT_ASSERT_VALUES_EQUAL(value["test"][0]["name"].GetString(), TString("A")); - } - + } + Y_UNIT_TEST(TJsonReadTreeWithComments) { { TString leadingCommentData = "{ // \"test\" : 1 \n}"; @@ -396,7 +396,7 @@ Y_UNIT_TEST_SUITE(TJsonReaderTest) { UNIT_ASSERT(v.GetMap().begin()->second.IsString()); UNIT_ASSERT_VALUES_EQUAL("", v.GetMap().begin()->second.GetString()); } -} +} static const TString YANDEX_STREAMING_JSON("{\"a\":1}//d{\"b\":2}"); diff --git a/library/cpp/json/ut/json_writer_ut.cpp b/library/cpp/json/ut/json_writer_ut.cpp index cee42780ff..ca11d34dad 100644 --- a/library/cpp/json/ut/json_writer_ut.cpp +++ b/library/cpp/json/ut/json_writer_ut.cpp @@ -1,46 +1,46 @@ #include <library/cpp/json/json_writer.h> #include <library/cpp/testing/unittest/registar.h> - -#include <util/stream/str.h> - -using namespace NJson; - + +#include <util/stream/str.h> + +using namespace NJson; + Y_UNIT_TEST_SUITE(TJsonWriterTest) { Y_UNIT_TEST(SimpleWriteTest) { TString expected1 = "{\"key1\":1,\"key2\":2,\"key3\":3"; TString expected2 = expected1 + ",\"array\":[\"stroka\",false]"; TString expected3 = expected2 + "}"; - + TStringStream out; - + TJsonWriter json(&out, false); json.OpenMap(); json.Write("key1", (ui16)1); json.WriteKey("key2"); json.Write((i32)2); json.Write("key3", (ui64)3); - + UNIT_ASSERT(out.Empty()); json.Flush(); UNIT_ASSERT_VALUES_EQUAL(out.Str(), expected1); - + 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); - + json.CloseMap(); - + 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; @@ -53,7 +53,7 @@ Y_UNIT_TEST_SUITE(TJsonWriterTest) { TStringStream out; WriteJson(&out, &v); UNIT_ASSERT_VALUES_EQUAL(out.Str(), expected); - } + } Y_UNIT_TEST(FormatOutput) { TString expected = "{\n \"key1\":null,\n \"key2\":\n {\n \"subkey1\":\n [\n 1,\n {\n \"subsubkey\":\"test2\"\n },\n null,\n true\n ],\n \"subkey2\":\"test\"\n }\n}"; @@ -225,4 +225,4 @@ Y_UNIT_TEST_SUITE(TJsonWriterTest) { UNIT_ASSERT_VALUES_EQUAL(actual, expected); } } -} +} diff --git a/library/cpp/json/writer/json_value.cpp b/library/cpp/json/writer/json_value.cpp index c04c975b31..c61e8d1dc4 100644 --- a/library/cpp/json/writer/json_value.cpp +++ b/library/cpp/json/writer/json_value.cpp @@ -1,6 +1,6 @@ -#include "json_value.h" +#include "json_value.h" #include "json.h" - + #include <util/generic/ymath.h> #include <util/generic/ylimits.h> #include <util/generic/utility.h> @@ -67,9 +67,9 @@ AreJsonArraysEqual(const NJson::TJsonValue& lhs, const NJson::TJsonValue& rhs) { return true; } -namespace NJson { +namespace NJson { const TJsonValue TJsonValue::UNDEFINED{}; - + TJsonValue::TJsonValue(const EJsonValueType type) { SetType(type); } @@ -80,7 +80,7 @@ namespace NJson { vval.SwapWithUndefined(*this); Zero(vval.Value); } - + TJsonValue::TJsonValue(const TJsonValue& val) : Type(val.Type) { @@ -103,16 +103,16 @@ namespace NJson { std::memcpy(&Value, &val.Value, sizeof(Value)); break; } - } - + } + TJsonValue& TJsonValue::operator=(const TJsonValue& val) { if (this == &val) return *this; TJsonValue tmp(val); tmp.Swap(*this); - return *this; + return *this; } - + TJsonValue& TJsonValue::operator=(TJsonValue&& val) noexcept { if (this == &val) return *this; @@ -125,12 +125,12 @@ namespace NJson { SetType(JSON_BOOLEAN); Value.Boolean = value; } - + TJsonValue::TJsonValue(const long long value) noexcept { SetType(JSON_INTEGER); Value.Integer = value; } - + TJsonValue::TJsonValue(const unsigned long long value) noexcept { SetType(JSON_UINTEGER); Value.UInteger = value; @@ -140,7 +140,7 @@ namespace NJson { SetType(JSON_INTEGER); Value.Integer = value; } - + TJsonValue::TJsonValue(const unsigned int value) noexcept { SetType(JSON_UINTEGER); Value.UInteger = value; @@ -150,7 +150,7 @@ namespace NJson { SetType(JSON_INTEGER); Value.Integer = value; } - + TJsonValue::TJsonValue(const unsigned long value) noexcept { SetType(JSON_UINTEGER); Value.UInteger = value; @@ -179,14 +179,14 @@ namespace NJson { EJsonValueType TJsonValue::GetType() const noexcept { return Type; } - + TJsonValue& TJsonValue::SetType(const EJsonValueType type) { if (Type == type) return *this; Clear(); Type = type; - + switch (Type) { case JSON_STRING: new (&Value.String) TString(); @@ -207,17 +207,17 @@ namespace NJson { } return *this; - } - + } + TJsonValue& TJsonValue::SetValue(const TJsonValue& value) { return *this = value; } - + TJsonValue& TJsonValue::SetValue(TJsonValue&& value) { *this = std::move(value); return *this; } - + TJsonValue& TJsonValue::InsertValue(const TString& key, const TJsonValue& value) { SetType(JSON_MAP); return (*Value.Map)[key] = value; @@ -227,12 +227,12 @@ namespace NJson { SetType(JSON_MAP); return (*Value.Map)[key] = value; } - + TJsonValue& TJsonValue::InsertValue(const char* key, const TJsonValue& value) { SetType(JSON_MAP); return (*Value.Map)[key] = value; } - + TJsonValue& TJsonValue::InsertValue(const TString& key, TJsonValue&& value) { SetType(JSON_MAP); return (*Value.Map)[key] = std::move(value); @@ -269,7 +269,7 @@ namespace NJson { Value.Array->push_back(std::move(value)); return Value.Array->back(); } - + void TJsonValue::EraseValue(const TStringBuf key) { if (IsMap()) { TMapType::iterator it = Value.Map->find(key); @@ -317,12 +317,12 @@ namespace NJson { Value.Array->resize(idx + 1); return (*Value.Array)[idx]; } - + TJsonValue& TJsonValue::operator[](const TStringBuf& key) { SetType(JSON_MAP); return (*Value.Map)[key]; - } - + } + namespace { struct TDefaultsHolder { const TString String{}; @@ -331,23 +331,23 @@ namespace NJson { const TJsonValue Value{}; }; } - + const TJsonValue& TJsonValue::operator[](const size_t idx) const noexcept { const TJsonValue* ret = nullptr; if (GetValuePointer(idx, &ret)) return *ret; - + return Singleton<TDefaultsHolder>()->Value; } - + const TJsonValue& TJsonValue::operator[](const TStringBuf& key) const noexcept { const TJsonValue* ret = nullptr; if (GetValuePointer(key, &ret)) return *ret; - + return Singleton<TDefaultsHolder>()->Value; } - + bool TJsonValue::GetBoolean() const { return Type != JSON_BOOLEAN ? false : Value.Boolean; } @@ -355,11 +355,11 @@ namespace NJson { long long TJsonValue::GetInteger() const { if (!IsInteger()) return 0; - + switch (Type) { case JSON_INTEGER: return Value.Integer; - + case JSON_UINTEGER: return Value.UInteger; @@ -652,7 +652,7 @@ namespace NJson { *value = Value.Boolean; return true; } - + bool TJsonValue::GetInteger(long long* value) const noexcept { if (!IsInteger()) return false; @@ -660,7 +660,7 @@ namespace NJson { *value = GetInteger(); return true; } - + bool TJsonValue::GetUInteger(unsigned long long* value) const noexcept { if (!IsUInteger()) return false; @@ -676,7 +676,7 @@ namespace NJson { *value = GetDouble(); return true; } - + bool TJsonValue::GetString(TString* value) const { if (Type != JSON_STRING) return false; @@ -684,7 +684,7 @@ namespace NJson { *value = Value.String; return true; } - + bool TJsonValue::GetMap(TJsonValue::TMapType* value) const { if (Type != JSON_MAP) return false; @@ -692,7 +692,7 @@ namespace NJson { *value = *Value.Map; return true; } - + bool TJsonValue::GetArray(TJsonValue::TArray* value) const { if (Type != JSON_ARRAY) return false; @@ -700,15 +700,15 @@ namespace NJson { *value = *Value.Array; return true; } - + bool TJsonValue::GetMapPointer(const TJsonValue::TMapType** value) const noexcept { if (Type != JSON_MAP) return false; - + *value = Value.Map; return true; } - + bool TJsonValue::GetArrayPointer(const TJsonValue::TArray** value) const noexcept { if (Type != JSON_ARRAY) return false; @@ -716,7 +716,7 @@ namespace NJson { *value = Value.Array; return true; } - + bool TJsonValue::GetValue(const size_t index, TJsonValue* value) const { const TJsonValue* tmp = nullptr; if (GetValuePointer(index, &tmp)) { @@ -725,7 +725,7 @@ namespace NJson { } return false; } - + bool TJsonValue::GetValue(const TStringBuf key, TJsonValue* value) const { const TJsonValue* tmp = nullptr; if (GetValuePointer(key, &tmp)) { @@ -734,7 +734,7 @@ namespace NJson { } return false; } - + bool TJsonValue::GetValuePointer(const size_t index, const TJsonValue** value) const noexcept { if (Type == JSON_ARRAY && index < Value.Array->size()) { *value = &(*Value.Array)[index]; @@ -742,7 +742,7 @@ namespace NJson { } return false; } - + bool TJsonValue::GetValuePointer(const TStringBuf key, const TJsonValue** value) const noexcept { if (Type == JSON_MAP) { const TMapType::const_iterator it = Value.Map->find(key); @@ -757,11 +757,11 @@ namespace NJson { bool TJsonValue::GetValuePointer(const TStringBuf key, TJsonValue** value) noexcept { return static_cast<const TJsonValue*>(this)->GetValuePointer(key, const_cast<const TJsonValue**>(value)); } - + bool TJsonValue::IsNull() const noexcept { return Type == JSON_NULL; } - + bool TJsonValue::IsBoolean() const noexcept { return Type == JSON_BOOLEAN; } @@ -781,7 +781,7 @@ namespace NJson { return false; } } - + bool TJsonValue::IsUInteger() const noexcept { switch (Type) { case JSON_UINTEGER: @@ -815,7 +815,7 @@ namespace NJson { return false; } } - + namespace { template <class TPtr, class T> TPtr* CreateOrNullptr(TPtr* p, T key, std::true_type /*create*/) { @@ -988,7 +988,7 @@ namespace NJson { rhs.SwapWithUndefined(*this); tmp.SwapWithUndefined(rhs); } - + void TJsonValue::Save(IOutputStream* s) const { ::Save(s, static_cast<ui8>(Type)); switch (Type) { @@ -1061,7 +1061,7 @@ namespace NJson { *value = &v->GetMap(); return true; } - + bool GetArrayPointer(const TJsonValue& jv, const size_t index, const TJsonValue::TArray** value) { const TJsonValue* v; if (!jv.GetValuePointer(index, &v) || !v->IsArray()) @@ -1070,7 +1070,7 @@ namespace NJson { *value = &v->GetArray(); return true; } - + bool GetMapPointer(const TJsonValue& jv, const TStringBuf key, const TJsonValue::TMapType** value) { const TJsonValue* v; if (!jv.GetValuePointer(key, &v) || !v->IsMap()) @@ -1084,7 +1084,7 @@ namespace NJson { const TJsonValue* v; if (!jv.GetValuePointer(key, &v) || !v->IsArray()) return false; - + *value = &v->GetArray(); return true; } @@ -1096,8 +1096,8 @@ namespace NJson { if (Value.Array->empty()) ythrow TJsonException() << "Get back on empty array"; } -} - +} + template <> void Out<NJson::TJsonValue>(IOutputStream& out, const NJson::TJsonValue& v) { NJsonWriter::TBuf buf(NJsonWriter::HEM_DONT_ESCAPE_HTML, &out); diff --git a/library/cpp/json/writer/json_value.h b/library/cpp/json/writer/json_value.h index fa7cd76615..3f0f50bc4c 100644 --- a/library/cpp/json/writer/json_value.h +++ b/library/cpp/json/writer/json_value.h @@ -1,15 +1,15 @@ -#pragma once - +#pragma once + #include <library/cpp/json/common/defs.h> #include <util/generic/string.h> -#include <util/generic/hash.h> -#include <util/generic/vector.h> +#include <util/generic/hash.h> +#include <util/generic/vector.h> #include <util/generic/deque.h> #include <util/generic/utility.h> -#include <util/generic/yexception.h> - -namespace NJson { +#include <util/generic/yexception.h> + +namespace NJson { enum EJsonValueType { JSON_UNDEFINED /* "Undefined" */, JSON_NULL /* "Null" */, @@ -21,9 +21,9 @@ namespace NJson { JSON_ARRAY /* "Array" */, JSON_UINTEGER /* "UInteger" */ }; - + class TJsonValue; - + class IScanCallback { public: virtual ~IScanCallback() = default; @@ -37,7 +37,7 @@ namespace NJson { public: typedef THashMap<TString, TJsonValue> TMapType; typedef TDeque<TJsonValue> TArray; - + TJsonValue() noexcept = default; TJsonValue(EJsonValueType type); TJsonValue(bool value) noexcept; @@ -61,20 +61,20 @@ namespace NJson { TJsonValue(const TJsonValue& vval); TJsonValue(TJsonValue&& vval) noexcept; - + TJsonValue& operator=(const TJsonValue& val); TJsonValue& operator=(TJsonValue&& val) noexcept; - + ~TJsonValue() { Clear(); } EJsonValueType GetType() const noexcept; TJsonValue& SetType(EJsonValueType type); - + TJsonValue& SetValue(const TJsonValue& value); TJsonValue& SetValue(TJsonValue&& value); - + // for Map TJsonValue& InsertValue(const TString& key, const TJsonValue& value); TJsonValue& InsertValue(TStringBuf key, const TJsonValue& value); @@ -82,13 +82,13 @@ namespace NJson { TJsonValue& InsertValue(const TString& key, TJsonValue&& value); TJsonValue& InsertValue(TStringBuf key, TJsonValue&& value); TJsonValue& InsertValue(const char* key, TJsonValue&& value); - + // for Array TJsonValue& AppendValue(const TJsonValue& value); TJsonValue& AppendValue(TJsonValue&& value); TJsonValue& Back(); const TJsonValue& Back() const; - + bool GetValueByPath(TStringBuf path, TJsonValue& result, char delimiter = '.') const; bool SetValueByPath(TStringBuf path, const TJsonValue& value, char delimiter = '.'); bool SetValueByPath(TStringBuf path, TJsonValue&& value, char delimiter = '.'); @@ -112,7 +112,7 @@ namespace NJson { const TString& GetString() const; const TMapType& GetMap() const; const TArray& GetArray() const; - + //throwing TJsonException possible bool GetBooleanSafe() const; long long GetIntegerSafe() const; @@ -123,7 +123,7 @@ namespace NJson { TMapType& GetMapSafe(); const TArray& GetArraySafe() const; TArray& GetArraySafe(); - + bool GetBooleanSafe(bool defaultValue) const; long long GetIntegerSafe(long long defaultValue) const; unsigned long long GetUIntegerSafe(unsigned long long defaultValue) const; @@ -157,7 +157,7 @@ namespace NJson { bool IsDefined() const noexcept { return Type != JSON_UNDEFINED && Type != JSON_NULL; } - + bool IsNull() const noexcept; bool IsBoolean() const noexcept; bool IsDouble() const noexcept; @@ -167,7 +167,7 @@ namespace NJson { /// @return true if JSON_INTEGER or (JSON_UINTEGER and Value <= Max<long long>) bool IsInteger() const noexcept; - + /// @return true if JSON_UINTEGER or (JSON_INTEGER and Value >= 0) bool IsUInteger() const noexcept; @@ -175,7 +175,7 @@ namespace NJson { bool Has(size_t key) const noexcept; void Scan(IScanCallback& callback); - + /// Non-robust comparison. bool operator==(const TJsonValue& rhs) const; @@ -219,7 +219,7 @@ namespace NJson { */ void BackChecks() const; }; - + inline bool GetBoolean(const TJsonValue& jv, size_t index, bool* value) noexcept { return jv[index].GetBoolean(value); } @@ -246,11 +246,11 @@ namespace NJson { inline bool GetBoolean(const TJsonValue& jv, TStringBuf key, bool* value) noexcept { return jv[key].GetBoolean(value); } - + inline bool GetInteger(const TJsonValue& jv, TStringBuf key, long long* value) noexcept { return jv[key].GetInteger(value); } - + inline bool GetUInteger(const TJsonValue& jv, TStringBuf key, unsigned long long* value) noexcept { return jv[key].GetUInteger(value); } diff --git a/library/cpp/json/ya.make b/library/cpp/json/ya.make index ca5cef5d8e..d58eead8ec 100644 --- a/library/cpp/json/ya.make +++ b/library/cpp/json/ya.make @@ -1,23 +1,23 @@ -LIBRARY() - +LIBRARY() + OWNER( pg velavokr ) SRCS( - json_writer.cpp - json_reader.cpp + json_writer.cpp + json_reader.cpp json_prettifier.cpp rapidjson_helpers.cpp -) - -PEERDIR( +) + +PEERDIR( contrib/libs/rapidjson library/cpp/json/common library/cpp/json/fast_sax library/cpp/json/writer library/cpp/string_utils/relaxed_escaper -) - -END() +) + +END() |