Add facilities for sanitising strings for use as JSON values
The present list of supported string(JSON ...)
functions are great, except for the fact that they provide no facilities for JSON value creation when inserting string values with string(JSON ... SET ...)
.
For example, take the following snippet...
set(my-invalid-value [[my \"invalid" value]])
string(JSON json SET "{}" "key" "\"${my-invalid-value}\"")
message(FATAL_ERROR "${json}")
Where you might expect the following:
{
"key" : "my \\\"invalid\" value"
}
You are today instead greeted with:
{
"key" : "my \"invalid"
}
There's not a great way around this today, short of manually replacing escapable characters. It would be helpful if CMake provided a way to escape our strings according to section 9 of the JSON specification. For example:
# string(JSON <out-var> TO_VALUE <string>)
string(JSON value TO_VALUE [[my \"invalid" value]])
message(FATAL_ERROR "${value}") # "my \\\"invalid\" value"
Or, even better, if string(JSON ... SET ...)
had a flag to accept raw strings and sanitise them automatically:
string(JSON json SET SANITIZED "{}" "key" [[my \"invalid" value]])