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

Open
ayanamist opened this issue Aug 19, 2024 · 1 comment
Open

[Feature Request] case sensitive unmarshal #690

ayanamist opened this issue Aug 19, 2024 · 1 comment

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

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

No branches or pull requests

2 participants