mirror of
https://github.com/hanzoai/metrics.git
synced 2026-10-30 20:03:39 +00:00
The %q formatter may result in incorrectly formatted JSON string if the original string
contains special chars such as \x1b . They must be encoded as \u001b , otherwise the resulting JSON string
cannot be parsed by JSON parsers.
This is a follow-up for 9bec706860
See https://github.com/VictoriaMetrics/victorialogs-datasource/issues/24
21 lines
369 B
Go
21 lines
369 B
Go
package stringsutil
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestJSONString(t *testing.T) {
|
|
f := func(s, resultExpected string) {
|
|
t.Helper()
|
|
|
|
result := JSONString(s)
|
|
if result != resultExpected {
|
|
t.Fatalf("unexpected result\ngot\n%s\nwant\n%s", result, resultExpected)
|
|
}
|
|
}
|
|
|
|
f(``, `""`)
|
|
f(`foo`, `"foo"`)
|
|
f("\n\b\f\t\"acЫВА'\\", `"\n\b\f\t\"acЫВА\u0027\\"`)
|
|
}
|