diff options
| author | nae202 <[email protected]> | 2025-09-23 07:48:35 +0300 |
|---|---|---|
| committer | nae202 <[email protected]> | 2025-09-23 08:03:21 +0300 |
| commit | 63774bc851bb76e24f369ef4e0328e5f05edbb4b (patch) | |
| tree | d3226ed9a5f200097949f6001a7e3183aff7adfa /library/cpp/json/writer/json_value.cpp | |
| parent | f4cbf746a0b6649479cfb305eceababced0ac10a (diff) | |
Style fix
commit_hash:d506e2a0f2770f3662449a3711e976499eb9ccbb
Diffstat (limited to 'library/cpp/json/writer/json_value.cpp')
| -rw-r--r-- | library/cpp/json/writer/json_value.cpp | 238 |
1 files changed, 105 insertions, 133 deletions
diff --git a/library/cpp/json/writer/json_value.cpp b/library/cpp/json/writer/json_value.cpp index e71e9ce8199..6a0546fe043 100644 --- a/library/cpp/json/writer/json_value.cpp +++ b/library/cpp/json/writer/json_value.cpp @@ -20,25 +20,25 @@ AreJsonMapsEqual(const NJson::TJsonValue& lhs, const NJson::TJsonValue& rhs) { Y_ABORT_UNLESS(lhs.GetType() == JSON_MAP, "lhs has not a JSON_MAP type."); - if (rhs.GetType() != JSON_MAP) + 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()) + if (lhsMap.size() != rhsMap.size()) { return false; - + } for (const auto& lhsIt : lhsMap) { TMapType::const_iterator rhsIt = rhsMap.find(lhsIt.first); - if (rhsIt == rhsMap.end()) + if (rhsIt == rhsMap.end()) { return false; - - if (lhsIt.second != rhsIt->second) + } + if (lhsIt.second != rhsIt->second) { return false; + } } - return true; } @@ -48,22 +48,22 @@ AreJsonArraysEqual(const NJson::TJsonValue& lhs, const NJson::TJsonValue& rhs) { Y_ABORT_UNLESS(lhs.GetType() == JSON_ARRAY, "lhs has not a JSON_ARRAY type."); - if (rhs.GetType() != JSON_ARRAY) + 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()) + 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) + if (*lhsIt != *rhsIt) { return false; + } } - return true; } @@ -106,16 +106,18 @@ namespace NJson { } TJsonValue& TJsonValue::operator=(const TJsonValue& val) { - if (this == &val) + if (this == &val) { return *this; + } TJsonValue tmp(val); tmp.Swap(*this); return *this; } TJsonValue& TJsonValue::operator=(TJsonValue&& val) noexcept { - if (this == &val) + if (this == &val) { return *this; + } TJsonValue tmp(std::move(val)); tmp.Swap(*this); return *this; @@ -181,12 +183,11 @@ namespace NJson { } TJsonValue& TJsonValue::SetType(const EJsonValueType type) { - if (Type == type) + if (Type == type) { return *this; - + } Clear(); Type = type; - switch (Type) { case JSON_STRING: new (&Value.String) TString(); @@ -205,7 +206,6 @@ namespace NJson { case JSON_DOUBLE: break; } - return *this; } @@ -273,8 +273,9 @@ namespace NJson { void TJsonValue::EraseValue(const TStringBuf key) { if (IsMap()) { TMapType::iterator it = Value.Map->find(key); - if (it != Value.Map->end()) + if (it != Value.Map->end()) { Value.Map->erase(it); + } } } @@ -313,8 +314,9 @@ namespace NJson { TJsonValue& TJsonValue::operator[](const size_t idx) { SetType(JSON_ARRAY); - if (Value.Array->size() <= idx) + if (Value.Array->size() <= idx) { Value.Array->resize(idx + 1); + } return (*Value.Array)[idx]; } @@ -330,21 +332,21 @@ namespace NJson { const TJsonValue::TArray Array{}; const TJsonValue Value{}; }; - } + } // namespace const TJsonValue& TJsonValue::operator[](const size_t idx) const noexcept { const TJsonValue* ret = nullptr; - if (GetValuePointer(idx, &ret)) + 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)) + if (GetValuePointer(key, &ret)) { return *ret; - + } return Singleton<TDefaultsHolder>()->Value; } @@ -353,9 +355,9 @@ namespace NJson { } long long TJsonValue::GetInteger() const { - if (!IsInteger()) + if (!IsInteger()) { return 0; - + } switch (Type) { case JSON_INTEGER: return Value.Integer; @@ -373,19 +375,16 @@ namespace NJson { } unsigned long long TJsonValue::GetUInteger() const { - if (!IsUInteger()) + 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; @@ -393,19 +392,16 @@ namespace NJson { } double TJsonValue::GetDouble() const { - if (!IsDouble()) + 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; @@ -425,79 +421,79 @@ namespace NJson { } bool TJsonValue::GetBooleanSafe() const { - if (Type != JSON_BOOLEAN) + if (Type != JSON_BOOLEAN) { ythrow TJsonException() << "Not a boolean"; - + } return Value.Boolean; } long long TJsonValue::GetIntegerSafe() const { - if (!IsInteger()) + if (!IsInteger()) { ythrow TJsonException() << "Not an integer"; - + } return GetInteger(); } unsigned long long TJsonValue::GetUIntegerSafe() const { - if (!IsUInteger()) + if (!IsUInteger()) { ythrow TJsonException() << "Not an unsigned integer"; - + } return GetUInteger(); } double TJsonValue::GetDoubleSafe() const { - if (!IsDouble()) + if (!IsDouble()) { ythrow TJsonException() << "Not a double"; - + } return GetDouble(); } const TString& TJsonValue::GetStringSafe() const { - if (Type != JSON_STRING) + if (Type != JSON_STRING) { ythrow TJsonException() << "Not a string"; - + } return Value.String; } bool TJsonValue::GetBooleanSafe(const bool defaultValue) const { - if (Type == JSON_UNDEFINED) + if (Type == JSON_UNDEFINED) { return defaultValue; - + } return GetBooleanSafe(); } long long TJsonValue::GetIntegerSafe(const long long defaultValue) const { - if (Type == JSON_UNDEFINED) + if (Type == JSON_UNDEFINED) { return defaultValue; - + } return GetIntegerSafe(); } unsigned long long TJsonValue::GetUIntegerSafe(const unsigned long long defaultValue) const { - if (Type == JSON_UNDEFINED) + if (Type == JSON_UNDEFINED) { return defaultValue; - + } return GetUIntegerSafe(); } double TJsonValue::GetDoubleSafe(const double defaultValue) const { - if (Type == JSON_UNDEFINED) + if (Type == JSON_UNDEFINED) { return defaultValue; - + } return GetDoubleSafe(); } TString TJsonValue::GetStringSafe(const TString& defaultValue) const { - if (Type == JSON_UNDEFINED) + if (Type == JSON_UNDEFINED) { return defaultValue; - + } return GetStringSafe(); } const TJsonValue::TMapType& TJsonValue::GetMapSafe() const { - if (Type != JSON_MAP) + if (Type != JSON_MAP) { ythrow TJsonException() << "Not a map"; - + } return *Value.Map; } @@ -506,9 +502,9 @@ namespace NJson { } const TJsonValue::TArray& TJsonValue::GetArraySafe() const { - if (Type != JSON_ARRAY) + if (Type != JSON_ARRAY) { ythrow TJsonException() << "Not an array"; - + } return *Value.Array; } @@ -646,73 +642,73 @@ namespace NJson { } bool TJsonValue::GetBoolean(bool* value) const noexcept { - if (Type != JSON_BOOLEAN) + if (Type != JSON_BOOLEAN) { return false; - + } *value = Value.Boolean; return true; } bool TJsonValue::GetInteger(long long* value) const noexcept { - if (!IsInteger()) + if (!IsInteger()) { return false; - + } *value = GetInteger(); return true; } bool TJsonValue::GetUInteger(unsigned long long* value) const noexcept { - if (!IsUInteger()) + if (!IsUInteger()) { return false; - + } *value = GetUInteger(); return true; } bool TJsonValue::GetDouble(double* value) const noexcept { - if (!IsDouble()) + if (!IsDouble()) { return false; - + } *value = GetDouble(); return true; } bool TJsonValue::GetString(TString* value) const { - if (Type != JSON_STRING) + if (Type != JSON_STRING) { return false; - + } *value = Value.String; return true; } bool TJsonValue::GetMap(TJsonValue::TMapType* value) const { - if (Type != JSON_MAP) + if (Type != JSON_MAP) { return false; - + } *value = *Value.Map; return true; } bool TJsonValue::GetArray(TJsonValue::TArray* value) const { - if (Type != JSON_ARRAY) + if (Type != JSON_ARRAY) { return false; - + } *value = *Value.Array; return true; } bool TJsonValue::GetMapPointer(const TJsonValue::TMapType** value) const noexcept { - if (Type != JSON_MAP) + if (Type != JSON_MAP) { return false; - + } *value = Value.Map; return true; } bool TJsonValue::GetArrayPointer(const TJsonValue::TArray** value) const noexcept { - if (Type != JSON_ARRAY) + if (Type != JSON_ARRAY) { return false; - + } *value = Value.Array; return true; } @@ -770,13 +766,10 @@ namespace NJson { 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; } @@ -786,13 +779,10 @@ namespace NJson { 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; } @@ -804,13 +794,10 @@ namespace NJson { 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; } @@ -860,12 +847,10 @@ namespace NJson { } else { currentJson = CreateOrNullptr(currentJson, step, create_tag); } - if (!currentJson) { return nullptr; } } - return currentJson; } } // anonymous namespace @@ -909,7 +894,6 @@ namespace NJson { if (!callback.Do(path, parent, *this)) { return; } - if (Type == JSON_MAP) { for (auto&& i : *Value.Map) { i.second.DoScan(!!path ? TString::Join(path, ".", i.first) : i.first, this, callback); @@ -947,40 +931,24 @@ namespace NJson { bool TJsonValue::operator==(const TJsonValue& rhs) const { switch (Type) { - case JSON_UNDEFINED: { + case JSON_UNDEFINED: return (rhs.GetType() == JSON_UNDEFINED); - } - - case JSON_NULL: { + case JSON_NULL: return rhs.IsNull(); - } - - case JSON_BOOLEAN: { + case JSON_BOOLEAN: return (rhs.IsBoolean() && Value.Boolean == rhs.Value.Boolean); - } - - case JSON_INTEGER: { + case JSON_INTEGER: return (rhs.IsInteger() && GetInteger() == rhs.GetInteger()); - } - - case JSON_UINTEGER: { + case JSON_UINTEGER: return (rhs.IsUInteger() && GetUInteger() == rhs.GetUInteger()); - } - - case JSON_STRING: { + case JSON_STRING: return (rhs.IsString() && Value.String == rhs.Value.String); - } - - case JSON_DOUBLE: { + case JSON_DOUBLE: return (rhs.IsDouble() && fabs(GetDouble() - rhs.GetDouble()) <= FLT_EPSILON); - } - case JSON_MAP: return AreJsonMapsEqual(*this, rhs); - case JSON_ARRAY: return AreJsonArraysEqual(*this, rhs); - default: Y_ASSERT(false && "Unknown type."); return false; @@ -995,7 +963,6 @@ namespace NJson { } else { std::memcpy(&output.Value, &Value, sizeof(Value)); } - output.Type = Type; Type = JSON_UNDEFINED; } @@ -1009,8 +976,10 @@ namespace NJson { void TJsonValue::Save(IOutputStream* s) const { ::Save(s, static_cast<ui8>(Type)); switch (Type) { - case JSON_UNDEFINED:break; - case JSON_NULL:break; + case JSON_UNDEFINED: + break; + case JSON_NULL: + break; case JSON_BOOLEAN: ::Save(s, Value.Boolean); break; @@ -1042,8 +1011,10 @@ namespace NJson { SetType(static_cast<EJsonValueType>(loadedType)); } switch (Type) { - case JSON_UNDEFINED:break; - case JSON_NULL:break; + case JSON_UNDEFINED: + break; + case JSON_NULL: + break; case JSON_BOOLEAN: ::Load(s, Value.Boolean); break; @@ -1072,48 +1043,49 @@ namespace NJson { bool GetMapPointer(const TJsonValue& jv, const size_t index, const TJsonValue::TMapType** value) { const TJsonValue* v; - if (!jv.GetValuePointer(index, &v) || !v->IsMap()) + if (!jv.GetValuePointer(index, &v) || !v->IsMap()) { return false; - + } *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()) + if (!jv.GetValuePointer(index, &v) || !v->IsArray()) { return false; - + } *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()) + if (!jv.GetValuePointer(key, &v) || !v->IsMap()) { return false; - + } *value = &v->GetMap(); return true; } bool GetArrayPointer(const TJsonValue& jv, const TStringBuf key, const TJsonValue::TArray** value) { const TJsonValue* v; - if (!jv.GetValuePointer(key, &v) || !v->IsArray()) + if (!jv.GetValuePointer(key, &v) || !v->IsArray()) { return false; - + } *value = &v->GetArray(); return true; } void TJsonValue::BackChecks() const { - if (Type != JSON_ARRAY) + if (Type != JSON_ARRAY) { ythrow TJsonException() << "Not an array"; - - if (Value.Array->empty()) + } + if (Value.Array->empty()) { ythrow TJsonException() << "Get back on empty array"; + } } -} +} // namespace NJson template <> void Out<NJson::TJsonValue>(IOutputStream& out, const NJson::TJsonValue& v) { |
