sonic icon indicating copy to clipboard operation
sonic copied to clipboard

[Feature Request] case sensitive unmarshal

Open ayanamist opened this issue 1 year ago • 1 comments

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

ayanamist avatar Aug 19 '24 09:08 ayanamist

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

AsterDY avatar Aug 20 '24 03:08 AsterDY

Can u try #709 first? @ayanamist

AsterDY avatar Jan 07 '25 09:01 AsterDY

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 avatar Jan 07 '25 09:01 ayanamist

@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 avatar Mar 27 '25 07:03 ezxfv

@ezxfv Just rewrite all related codes. It's not a drop-in replacement.

ayanamist avatar Mar 27 '25 07:03 ayanamist