aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/json/writer
diff options
context:
space:
mode:
authorfamilom <familom@yandex-team.ru>2022-02-10 16:49:49 +0300
committerDaniil Cherednik <dcherednik@yandex-team.ru>2022-02-10 16:49:49 +0300
commitf281aaf77179d27d6208b873e95ae6cd45765a63 (patch)
treeb4229c6ece98c855bd9821ef0b656042c29a8953 /library/cpp/json/writer
parent53d07fb9e28d179add32cd299c9341bf8a231a31 (diff)
downloadydb-f281aaf77179d27d6208b873e95ae6cd45765a63.tar.gz
Restoring authorship annotation for <familom@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/json/writer')
-rw-r--r--library/cpp/json/writer/json_value.cpp190
-rw-r--r--library/cpp/json/writer/json_value.h8
-rw-r--r--library/cpp/json/writer/json_value_ut.cpp350
3 files changed, 274 insertions, 274 deletions
diff --git a/library/cpp/json/writer/json_value.cpp b/library/cpp/json/writer/json_value.cpp
index c61e8d1dc4..3e7ab2915e 100644
--- a/library/cpp/json/writer/json_value.cpp
+++ b/library/cpp/json/writer/json_value.cpp
@@ -2,7 +2,7 @@
#include "json.h"
#include <util/generic/ymath.h>
-#include <util/generic/ylimits.h>
+#include <util/generic/ylimits.h>
#include <util/generic/utility.h>
#include <util/generic/singleton.h>
#include <util/stream/str.h>
@@ -10,63 +10,63 @@
#include <util/string/cast.h>
#include <util/string/type.h>
#include <util/string/vector.h>
-#include <util/system/yassert.h>
+#include <util/system/yassert.h>
#include <util/ysaveload.h>
#include <util/generic/bt_exception.h>
-static bool
-AreJsonMapsEqual(const NJson::TJsonValue& lhs, const NJson::TJsonValue& rhs) {
- using namespace NJson;
-
+static bool
+AreJsonMapsEqual(const NJson::TJsonValue& lhs, const NJson::TJsonValue& rhs) {
+ using namespace NJson;
+
Y_VERIFY(lhs.GetType() == JSON_MAP, "lhs has not a JSON_MAP type.");
-
- if (rhs.GetType() != JSON_MAP)
- return false;
-
+
+ if (rhs.GetType() != JSON_MAP)
+ return false;
+
typedef TJsonValue::TMapType TMapType;
const TMapType& lhsMap = lhs.GetMap();
const TMapType& rhsMap = rhs.GetMap();
-
- if (lhsMap.size() != rhsMap.size())
- return false;
-
+
+ if (lhsMap.size() != rhsMap.size())
+ return false;
+
for (const auto& lhsIt : lhsMap) {
TMapType::const_iterator rhsIt = rhsMap.find(lhsIt.first);
- if (rhsIt == rhsMap.end())
- return false;
-
+ if (rhsIt == rhsMap.end())
+ return false;
+
if (lhsIt.second != rhsIt->second)
- return false;
- }
-
- return true;
-}
-
-static bool
-AreJsonArraysEqual(const NJson::TJsonValue& lhs, const NJson::TJsonValue& rhs) {
- using namespace NJson;
-
+ return false;
+ }
+
+ return true;
+}
+
+static bool
+AreJsonArraysEqual(const NJson::TJsonValue& lhs, const NJson::TJsonValue& rhs) {
+ using namespace NJson;
+
Y_VERIFY(lhs.GetType() == JSON_ARRAY, "lhs has not a JSON_ARRAY type.");
-
- if (rhs.GetType() != JSON_ARRAY)
- return false;
-
- typedef TJsonValue::TArray TArray;
- const TArray& lhsArray = lhs.GetArray();
- const TArray& rhsArray = rhs.GetArray();
-
- if (lhsArray.size() != rhsArray.size())
- return false;
-
- for (TArray::const_iterator lhsIt = lhsArray.begin(), rhsIt = rhsArray.begin();
+
+ if (rhs.GetType() != JSON_ARRAY)
+ return false;
+
+ typedef TJsonValue::TArray TArray;
+ const TArray& lhsArray = lhs.GetArray();
+ const TArray& rhsArray = rhs.GetArray();
+
+ if (lhsArray.size() != rhsArray.size())
+ return false;
+
+ for (TArray::const_iterator lhsIt = lhsArray.begin(), rhsIt = rhsArray.begin();
lhsIt != lhsArray.end(); ++lhsIt, ++rhsIt) {
- if (*lhsIt != *rhsIt)
- return false;
- }
-
- return true;
-}
-
+ if (*lhsIt != *rhsIt)
+ return false;
+ }
+
+ return true;
+}
+
namespace NJson {
const TJsonValue TJsonValue::UNDEFINED{};
@@ -135,7 +135,7 @@ namespace NJson {
SetType(JSON_UINTEGER);
Value.UInteger = value;
}
-
+
TJsonValue::TJsonValue(const int value) noexcept {
SetType(JSON_INTEGER);
Value.Integer = value;
@@ -145,7 +145,7 @@ namespace NJson {
SetType(JSON_UINTEGER);
Value.UInteger = value;
}
-
+
TJsonValue::TJsonValue(const long value) noexcept {
SetType(JSON_INTEGER);
Value.Integer = value;
@@ -155,7 +155,7 @@ namespace NJson {
SetType(JSON_UINTEGER);
Value.UInteger = value;
}
-
+
TJsonValue::TJsonValue(const double value) noexcept {
SetType(JSON_DOUBLE);
Value.Double = value;
@@ -362,55 +362,55 @@ namespace NJson {
case JSON_UINTEGER:
return Value.UInteger;
-
+
case JSON_DOUBLE:
return Value.Double;
-
+
default:
Y_ASSERT(false && "Unexpected type.");
return 0;
}
- }
+ }
unsigned long long TJsonValue::GetUInteger() const {
if (!IsUInteger())
return 0;
-
+
switch (Type) {
case JSON_UINTEGER:
return Value.UInteger;
-
+
case JSON_INTEGER:
return Value.Integer;
-
+
case JSON_DOUBLE:
return Value.Double;
-
+
default:
Y_ASSERT(false && "Unexpected type.");
return 0;
}
- }
-
+ }
+
double TJsonValue::GetDouble() const {
if (!IsDouble())
return 0.0;
-
+
switch (Type) {
case JSON_DOUBLE:
return Value.Double;
-
+
case JSON_INTEGER:
return Value.Integer;
-
+
case JSON_UINTEGER:
return Value.UInteger;
-
+
default:
Y_ASSERT(false && "Unexpected type.");
return 0.0;
}
- }
+ }
const TString& TJsonValue::GetString() const {
return Type != JSON_STRING ? Singleton<TDefaultsHolder>()->String : Value.String;
@@ -593,8 +593,8 @@ namespace NJson {
case JSON_UINTEGER:
return Value.UInteger;
}
- }
-
+ }
+
double TJsonValue::GetDoubleRobust() const noexcept {
switch (Type) {
case JSON_ARRAY:
@@ -664,11 +664,11 @@ namespace NJson {
bool TJsonValue::GetUInteger(unsigned long long* value) const noexcept {
if (!IsUInteger())
return false;
-
+
*value = GetUInteger();
return true;
}
-
+
bool TJsonValue::GetDouble(double* value) const noexcept {
if (!IsDouble())
return false;
@@ -765,56 +765,56 @@ namespace NJson {
bool TJsonValue::IsBoolean() const noexcept {
return Type == JSON_BOOLEAN;
}
-
+
bool TJsonValue::IsInteger() const noexcept {
switch (Type) {
case JSON_INTEGER:
return true;
-
+
case JSON_UINTEGER:
return (Value.UInteger <= static_cast<unsigned long long>(Max<long long>()));
-
+
case JSON_DOUBLE:
return ((long long)Value.Double == Value.Double);
default:
return false;
}
- }
+ }
bool TJsonValue::IsUInteger() const noexcept {
switch (Type) {
case JSON_UINTEGER:
return true;
-
+
case JSON_INTEGER:
return (Value.Integer >= 0);
-
+
case JSON_DOUBLE:
return ((unsigned long long)Value.Double == Value.Double);
-
+
default:
return false;
}
- }
-
+ }
+
bool TJsonValue::IsDouble() const noexcept {
// Check whether we can convert integer to floating-point
// without precision loss.
switch (Type) {
case JSON_DOUBLE:
return true;
-
+
case JSON_INTEGER:
return (1ll << std::numeric_limits<double>::digits) >= Abs(Value.Integer);
-
+
case JSON_UINTEGER:
return (1ull << std::numeric_limits<double>::digits) >= Value.UInteger;
-
+
default:
return false;
}
- }
+ }
namespace {
template <class TPtr, class T>
@@ -906,42 +906,42 @@ namespace NJson {
void TJsonValue::Scan(IScanCallback& callback) {
DoScan("", nullptr, callback);
- }
-
+ }
+
bool TJsonValue::IsString() const noexcept {
return Type == JSON_STRING;
- }
-
+ }
+
bool TJsonValue::IsMap() const noexcept {
return Type == JSON_MAP;
- }
-
+ }
+
bool TJsonValue::IsArray() const noexcept {
return Type == JSON_ARRAY;
- }
-
+ }
+
bool TJsonValue::Has(const TStringBuf& key) const noexcept {
return Type == JSON_MAP && Value.Map->contains(key);
- }
-
+ }
+
bool TJsonValue::Has(size_t key) const noexcept {
return Type == JSON_ARRAY && Value.Array->size() > key;
- }
-
+ }
+
bool TJsonValue::operator==(const TJsonValue& rhs) const {
switch (Type) {
case JSON_UNDEFINED: {
return (rhs.GetType() == JSON_UNDEFINED);
}
-
+
case JSON_NULL: {
return rhs.IsNull();
}
-
+
case JSON_BOOLEAN: {
return (rhs.IsBoolean() && Value.Boolean == rhs.Value.Boolean);
}
-
+
case JSON_INTEGER: {
return (rhs.IsInteger() && GetInteger() == rhs.GetInteger());
}
diff --git a/library/cpp/json/writer/json_value.h b/library/cpp/json/writer/json_value.h
index 3f0f50bc4c..d777d07062 100644
--- a/library/cpp/json/writer/json_value.h
+++ b/library/cpp/json/writer/json_value.h
@@ -170,10 +170,10 @@ namespace NJson {
/// @return true if JSON_UINTEGER or (JSON_INTEGER and Value >= 0)
bool IsUInteger() const noexcept;
-
+
bool Has(const TStringBuf& key) const noexcept;
bool Has(size_t key) const noexcept;
-
+
void Scan(IScanCallback& callback);
/// Non-robust comparison.
@@ -182,9 +182,9 @@ namespace NJson {
bool operator!=(const TJsonValue& rhs) const {
return !(*this == rhs);
}
-
+
void Swap(TJsonValue& rhs) noexcept;
-
+
// save using util/ysaveload.h serialization (not to JSON stream)
void Save(IOutputStream* s) const;
diff --git a/library/cpp/json/writer/json_value_ut.cpp b/library/cpp/json/writer/json_value_ut.cpp
index dc7f6affdf..7cb1003a75 100644
--- a/library/cpp/json/writer/json_value_ut.cpp
+++ b/library/cpp/json/writer/json_value_ut.cpp
@@ -1,11 +1,11 @@
-#include "json_value.h"
-
+#include "json_value.h"
+
#include <library/cpp/testing/unittest/registar.h>
-
-#include <util/stream/input.h>
-
-using namespace NJson;
-
+
+#include <util/stream/input.h>
+
+using namespace NJson;
+
Y_UNIT_TEST_SUITE(TJsonValueTest) {
Y_UNIT_TEST(UndefTest) {
TJsonValue undef;
@@ -37,177 +37,177 @@ Y_UNIT_TEST_SUITE(TJsonValueTest) {
}
Y_UNIT_TEST(DefaultCompareTest) {
- {
- TJsonValue lhs;
- TJsonValue rhs;
- UNIT_ASSERT(lhs == rhs);
- UNIT_ASSERT(rhs == lhs);
- }
-
- {
- TJsonValue lhs;
- TJsonValue rhs(JSON_NULL);
- UNIT_ASSERT(lhs != rhs);
- UNIT_ASSERT(rhs != lhs);
- }
- }
-
+ {
+ TJsonValue lhs;
+ TJsonValue rhs;
+ UNIT_ASSERT(lhs == rhs);
+ UNIT_ASSERT(rhs == lhs);
+ }
+
+ {
+ TJsonValue lhs;
+ TJsonValue rhs(JSON_NULL);
+ UNIT_ASSERT(lhs != rhs);
+ UNIT_ASSERT(rhs != lhs);
+ }
+ }
+
Y_UNIT_TEST(NullCompareTest) {
- TJsonValue lhs(JSON_NULL);
- TJsonValue rhs(JSON_NULL);
- UNIT_ASSERT(lhs == rhs);
- UNIT_ASSERT(rhs == lhs);
- }
-
+ TJsonValue lhs(JSON_NULL);
+ TJsonValue rhs(JSON_NULL);
+ UNIT_ASSERT(lhs == rhs);
+ UNIT_ASSERT(rhs == lhs);
+ }
+
Y_UNIT_TEST(StringCompareTest) {
- {
- TJsonValue lhs(JSON_STRING);
- TJsonValue rhs(JSON_STRING);
- UNIT_ASSERT(lhs == rhs);
- UNIT_ASSERT(rhs == lhs);
- }
-
- {
- TJsonValue lhs("");
- TJsonValue rhs("");
- UNIT_ASSERT(lhs == rhs);
- UNIT_ASSERT(rhs == lhs);
- }
-
- {
- TJsonValue lhs("abc");
- TJsonValue rhs("abc");
- UNIT_ASSERT(lhs == rhs);
- UNIT_ASSERT(rhs == lhs);
- }
-
- {
- TJsonValue lhs("1");
- TJsonValue rhs(1);
- UNIT_ASSERT(lhs != rhs);
- UNIT_ASSERT(rhs != lhs);
- }
- }
-
+ {
+ TJsonValue lhs(JSON_STRING);
+ TJsonValue rhs(JSON_STRING);
+ UNIT_ASSERT(lhs == rhs);
+ UNIT_ASSERT(rhs == lhs);
+ }
+
+ {
+ TJsonValue lhs("");
+ TJsonValue rhs("");
+ UNIT_ASSERT(lhs == rhs);
+ UNIT_ASSERT(rhs == lhs);
+ }
+
+ {
+ TJsonValue lhs("abc");
+ TJsonValue rhs("abc");
+ UNIT_ASSERT(lhs == rhs);
+ UNIT_ASSERT(rhs == lhs);
+ }
+
+ {
+ TJsonValue lhs("1");
+ TJsonValue rhs(1);
+ UNIT_ASSERT(lhs != rhs);
+ UNIT_ASSERT(rhs != lhs);
+ }
+ }
+
Y_UNIT_TEST(ArrayCompareTest) {
- {
- TJsonValue lhs(JSON_ARRAY);
- TJsonValue rhs(JSON_ARRAY);
- UNIT_ASSERT(lhs == rhs);
- UNIT_ASSERT(rhs == lhs);
- }
-
- {
- TJsonValue lhs;
- TJsonValue rhs;
-
- lhs.AppendValue(TJsonValue());
-
- UNIT_ASSERT(lhs != rhs);
- UNIT_ASSERT(rhs != lhs);
- }
-
- {
- TJsonValue lhs;
- TJsonValue rhs;
-
- lhs.AppendValue(1);
- lhs.AppendValue("2");
- lhs.AppendValue(3.0);
- lhs.AppendValue(TJsonValue());
- lhs.AppendValue(TJsonValue(JSON_NULL));
-
- rhs.AppendValue(1);
- rhs.AppendValue("2");
- rhs.AppendValue(3.0);
- rhs.AppendValue(TJsonValue());
- rhs.AppendValue(TJsonValue(JSON_NULL));
-
- UNIT_ASSERT(lhs == rhs);
- UNIT_ASSERT(rhs == lhs);
- }
-
- {
- TJsonValue lhs;
- TJsonValue rhs;
-
- lhs.AppendValue(1);
- rhs.AppendValue("1");
-
- UNIT_ASSERT(lhs != rhs);
- UNIT_ASSERT(rhs != lhs);
- }
- }
-
+ {
+ TJsonValue lhs(JSON_ARRAY);
+ TJsonValue rhs(JSON_ARRAY);
+ UNIT_ASSERT(lhs == rhs);
+ UNIT_ASSERT(rhs == lhs);
+ }
+
+ {
+ TJsonValue lhs;
+ TJsonValue rhs;
+
+ lhs.AppendValue(TJsonValue());
+
+ UNIT_ASSERT(lhs != rhs);
+ UNIT_ASSERT(rhs != lhs);
+ }
+
+ {
+ TJsonValue lhs;
+ TJsonValue rhs;
+
+ lhs.AppendValue(1);
+ lhs.AppendValue("2");
+ lhs.AppendValue(3.0);
+ lhs.AppendValue(TJsonValue());
+ lhs.AppendValue(TJsonValue(JSON_NULL));
+
+ rhs.AppendValue(1);
+ rhs.AppendValue("2");
+ rhs.AppendValue(3.0);
+ rhs.AppendValue(TJsonValue());
+ rhs.AppendValue(TJsonValue(JSON_NULL));
+
+ UNIT_ASSERT(lhs == rhs);
+ UNIT_ASSERT(rhs == lhs);
+ }
+
+ {
+ TJsonValue lhs;
+ TJsonValue rhs;
+
+ lhs.AppendValue(1);
+ rhs.AppendValue("1");
+
+ UNIT_ASSERT(lhs != rhs);
+ UNIT_ASSERT(rhs != lhs);
+ }
+ }
+
Y_UNIT_TEST(CompareTest) {
- {
- TJsonValue lhs;
- lhs.InsertValue("null value", TJsonValue(JSON_NULL));
- lhs.InsertValue("int key", TJsonValue(10));
- lhs.InsertValue("double key", TJsonValue(11.11));
- lhs.InsertValue("string key", TJsonValue("string"));
-
- TJsonValue array;
- array.AppendValue(1);
- array.AppendValue(2);
- array.AppendValue(3);
- array.AppendValue("string");
- lhs.InsertValue("array", array);
-
- lhs.InsertValue("bool key", TJsonValue(true));
-
- TJsonValue rhs;
- rhs = lhs;
-
- UNIT_ASSERT(lhs == rhs);
- UNIT_ASSERT(rhs == lhs);
- }
-
- {
- // Insert keys in different orders
- const int NUM_KEYS = 1000;
-
- TJsonValue lhs;
- for (int i = 0; i < NUM_KEYS; ++i)
- lhs.InsertValue(ToString(i), i);
-
- TJsonValue rhs;
- for (int i = 0; i < NUM_KEYS; i += 2)
- rhs.InsertValue(ToString(i), i);
- for (int i = 1; i < NUM_KEYS; i += 2)
- rhs.InsertValue(ToString(i), i);
-
- UNIT_ASSERT(lhs == rhs);
- UNIT_ASSERT(rhs == lhs);
- }
-
- {
- TJsonValue lhs;
- lhs.InsertValue("null value", TJsonValue(JSON_NULL));
- lhs.InsertValue("int key", TJsonValue(10));
- lhs.InsertValue("double key", TJsonValue(11.11));
- lhs.InsertValue("string key", TJsonValue("string"));
-
- TJsonValue array;
- array.AppendValue(1);
- array.AppendValue(2);
- array.AppendValue(3);
- array.AppendValue("string");
- lhs.InsertValue("array", array);
-
- lhs.InsertValue("bool key", TJsonValue(true));
-
- TJsonValue rhs;
- rhs.InsertValue("null value", TJsonValue(JSON_NULL));
- rhs.InsertValue("int key", TJsonValue(10));
- rhs.InsertValue("double key", TJsonValue(11.11));
- rhs.InsertValue("string key", TJsonValue("string"));
- rhs.InsertValue("bool key", TJsonValue(true));
-
- UNIT_ASSERT(lhs != rhs);
- UNIT_ASSERT(rhs != lhs);
- }
- }
+ {
+ TJsonValue lhs;
+ lhs.InsertValue("null value", TJsonValue(JSON_NULL));
+ lhs.InsertValue("int key", TJsonValue(10));
+ lhs.InsertValue("double key", TJsonValue(11.11));
+ lhs.InsertValue("string key", TJsonValue("string"));
+
+ TJsonValue array;
+ array.AppendValue(1);
+ array.AppendValue(2);
+ array.AppendValue(3);
+ array.AppendValue("string");
+ lhs.InsertValue("array", array);
+
+ lhs.InsertValue("bool key", TJsonValue(true));
+
+ TJsonValue rhs;
+ rhs = lhs;
+
+ UNIT_ASSERT(lhs == rhs);
+ UNIT_ASSERT(rhs == lhs);
+ }
+
+ {
+ // Insert keys in different orders
+ const int NUM_KEYS = 1000;
+
+ TJsonValue lhs;
+ for (int i = 0; i < NUM_KEYS; ++i)
+ lhs.InsertValue(ToString(i), i);
+
+ TJsonValue rhs;
+ for (int i = 0; i < NUM_KEYS; i += 2)
+ rhs.InsertValue(ToString(i), i);
+ for (int i = 1; i < NUM_KEYS; i += 2)
+ rhs.InsertValue(ToString(i), i);
+
+ UNIT_ASSERT(lhs == rhs);
+ UNIT_ASSERT(rhs == lhs);
+ }
+
+ {
+ TJsonValue lhs;
+ lhs.InsertValue("null value", TJsonValue(JSON_NULL));
+ lhs.InsertValue("int key", TJsonValue(10));
+ lhs.InsertValue("double key", TJsonValue(11.11));
+ lhs.InsertValue("string key", TJsonValue("string"));
+
+ TJsonValue array;
+ array.AppendValue(1);
+ array.AppendValue(2);
+ array.AppendValue(3);
+ array.AppendValue("string");
+ lhs.InsertValue("array", array);
+
+ lhs.InsertValue("bool key", TJsonValue(true));
+
+ TJsonValue rhs;
+ rhs.InsertValue("null value", TJsonValue(JSON_NULL));
+ rhs.InsertValue("int key", TJsonValue(10));
+ rhs.InsertValue("double key", TJsonValue(11.11));
+ rhs.InsertValue("string key", TJsonValue("string"));
+ rhs.InsertValue("bool key", TJsonValue(true));
+
+ UNIT_ASSERT(lhs != rhs);
+ UNIT_ASSERT(rhs != lhs);
+ }
+ }
Y_UNIT_TEST(SwapTest) {
{
@@ -647,4 +647,4 @@ Y_UNIT_TEST_SUITE(TJsonValueTest) {
UNIT_ASSERT_VALUES_EQUAL(filled["4"].GetMapSafe().size(), 1);
UNIT_ASSERT_VALUES_EQUAL(filled["4"]["5"], TJsonValue{5});
}
-} // TJsonValueTest
+} // TJsonValueTest