From 4678b7b34a7150803377e02e1229dac9b783ab64 Mon Sep 17 00:00:00 2001 From: Tyler Yankee Date: Sat, 1 Aug 2026 11:43:09 -0400 Subject: [PATCH] string(JSON): Use cmJSONState to read input This approach will be more portable. --- Source/cmStringCommand.cxx | 17 +++++++---------- Tests/RunCMake/string/JSONBadJson-stderr.txt | 16 ++++++++++++---- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/Source/cmStringCommand.cxx b/Source/cmStringCommand.cxx index f67b074fe2..acda83e42d 100644 --- a/Source/cmStringCommand.cxx +++ b/Source/cmStringCommand.cxx @@ -10,7 +10,7 @@ #include #include #include -#include +#include #include #include @@ -19,12 +19,12 @@ #include #include -#include #include #include #include "cmCMakeString.hxx" #include "cmExecutionStatus.h" +#include "cmJSONState.h" #include "cmList.h" #include "cmMakefile.h" #include "cmMessageType.h" @@ -909,15 +909,12 @@ Json::Value& ResolvePath(Json::Value& json, Args path) Json::Value ReadJson(std::string const& jsonstr) { - Json::CharReaderBuilder builder; - builder["collectComments"] = false; - auto jsonReader = std::unique_ptr(builder.newCharReader()); + std::istringstream iss(jsonstr); Json::Value json; - std::string error; - if (!jsonReader->parse(jsonstr.data(), jsonstr.data() + jsonstr.size(), - &json, &error)) { - throw json_error( - cmStrCat("failed parsing json string:\n"_s, jsonstr, '\n', error)); + cmJSONState parseState(iss, &json, cmJSONState::StrictMode::Relaxed); + if (!parseState.errors.empty()) { + throw json_error(cmStrCat("failed parsing json string:\n"_s, + parseState.GetErrorMessage())); } return json; } diff --git a/Tests/RunCMake/string/JSONBadJson-stderr.txt b/Tests/RunCMake/string/JSONBadJson-stderr.txt index 812ef88494..c6052f5ffc 100644 --- a/Tests/RunCMake/string/JSONBadJson-stderr.txt +++ b/Tests/RunCMake/string/JSONBadJson-stderr.txt @@ -1,26 +1,34 @@ CMake Error at JSONBadJson\.cmake:1 \(string\): string sub-command JSON failed parsing json string: +( + Syntax error: value, object or array expected\. invalid - \* Line 1, Column 1 + \^\.| + JSON Parse Error: + \* Line 1, Column 1 Syntax error: value, object or array expected\. - \. + \.) Call Stack \(most recent call first\): CMakeLists\.txt:[0-9]+ \(include\) CMake Error at JSONBadJson\.cmake:2 \(string\): string sub-command JSON failed parsing json string: +( + Syntax error: value, object or array expected\. value - \* Line 1, Column 1 + \^\.| + JSON Parse Error: + \* Line 1, Column 1 Syntax error: value, object or array expected\. - \. + \.) Call Stack \(most recent call first\): CMakeLists\.txt:[0-9]+ \(include\)