diff options
author | maxfed <maxfed@yandex-team.com> | 2022-12-22 13:10:49 +0300 |
---|---|---|
committer | maxfed <maxfed@yandex-team.com> | 2022-12-22 13:10:49 +0300 |
commit | 3412f691a8c5be823c995c4da884039769b5aeef (patch) | |
tree | dc30ad224de959038d9a04a7b588eff711d147c2 /library | |
parent | dcc1ffc26fabcd374f52f45b980499d00fe5a00c (diff) | |
download | ydb-3412f691a8c5be823c995c4da884039769b5aeef.tar.gz |
Add settings for converting float to string in TProto2JsonConfig
init
Diffstat (limited to 'library')
-rw-r--r-- | library/cpp/protobuf/json/config.h | 22 | ||||
-rw-r--r-- | library/cpp/protobuf/json/json_writer_output.cpp | 3 | ||||
-rw-r--r-- | library/cpp/protobuf/json/ut/proto2json_ut.cpp | 26 |
3 files changed, 51 insertions, 0 deletions
diff --git a/library/cpp/protobuf/json/config.h b/library/cpp/protobuf/json/config.h index 8b9d60ab4f..2ffda4a065 100644 --- a/library/cpp/protobuf/json/config.h +++ b/library/cpp/protobuf/json/config.h @@ -5,6 +5,9 @@ #include <util/generic/vector.h> #include <util/generic/yexception.h> +#include <util/string/cast.h> + +#include <library/cpp/json/json_writer.h> #include <functional> @@ -12,6 +15,10 @@ namespace NProtobufJson { struct TProto2JsonConfig { using TSelf = TProto2JsonConfig; + ui32 DoubleNDigits = NJson::TJsonWriterConfig::DefaultDoubleNDigits; + ui32 FloatNDigits = NJson::TJsonWriterConfig::DefaultFloatNDigits; + EFloatToStringMode FloatToStringMode = NJson::TJsonWriterConfig::DefaultFloatToStringMode; + bool FormatOutput = false; enum MissingKeyMode { @@ -91,6 +98,21 @@ namespace NProtobufJson { bool WriteNanAsString = false; + TSelf& SetDoubleNDigits(ui32 ndigits) { + DoubleNDigits = ndigits; + return *this; + } + + TSelf& SetFloatNDigits(ui32 ndigits) { + FloatNDigits = ndigits; + return *this; + } + + TSelf& SetFloatToStringMode(EFloatToStringMode mode) { + FloatToStringMode = mode; + return *this; + } + TSelf& SetFormatOutput(bool format) { FormatOutput = format; return *this; diff --git a/library/cpp/protobuf/json/json_writer_output.cpp b/library/cpp/protobuf/json/json_writer_output.cpp index 288f645bab..a8e060c53c 100644 --- a/library/cpp/protobuf/json/json_writer_output.cpp +++ b/library/cpp/protobuf/json/json_writer_output.cpp @@ -8,6 +8,9 @@ namespace NProtobufJson { jsonConfig.ValidateUtf8 = false; jsonConfig.DontEscapeStrings = false; jsonConfig.WriteNanAsString = config.WriteNanAsString; + jsonConfig.DoubleNDigits = config.DoubleNDigits; + jsonConfig.FloatNDigits = config.FloatNDigits; + jsonConfig.FloatToStringMode = config.FloatToStringMode; for (size_t i = 0; i < config.StringTransforms.size(); ++i) { Y_ASSERT(config.StringTransforms[i]); diff --git a/library/cpp/protobuf/json/ut/proto2json_ut.cpp b/library/cpp/protobuf/json/ut/proto2json_ut.cpp index 3f154645a1..bf8b4994f2 100644 --- a/library/cpp/protobuf/json/ut/proto2json_ut.cpp +++ b/library/cpp/protobuf/json/ut/proto2json_ut.cpp @@ -1053,4 +1053,30 @@ Y_UNIT_TEST(TestSimplifiedTimestamp) { UNIT_ASSERT_EQUAL_C(json, "{\"Timestamp\":\"1970-04-26T17:46:40.000000504Z\"}", "real value is " << json); } // TestSimplifiedTimestamp +Y_UNIT_TEST(TestFloatToString) { +#define TEST_SINGLE(mode, value, expectedValue) \ + do { \ + TFlatOptional proto; \ + proto.SetFloat(value); \ + \ + TStringStream jsonStr; \ + TProto2JsonConfig config; \ + config.SetFloatNDigits(3).SetFloatToStringMode(mode); \ + UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config)); \ + TString expectedStr = TStringBuilder() << "{\"Float\":" << expectedValue << "}"; \ + UNIT_ASSERT_EQUAL_C(jsonStr.Str(), expectedStr, "real value is " << jsonStr.Str()); \ + } while (false) + + TEST_SINGLE(EFloatToStringMode::PREC_NDIGITS, 1234.18345, "1.23e+03"); + TEST_SINGLE(EFloatToStringMode::PREC_NDIGITS, 12.18345, "12.2"); + TEST_SINGLE(EFloatToStringMode::PREC_POINT_DIGITS, 12345.18355, "12345.184"); + TEST_SINGLE(EFloatToStringMode::PREC_POINT_DIGITS, 12.18355, "12.184"); + TEST_SINGLE(EFloatToStringMode::PREC_POINT_DIGITS, 12.18, "12.180"); + TEST_SINGLE(EFloatToStringMode::PREC_POINT_DIGITS_STRIP_ZEROES, 12345.18355, "12345.184"); + TEST_SINGLE(EFloatToStringMode::PREC_POINT_DIGITS_STRIP_ZEROES, 12.18355, "12.184"); + TEST_SINGLE(EFloatToStringMode::PREC_POINT_DIGITS_STRIP_ZEROES, 12.18, "12.18"); + +#undef TEST_SINGLE +} // TestFloatToString + } // TProto2JsonTest |