diff options
author | marakasov <marakasov@yandex-team.ru> | 2022-02-10 16:49:50 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:50 +0300 |
commit | 359de4829c1d82b06fad5e0ad82470e4e9f27c89 (patch) | |
tree | a58ba6d37e60106215ea04536f6f33d3e2fe4f6f /library/cpp/protobuf/json | |
parent | d94c2eed82b3c1259ac7320eb28e9731cd990c22 (diff) | |
download | ydb-359de4829c1d82b06fad5e0ad82470e4e9f27c89.tar.gz |
Restoring authorship annotation for <marakasov@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/protobuf/json')
-rw-r--r-- | library/cpp/protobuf/json/config.h | 4 | ||||
-rw-r--r-- | library/cpp/protobuf/json/proto2json_printer.cpp | 66 | ||||
-rw-r--r-- | library/cpp/protobuf/json/proto2json_printer.h | 6 | ||||
-rw-r--r-- | library/cpp/protobuf/json/ut/proto2json_ut.cpp | 12 |
4 files changed, 44 insertions, 44 deletions
diff --git a/library/cpp/protobuf/json/config.h b/library/cpp/protobuf/json/config.h index dc84fb4d5d..2cfd82b4be 100644 --- a/library/cpp/protobuf/json/config.h +++ b/library/cpp/protobuf/json/config.h @@ -82,7 +82,7 @@ namespace NProtobufJson { /// Custom field names generator. TNameGenerator NameGenerator = {}; - + /// Custom enum values generator. TEnumValueGenerator EnumValueGenerator = {}; @@ -144,7 +144,7 @@ namespace NProtobufJson { StringifyLongNumbers = stringify; return *this; } - + TSelf& SetNameGenerator(TNameGenerator callback) { NameGenerator = callback; return *this; diff --git a/library/cpp/protobuf/json/proto2json_printer.cpp b/library/cpp/protobuf/json/proto2json_printer.cpp index 6123eab0f2..145dbcabfc 100644 --- a/library/cpp/protobuf/json/proto2json_printer.cpp +++ b/library/cpp/protobuf/json/proto2json_printer.cpp @@ -201,8 +201,8 @@ namespace NProtobufJson { json.WriteKey(key).Write(value); \ } \ break; \ - } - + } + const Reflection* reflection = proto.GetReflection(); bool shouldPrintField = reflection->HasField(proto, &field); @@ -218,10 +218,10 @@ namespace NProtobufJson { if (shouldPrintField) { switch (field.cpp_type()) { - INT_FIELD_TO_JSON(CPPTYPE_INT32, GetInt32); - INT_FIELD_TO_JSON(CPPTYPE_INT64, GetInt64); - INT_FIELD_TO_JSON(CPPTYPE_UINT32, GetUInt32); - INT_FIELD_TO_JSON(CPPTYPE_UINT64, GetUInt64); + INT_FIELD_TO_JSON(CPPTYPE_INT32, GetInt32); + INT_FIELD_TO_JSON(CPPTYPE_INT64, GetInt64); + INT_FIELD_TO_JSON(CPPTYPE_UINT32, GetUInt32); + INT_FIELD_TO_JSON(CPPTYPE_UINT64, GetUInt64); FIELD_TO_JSON(CPPTYPE_DOUBLE, GetDouble); FIELD_TO_JSON(CPPTYPE_FLOAT, GetFloat); FIELD_TO_JSON(CPPTYPE_BOOL, GetBool); @@ -487,31 +487,31 @@ namespace NProtobufJson { } } - template <class T, class U> - std::enable_if_t<!std::is_unsigned<T>::value, bool> ValueInRange(T value, U range) { - return value >= -range && value <= range; - } - - template <class T, class U> - std::enable_if_t<std::is_unsigned<T>::value, bool> ValueInRange(T value, U range) { - return value <= (std::make_unsigned_t<U>)(range); - } - - template <class T> - bool TProto2JsonPrinter::NeedStringifyNumber(T value) const { - constexpr long SAFE_INTEGER_RANGE_FLOAT = 16777216; - constexpr long long SAFE_INTEGER_RANGE_DOUBLE = 9007199254740992; - - switch (GetConfig().StringifyLongNumbers) { - case TProto2JsonConfig::StringifyLongNumbersNever: - return false; - case TProto2JsonConfig::StringifyLongNumbersForFloat: - return !ValueInRange(value, SAFE_INTEGER_RANGE_FLOAT); - case TProto2JsonConfig::StringifyLongNumbersForDouble: - return !ValueInRange(value, SAFE_INTEGER_RANGE_DOUBLE); - } - - return false; - } - + template <class T, class U> + std::enable_if_t<!std::is_unsigned<T>::value, bool> ValueInRange(T value, U range) { + return value >= -range && value <= range; + } + + template <class T, class U> + std::enable_if_t<std::is_unsigned<T>::value, bool> ValueInRange(T value, U range) { + return value <= (std::make_unsigned_t<U>)(range); + } + + template <class T> + bool TProto2JsonPrinter::NeedStringifyNumber(T value) const { + constexpr long SAFE_INTEGER_RANGE_FLOAT = 16777216; + constexpr long long SAFE_INTEGER_RANGE_DOUBLE = 9007199254740992; + + switch (GetConfig().StringifyLongNumbers) { + case TProto2JsonConfig::StringifyLongNumbersNever: + return false; + case TProto2JsonConfig::StringifyLongNumbersForFloat: + return !ValueInRange(value, SAFE_INTEGER_RANGE_FLOAT); + case TProto2JsonConfig::StringifyLongNumbersForDouble: + return !ValueInRange(value, SAFE_INTEGER_RANGE_DOUBLE); + } + + return false; + } + } diff --git a/library/cpp/protobuf/json/proto2json_printer.h b/library/cpp/protobuf/json/proto2json_printer.h index 9dc5aa86c6..c60021aa8a 100644 --- a/library/cpp/protobuf/json/proto2json_printer.h +++ b/library/cpp/protobuf/json/proto2json_printer.h @@ -57,9 +57,9 @@ namespace NProtobufJson { const TStringBuf& key, const TString& value, IJsonOutput& json); - template <class T> - bool NeedStringifyNumber(T value) const; - + template <class T> + bool NeedStringifyNumber(T value) const; + protected: const TProto2JsonConfig& Config; TString TmpBuf; diff --git a/library/cpp/protobuf/json/ut/proto2json_ut.cpp b/library/cpp/protobuf/json/ut/proto2json_ut.cpp index 07e52d7f2f..1160269459 100644 --- a/library/cpp/protobuf/json/ut/proto2json_ut.cpp +++ b/library/cpp/protobuf/json/ut/proto2json_ut.cpp @@ -981,31 +981,31 @@ Y_UNIT_TEST(TestStringifyLongNumbers) { UNIT_ASSERT_EQUAL(jsonStr.Str(), "{\"SI64\":" #value "}"); \ } \ } while (false) - + TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersNever, 1, false); TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersNever, 1000000000, false); TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersNever, 10000000000000000, false); TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersNever, -1, false); TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersNever, -1000000000, false); TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersNever, -10000000000000000, false); - + TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForDouble, 1, false); TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForDouble, 1000000000, false); TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForDouble, 10000000000000000, true); TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForDouble, -1, false); TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForDouble, -1000000000, false); TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForDouble, -10000000000000000, true); - + TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForFloat, 1, false); TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForFloat, 1000000000, true); TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForFloat, 10000000000000000, true); TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForFloat, -1, false); TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForFloat, -1000000000, true); TEST_SINGLE(TProto2JsonConfig::StringifyLongNumbersForFloat, -10000000000000000, true); - -#undef TEST_SINGLE + +#undef TEST_SINGLE } // TestStringifyLongNumbers - + Y_UNIT_TEST(TestExtension) { TExtensionField proto; proto.SetExtension(bar, 1); |