From fa2712d89b1cb6451946a52bf055f6bb19b43b4f Mon Sep 17 00:00:00 2001 From: Tianyi Chen Date: Fri, 7 Jun 2024 22:25:20 -0700 Subject: [PATCH] wide_string_input_adapter fallback to get_character --- include/nlohmann/detail/input/input_adapters.hpp | 12 ++++++++---- single_include/nlohmann/json.hpp | 10 +++++++--- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/include/nlohmann/detail/input/input_adapters.hpp b/include/nlohmann/detail/input/input_adapters.hpp index d19425f3a4..a68d7019ca 100644 --- a/include/nlohmann/detail/input/input_adapters.hpp +++ b/include/nlohmann/detail/input/input_adapters.hpp @@ -136,7 +136,7 @@ class input_stream_adapter template std::size_t get_elements(T* dest, std::size_t count = 1) { - auto res = sb->sgetn(reinterpret_cast(dest), count * sizeof(T)); + auto res = sb->sgetn(reinterpret_cast(dest), static_cast(count * sizeof(T))); if (JSON_HEDLEY_UNLIKELY(res < count * sizeof(T))) { is->clear(is->rdstate() | std::ios::eofbit); @@ -359,10 +359,14 @@ class wide_string_input_adapter } template - std::size_t get_elements(T*, std::size_t = 1) + std::size_t get_elements(T* dest, std::size_t count = 1) { - JSON_THROW(other_error::create(500, "Unexpected get_elements call to wchar input adapter", nullptr)); - return 0; + auto ptr = reinterpret_cast(dest); + for (std::size_t read_index = 0; read_index < count * sizeof(T); ++read_index) + { + ptr[read_index] = get_character(); + } + return count * sizeof(T); } private: diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index dfa1d30056..e9890ea0e9 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -6281,7 +6281,7 @@ class input_stream_adapter template std::size_t get_elements(T* dest, std::size_t count = 1) { - auto res = sb->sgetn(reinterpret_cast(dest), count * sizeof(T)); + auto res = sb->sgetn(reinterpret_cast(dest), static_cast(count * sizeof(T))); if (JSON_HEDLEY_UNLIKELY(res < count * sizeof(T))) { is->clear(is->rdstate() | std::ios::eofbit); @@ -6506,8 +6506,12 @@ class wide_string_input_adapter template std::size_t get_elements(T* dest, std::size_t count = 1) { - JSON_THROW(other_error::create(500, "Unexpected get_elements call to wchar input adapter", nullptr)); - return 0; + auto ptr = reinterpret_cast(dest); + for (std::size_t read_index = 0; read_index < count * sizeof(T); ++read_index) + { + ptr[read_index] = get_character(); + } + return count * sizeof(T); } private: