mirror of
https://github.com/google/go-jsonnet.git
synced 2025-08-08 07:17:12 +02:00
fix(stdlib): 🐛 manifestJsonEx was escaping HTML
minimal repo: https://play.golang.org/p/GQBNMe6k_nm https://stackoverflow.com/questions/28595664/how-to-stop-json-marshal-from-escaping-and\nminimal fix proof: https://play.golang.org/p/SJM3KLkYW- resolves #423
This commit is contained in:
parent
3563a21847
commit
d767ab7bf5
20
builtins.go
20
builtins.go
@ -1163,6 +1163,18 @@ func builtinParseJSON(i *interpreter, trace traceElement, str value) (value, err
|
||||
return jsonToValue(i, trace, parsedJSON)
|
||||
}
|
||||
|
||||
func jsonEncode(v interface{}) (string, error) {
|
||||
buf := new(bytes.Buffer)
|
||||
enc := json.NewEncoder(buf)
|
||||
enc.SetEscapeHTML(false)
|
||||
err := enc.Encode(v)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return strings.TrimRight(buf.String(), "\n"), nil
|
||||
}
|
||||
|
||||
// We have a very similar logic here /interpreter.go@v0.16.0#L695 and here: /interpreter.go@v0.16.0#L627
|
||||
// These should ideally be unified
|
||||
// For backwards compatibility reasons, we are manually marshalling to json so we can control formatting
|
||||
@ -1188,11 +1200,11 @@ func builtinManifestJSONEx(i *interpreter, trace traceElement, obj, indent value
|
||||
case *valueNull:
|
||||
return "null", nil
|
||||
case valueString:
|
||||
b, err := json.Marshal(v.getGoString())
|
||||
jStr, err := jsonEncode(v.getGoString())
|
||||
if err != nil {
|
||||
return "", i.Error(fmt.Sprintf("failed to marshal valueString to JSON: %v", err.Error()), trace)
|
||||
}
|
||||
return string(b), nil
|
||||
return jStr, nil
|
||||
case *valueNumber:
|
||||
return strconv.FormatFloat(v.value, 'f', -1, 64), nil
|
||||
case *valueBoolean:
|
||||
@ -1233,11 +1245,11 @@ func builtinManifestJSONEx(i *interpreter, trace traceElement, obj, indent value
|
||||
return "", err
|
||||
}
|
||||
|
||||
fieldNameMarshalled, err := json.Marshal(fieldName)
|
||||
fieldNameMarshalled, err := jsonEncode(fieldName)
|
||||
if err != nil {
|
||||
return "", i.Error(fmt.Sprintf("failed to marshal object fieldname to JSON: %v", err.Error()), trace)
|
||||
}
|
||||
|
||||
|
||||
newPath := append(path, fieldName)
|
||||
mvs, err := aux(fieldValue, newPath, newIndent)
|
||||
if err != nil {
|
||||
|
1
testdata/builtinManifestJsonEx.golden
vendored
Normal file
1
testdata/builtinManifestJsonEx.golden
vendored
Normal file
@ -0,0 +1 @@
|
||||
"{\n \"bam\": true,\n \"bar\": \"bar\",\n \"baz\": 1,\n \"bazel\": 1.42,\n \"bim\": false,\n \"blamo\": {\n \"cereal\": [\n \"<>& fizbuzz\"\n ],\n \"treats\": [\n {\n \"name\": \"chocolate\"\n }\n ]\n },\n \"boom\": -1,\n \"foo\": \"bar\"\n}"
|
22
testdata/builtinManifestJsonEx.jsonnet
vendored
Normal file
22
testdata/builtinManifestJsonEx.jsonnet
vendored
Normal file
@ -0,0 +1,22 @@
|
||||
local a = {
|
||||
foo: "bar",
|
||||
bar: self.foo,
|
||||
baz: 1,
|
||||
bazel: 1.42,
|
||||
boom: -1,
|
||||
bim: false,
|
||||
bam: true,
|
||||
blamo: {
|
||||
cereal: [
|
||||
"<>& fizbuzz",
|
||||
],
|
||||
|
||||
treats: [
|
||||
{
|
||||
name: "chocolate",
|
||||
}
|
||||
],
|
||||
}
|
||||
};
|
||||
|
||||
std.manifestJsonEx(a, " ")
|
Loading…
Reference in New Issue
Block a user