diff --git a/gjson.go b/gjson.go index 360594d..66965ba 100644 --- a/gjson.go +++ b/gjson.go @@ -1950,7 +1950,6 @@ func Get(json, path string) Result { } } } - var i int var c = &parseContext{json: json} if len(path) >= 2 && path[0] == '.' && path[1] == '.' { @@ -2624,6 +2623,26 @@ func ModifierExists(name string, fn func(json, arg string) string) bool { return ok } +// cleanWS remove any non-whitespace from string +func cleanWS(s string) string { + for i := 0; i < len(s); i++ { + switch s[i] { + case ' ', '\t', '\n', '\r': + continue + default: + var s2 []byte + for i := 0; i < len(s); i++ { + switch s[i] { + case ' ', '\t', '\n', '\r': + s2 = append(s2, s[i]) + } + } + return string(s2) + } + } + return s +} + // @pretty modifier makes the json look nice. func modPretty(json, arg string) string { if len(arg) > 0 { @@ -2633,9 +2652,9 @@ func modPretty(json, arg string) string { case "sortKeys": opts.SortKeys = value.Bool() case "indent": - opts.Indent = value.String() + opts.Indent = cleanWS(value.String()) case "prefix": - opts.Prefix = value.String() + opts.Prefix = cleanWS(value.String()) case "width": opts.Width = int(value.Int()) } diff --git a/gjson_test.go b/gjson_test.go index 38e2db1..e3bfd84 100644 --- a/gjson_test.go +++ b/gjson_test.go @@ -2179,8 +2179,8 @@ func TestJoin152(t *testing.T) { assert(t, res.Raw == `[92.0,92.0,91.0,91.0,67.0]`) } -func TestIssue192(t *testing.T) { - assert(t, squash(`"000"hello`) == `"000"`) +func TestVariousFuzz(t *testing.T) { + // Issue #192 assert(t, squash(`"000"hello`) == `"000"`) assert(t, squash(`"000"`) == `"000"`) assert(t, squash(`"000`) == `"000`) assert(t, squash(`"`) == `"`) @@ -2193,15 +2193,14 @@ func TestIssue192(t *testing.T) { testJSON := `0.#[[{}]].@valid:"000` Get(testJSON, testJSON) -} -func TestIssue195(t *testing.T) { - testJSON := `\************************************` + + // Issue #195 + testJSON = `\************************************` + `**********{**",**,,**,**,**,**,"",**,**,**,**,**,**,**,**,**,**]` Get(testJSON, testJSON) -} -func TestIssue196(t *testing.T) { - testJSON := `[#.@pretty.@join:{""[]""preserve"3,"][{]]]` + // Issue #196 + testJSON = `[#.@pretty.@join:{""[]""preserve"3,"][{]]]` Get(testJSON, testJSON) + }