Mercurial > hg > orthanc
changeset 4397:3aeb5171fbd4
new function Toolbox::ReadJsonWithoutComments()
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 22 Dec 2020 08:59:23 +0100 |
parents | d07a65100a95 |
children | 38c22715bb56 |
files | OrthancFramework/Sources/Toolbox.cpp OrthancFramework/Sources/Toolbox.h OrthancFramework/UnitTestsSources/ToolboxTests.cpp OrthancServer/Plugins/Samples/Common/OrthancPluginCppWrapper.cpp |
diffstat | 4 files changed, 61 insertions(+), 22 deletions(-) [+] |
line wrap: on
line diff
--- a/OrthancFramework/Sources/Toolbox.cpp Tue Dec 22 08:16:08 2020 +0100 +++ b/OrthancFramework/Sources/Toolbox.cpp Tue Dec 22 08:59:23 2020 +0100 @@ -2297,30 +2297,22 @@ } - bool Toolbox::ReadJson(Json::Value& target, - const std::string& source) - { -#if JSONCPP_USE_DEPRECATED == 1 - Json::Reader reader; - return reader.parse(source, target); -#else - return ReadJson(target, source.c_str(), source.size()); -#endif - } - - - bool Toolbox::ReadJson(Json::Value& target, - const void* buffer, - size_t size) + static bool ReadJsonInternal(Json::Value& target, + const void* buffer, + size_t size, + bool collectComments) { #if JSONCPP_USE_DEPRECATED == 1 Json::Reader reader; return reader.parse(reinterpret_cast<const char*>(buffer), - reinterpret_cast<const char*>(buffer) + size, target); + reinterpret_cast<const char*>(buffer) + size, target, collectComments); #else Json::CharReaderBuilder builder; + builder.settings_["collectComments"] = collectComments; + const std::unique_ptr<Json::CharReader> reader(builder.newCharReader()); assert(reader.get() != NULL); + JSONCPP_STRING err; if (reader->parse(reinterpret_cast<const char*>(buffer), reinterpret_cast<const char*>(buffer) + size, &target, &err)) @@ -2334,6 +2326,36 @@ } #endif } + + + bool Toolbox::ReadJson(Json::Value& target, + const std::string& source) + { + return ReadJson(target, source.empty() ? NULL : source.c_str(), source.size()); + } + + + bool Toolbox::ReadJson(Json::Value& target, + const void* buffer, + size_t size) + { + return ReadJsonInternal(target, buffer, size, true); + } + + + bool Toolbox::ReadJsonWithoutComments(Json::Value& target, + const std::string& source) + { + return ReadJsonWithoutComments(target, source.empty() ? NULL : source.c_str(), source.size()); + } + + + bool Toolbox::ReadJsonWithoutComments(Json::Value& target, + const void* buffer, + size_t size) + { + return ReadJsonInternal(target, buffer, size, false); + } void Toolbox::WriteFastJson(std::string& target,
--- a/OrthancFramework/Sources/Toolbox.h Tue Dec 22 08:16:08 2020 +0100 +++ b/OrthancFramework/Sources/Toolbox.h Tue Dec 22 08:59:23 2020 +0100 @@ -268,6 +268,13 @@ const void* buffer, size_t size); + static bool ReadJsonWithoutComments(Json::Value& target, + const std::string& source); + + static bool ReadJsonWithoutComments(Json::Value& target, + const void* buffer, + size_t size); + static void WriteFastJson(std::string& target, const Json::Value& source);
--- a/OrthancFramework/UnitTestsSources/ToolboxTests.cpp Tue Dec 22 08:16:08 2020 +0100 +++ b/OrthancFramework/UnitTestsSources/ToolboxTests.cpp Tue Dec 22 08:59:23 2020 +0100 @@ -64,6 +64,21 @@ ASSERT_EQ(Toolbox::StripSpaces(f), "{\n \"hello\" : \"world\"\n}"); } +TEST(Toolbox, JsonComments) +{ + std::string a = "/* a */ { /* b */ \"hello\" : /* c */ \"world\" /* d */ } // e"; + + Json::Value b; + ASSERT_TRUE(Toolbox::ReadJsonWithoutComments(b, a)); + + std::string c; + Toolbox::WriteFastJson(c, b); + ASSERT_EQ(Toolbox::StripSpaces(c), "{\"hello\":\"world\"}"); + + Toolbox::WriteStyledJson(c, b); + ASSERT_EQ(Toolbox::StripSpaces(c), "{\n \"hello\" : \"world\"\n}"); +} + TEST(Toolbox, Base64_allByteValues) { std::string toEncode;
--- a/OrthancServer/Plugins/Samples/Common/OrthancPluginCppWrapper.cpp Tue Dec 22 08:16:08 2020 +0100 +++ b/OrthancServer/Plugins/Samples/Common/OrthancPluginCppWrapper.cpp Tue Dec 22 08:59:23 2020 +0100 @@ -316,12 +316,7 @@ bool ReadJson(Json::Value& target, const std::string& source) { -#if JSONCPP_USE_DEPRECATED == 1 - Json::Reader reader; - return reader.parse(source, target); -#else - return ReadJson(target, source.c_str(), source.size()); -#endif + return ReadJson(target, source.empty() ? NULL : source.c_str(), source.size()); }