Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] case sensitive unmarshal #690

Closed
ayanamist opened this issue Aug 19, 2024 · 3 comments · Fixed by #709
Closed

[Feature Request] case sensitive unmarshal #690

ayanamist opened this issue Aug 19, 2024 · 3 comments · Fixed by #709

Comments

@ayanamist
Copy link

ayanamist commented Aug 19, 2024

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
Copy link
Collaborator

AsterDY commented Aug 20, 2024

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

@AsterDY
Copy link
Collaborator

AsterDY commented Jan 7, 2025

Can u try #709 first? @ayanamist

@ayanamist
Copy link
Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants