diff options
author | d-dima <d-dima@yandex-team.com> | 2024-08-05 13:19:22 +0300 |
---|---|---|
committer | d-dima <d-dima@yandex-team.com> | 2024-08-05 13:29:58 +0300 |
commit | acf04c75cb52f804ce76b16c356611268be3daf3 (patch) | |
tree | 73df11988fb2ba789ad9f71bd26ff3da92ca3e1e /library/cpp | |
parent | 8ead472243985333d15c527eb0d998468dc716e1 (diff) | |
download | ydb-acf04c75cb52f804ce76b16c356611268be3daf3.tar.gz |
Allow to read Nan/Inf values from JSON
Allow to read Nan/Inf values from JSON
9fbb79a21d81cf178abc89cd11d7ac75a9a4aaed
Diffstat (limited to 'library/cpp')
-rw-r--r-- | library/cpp/json/json_reader.cpp | 7 | ||||
-rw-r--r-- | library/cpp/json/json_reader.h | 3 | ||||
-rw-r--r-- | library/cpp/json/ut/json_reader_nan_ut.cpp | 29 | ||||
-rw-r--r-- | library/cpp/json/ut/ya.make | 1 |
4 files changed, 40 insertions, 0 deletions
diff --git a/library/cpp/json/json_reader.cpp b/library/cpp/json/json_reader.cpp index 0d048dfcd1..a1a9584481 100644 --- a/library/cpp/json/json_reader.cpp +++ b/library/cpp/json/json_reader.cpp @@ -336,6 +336,9 @@ namespace NJson { constexpr ui32 ConvertToRapidJsonFlags(ui8 flags) { ui32 rapidjsonFlags = rapidjson::kParseNoFlags; + if (flags & ReaderConfigFlags::NANINF) { + rapidjsonFlags |= rapidjson::kParseNanAndInfFlag; + } if (flags & ReaderConfigFlags::ITERATIVE) { rapidjsonFlags |= rapidjson::kParseIterativeFlag; } @@ -371,6 +374,7 @@ namespace NJson { ); \ } + TRY_EXTRACT_FLAG(ReaderConfigFlags::NANINF); TRY_EXTRACT_FLAG(ReaderConfigFlags::ITERATIVE); TRY_EXTRACT_FLAG(ReaderConfigFlags::COMMENTS); TRY_EXTRACT_FLAG(ReaderConfigFlags::VALIDATE); @@ -405,6 +409,9 @@ namespace NJson { if (config.AllowEscapedApostrophe) { flags |= ReaderConfigFlags::ESCAPE; } + if (config.AllowReadNanInf) { + flags |= ReaderConfigFlags::NANINF; + } return ReadWithRuntimeFlags(flags, reader, is, handler); } diff --git a/library/cpp/json/json_reader.h b/library/cpp/json/json_reader.h index 6c8e8c32e2..373a390085 100644 --- a/library/cpp/json/json_reader.h +++ b/library/cpp/json/json_reader.h @@ -20,6 +20,8 @@ namespace NJson { bool AllowComments = false; bool DontValidateUtf8 = false; bool AllowEscapedApostrophe = false; + // Allow to read Nan and Inf as integer values + bool AllowReadNanInf = false; ui64 MaxDepth = 0; @@ -48,6 +50,7 @@ namespace NJson { bool ReadJson(IInputStream* in, const TJsonReaderConfig* config, TJsonCallbacks* callbacks); enum ReaderConfigFlags { + NANINF = 0b10000, ITERATIVE = 0b1000, COMMENTS = 0b0100, VALIDATE = 0b0010, diff --git a/library/cpp/json/ut/json_reader_nan_ut.cpp b/library/cpp/json/ut/json_reader_nan_ut.cpp new file mode 100644 index 0000000000..fdd66249eb --- /dev/null +++ b/library/cpp/json/ut/json_reader_nan_ut.cpp @@ -0,0 +1,29 @@ +#include <library/cpp/json/json_reader.h> +#include <library/cpp/testing/unittest/registar.h> + +using namespace NJson; + +namespace { + +constexpr TStringBuf JSON_NAN_TEST = "{ \"Value1\": 0.0, \"Value2\": 1, \"Value3\": NaN }"; + +} + +Y_UNIT_TEST_SUITE(TJsonReaderNanTest) { + Y_UNIT_TEST(WithoutNanTest) { + TJsonReaderConfig cfg; + TJsonValue out; + // This read will fail + UNIT_ASSERT(!ReadJsonTree(JSON_NAN_TEST, &cfg, &out, /* throwOnError */ false)); + + } + Y_UNIT_TEST(WithNanTest) { + TJsonReaderConfig cfg; + cfg.AllowReadNanInf = true; + + TJsonValue out; + // This read will ok + UNIT_ASSERT(ReadJsonTree(JSON_NAN_TEST, &cfg, &out, /* throwOnError */ false)); + } +} + diff --git a/library/cpp/json/ut/ya.make b/library/cpp/json/ut/ya.make index 2ed6ccb9f0..89363ddd6a 100644 --- a/library/cpp/json/ut/ya.make +++ b/library/cpp/json/ut/ya.make @@ -8,6 +8,7 @@ PEERDIR( SRCS( json_reader_fast_ut.cpp json_reader_ut.cpp + json_reader_nan_ut.cpp json_prettifier_ut.cpp json_writer_ut.cpp json_saveload_ut.cpp |