string(JSON): Use cmJSONState to read input

This approach will be more portable.
This commit is contained in:
Tyler Yankee
2026-08-03 19:52:53 -04:00
parent 76026ce818
commit 4678b7b34a
2 changed files with 19 additions and 14 deletions
+7 -10
View File
@@ -10,7 +10,7 @@
#include <cstdlib>
#include <exception>
#include <limits>
#include <memory>
#include <sstream>
#include <stdexcept>
#include <utility>
@@ -19,12 +19,12 @@
#include <cm/string_view>
#include <cmext/string_view>
#include <cm3p/json/reader.h>
#include <cm3p/json/value.h>
#include <cm3p/json/writer.h>
#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<Json::CharReader>(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;
}
+12 -4
View File
@@ -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\)