diff --git a/json/json.go b/json/json.go index 0707f0a..172099c 100644 --- a/json/json.go +++ b/json/json.go @@ -36,6 +36,7 @@ func init() { _value = Any(_null, _true, _false, _string, _number, _array, _object) } -func unmarshal(input string) (interface{}, error) { +// Unmarshall json string into map[string]interface{} or []interface{} +func Unmarshal(input string) (interface{}, error) { return Run(_value, input, ASCIIWhitespace) } diff --git a/json/json_test.go b/json/json_test.go index 0ff3833..f020a40 100644 --- a/json/json_test.go +++ b/json/json_test.go @@ -11,31 +11,31 @@ import ( func TestUnmarshal(t *testing.T) { t.Run("basic types", func(t *testing.T) { - result, err := unmarshal(`true`) + result, err := Unmarshal(`true`) require.NoError(t, err) require.Equal(t, true, result) - result, err = unmarshal(`false`) + result, err = Unmarshal(`false`) require.NoError(t, err) require.Equal(t, false, result) - result, err = unmarshal(`null`) + result, err = Unmarshal(`null`) require.NoError(t, err) require.Equal(t, nil, result) - result, err = unmarshal(`"true"`) + result, err = Unmarshal(`"true"`) require.NoError(t, err) require.Equal(t, "true", result) }) t.Run("array", func(t *testing.T) { - result, err := unmarshal(`[true, null, false, -1.23e+4]`) + result, err := Unmarshal(`[true, null, false, -1.23e+4]`) require.NoError(t, err) require.Equal(t, []interface{}{true, nil, false, -1.23e+4}, result) }) t.Run("object", func(t *testing.T) { - result, err := unmarshal(`{"true":true, "false":false, "null": null, "number": 404} `) + result, err := Unmarshal(`{"true":true, "false":false, "null": null, "number": 404} `) require.NoError(t, err) require.Equal(t, map[string]interface{}{"true": true, "false": false, "null": nil, "number": int64(404)}, result) }) @@ -54,7 +54,7 @@ func BenchmarkUnmarshalParsec(b *testing.B) { func BenchmarkUnmarshalParsify(b *testing.B) { for i := 0; i < b.N; i++ { - _, err := unmarshal(benchmarkString) + _, err := Unmarshal(benchmarkString) require.NoError(b, err) } goparsify.DumpDebugStats() diff --git a/json/profile/json.go b/json/profile/json.go index e6ae771..b7d9733 100644 --- a/json/profile/json.go +++ b/json/profile/json.go @@ -48,7 +48,7 @@ func main() { } for i := 0; i < max; i++ { - _, err := json.unmarshal(benchmarkString) + _, err := json.Unmarshal(benchmarkString) if err != nil { panic(err) }