bytedance / sonic

A blazingly fast JSON serializing & deserializing library
Apache License 2.0
6.73k stars 333 forks source link

[Feature Request] case sensitive unmarshal #690

Open ayanamist opened 3 weeks ago

ayanamist commented 3 weeks ago

We are migrating our application to sonic which is super fast, and encounter an problem on some scene. Some of our code using k8s.io/apimachinery/pkg/util/json which backed by sigs.k8s.io/json providing some enhancements like CaseSensitive and PreserveInts. PreserveInts can be replaced by sonic.Config.UseInt64=true However CaseSensitive has no suitable solution, and i have verified that sonic is case-insensitive. Can this be implemented?


Test sample

func TestSonic(t *testing.T) {
    t.Run("sonic", func(t *testing.T) {
        config := sonic.Config{
            EscapeHTML : true,
            SortMapKeys: true,
            CompactMarshaler: true,
            CopyString : true,
            ValidateString : true,
            UseInt64: true,
        }.Froze()
        var u2 struct {
            A int64
            B float64 `json:"b"`
        }
        if err := config.UnmarshalFromString(`{"a":2,"b":1.1}`, &u2); err != nil {
            t.Fatalf("failed to unmarshal: %v", err)
        } else {
            t.Logf("unmarshal result: %#v", u2)
        }
    })
    t.Run("kjson", func(t *testing.T) {
        var u2 struct {
            A int64
            B float64 `json:"b"`
        }
        if err := json.Unmarshal([]byte(`{"a":2,"b":1.1}`), &u2); err != nil {
            t.Fatalf("failed to unmarshal: %v", err)
        } else {
            t.Logf("unmarshal result: %#v", u2)
        }
    })
}
=== RUN   TestSonic
=== RUN   TestSonic/sonic
    utils_test.go:191: unmarshal result: struct { A int64; B float64 "json:\"b\"" }{A:2, B:1.1}
--- PASS: TestSonic/sonic (0.00s)
=== RUN   TestSonic/kjson
    utils_test.go:209: unmarshal result: struct { A int64; B float64 "json:\"b\"" }{A:0, B:1.1}
--- PASS: TestSonic/kjson (0.00s)
--- PASS: TestSonic (0.00s)
PASS
AsterDY commented 3 weeks ago

Seems possible. But now sonic is under refactoring. Maybe in our next middle version