[Feature Request] case sensitive unmarshal
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
Seems possible. But now sonic is under refactoring. Maybe in our next middle version
Can u try #709 first? @ayanamist
Good job, i see you have already added some unit tests for this feature which is identical to my test sample.
I test github.com/bytedance/sonic v1.12.5-0.20250103165927-9998182d92ad which is your branch in my project and all unit tests pass!
@ayanamist How did you replace sigs.k8s.io/json? I'm also looking to use Sonic to optimize this library. I built a Kubernetes gateway that requires frequent serialization and deserialization of Kubernetes objects.
@ezxfv Just rewrite all related codes. It's not a drop-in replacement.