# HG changeset patch # User Sebastien Jodogne # Date 1608623963 -3600 # Node ID 3aeb5171fbd454cf33c34e3df83b840434d4a729 # Parent d07a65100a953702a71385c9ed48689f400e88db new function Toolbox::ReadJsonWithoutComments() diff -r d07a65100a95 -r 3aeb5171fbd4 OrthancFramework/Sources/Toolbox.cpp --- 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(buffer), - reinterpret_cast(buffer) + size, target); + reinterpret_cast(buffer) + size, target, collectComments); #else Json::CharReaderBuilder builder; + builder.settings_["collectComments"] = collectComments; + const std::unique_ptr reader(builder.newCharReader()); assert(reader.get() != NULL); + JSONCPP_STRING err; if (reader->parse(reinterpret_cast(buffer), reinterpret_cast(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, diff -r d07a65100a95 -r 3aeb5171fbd4 OrthancFramework/Sources/Toolbox.h --- 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); diff -r d07a65100a95 -r 3aeb5171fbd4 OrthancFramework/UnitTestsSources/ToolboxTests.cpp --- 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; diff -r d07a65100a95 -r 3aeb5171fbd4 OrthancServer/Plugins/Samples/Common/OrthancPluginCppWrapper.cpp --- 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()); }