Skip to content

it needs to distinguish upper/lower field names in a case #236

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

Closed
reedom opened this issue Feb 12, 2018 · 4 comments
Closed

it needs to distinguish upper/lower field names in a case #236

reedom opened this issue Feb 12, 2018 · 4 comments

Comments

@reedom
Copy link

reedom commented Feb 12, 2018

The current implementation seems that it does not care of field name cases(case insensitive) in design.
But since some public API require case sensitive decoder, this library needs to do so against them.
c.f. https://github.com/binance-exchange/binance-official-api-docs/blob/master/user-data-stream.md

test:

import (
	"encoding/json"

	"github.com/json-iterator/go"
)

type X struct {
	Upper bool `json:"M"`
	Lower bool `json:"m"`
}

func TestJSON(t *testing.T) {
	inOrder := []byte(`{"M":false,"m":true}`)
	swapped := []byte(`{"m":true,"M":false}`)

	var x X

	err := json.Unmarshal(inOrder, &x)
	if err != nil {
		panic(err)
	}
	test(t, "encoding/json, in order", x)

	err = json.Unmarshal(swapped, &x)
	if err != nil {
		panic(err)
	}
	test(t, "encoding/json, swapped", x)

	err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(inOrder, &x)
	if err != nil {
		panic(err)
	}
	test(t, "jsoniter, in order", x)

	err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(swapped, &x)
	if err != nil {
		panic(err)
	}
	test(t, "jsoniter, swapped", x)
}

func test(t *testing.T, label string, x X) {
	if x.Upper {
		t.Errorf("%s: x.Upper is not false", label)
		return
	}
	if !x.Lower {
		t.Errorf("%s: x.Lower is not true", label)
		return
	}
}

result:

--- FAIL: TestJSON (0.00s)
    json_test.go: jsoniter, swapped: x.Lower is not true
FAIL
@taowen
Copy link
Contributor

taowen commented Feb 12, 2018

the stdlib does not care the case, so we need to be compatible.

@pokehanai
Copy link

the stdlib does not care the case, so we need to be compatible.

The stdlib cares, at least Go 1.9 does while both of upper and lower cased fields exist in a struct.
Please look in the result of the above test.

@reedom
Copy link
Author

reedom commented Feb 12, 2018

The following test shows how the stdlib handles:

import (
	"encoding/json"
	"fmt"
)

type UpperOnly struct {
	Upper bool `json:"M"` // type: bool
}

type LowerOnly struct {
	Lower int `json:"m"`. // type: int
}

type Both struct {
	Upper bool `json:"M"`
	Lower int  `json:"m"`
}

func TestJSON(t *testing.T) {
	data := []byte(`{"m":3,"M":true}`)
	swap := []byte(`{"M":true,"m":3}`)

	var upperOnly UpperOnly
	if err := json.Unmarshal(data, &upperOnly); err != nil {
		t.Error("upperOnly, data:", err)
	}
	if err := json.Unmarshal(swap, &upperOnly); err != nil {
		t.Error("upperOnly, swap:", err)
	}

	var lowerOnly LowerOnly
	if err := json.Unmarshal(data, &lowerOnly); err != nil {
		t.Error("lowerOnly, data:", err)
	}
	if err := json.Unmarshal(swap, &lowerOnly); err != nil {
		t.Error("lowerOnly, swap:", err)
	}

	var both Both
	if err := json.Unmarshal(data, &both); err != nil {
		t.Error("both, data:", err)
	}
	if err := json.Unmarshal(swap, &both); err != nil {
		t.Error("both, swap:", err)
	}
	fmt.Printf("%#v\n", both)
}

result:

test.Both{Upper:true, Lower:3}
--- FAIL: TestJSON (0.00s)
    test.go: upperOnly, data: json: cannot unmarshal number into Go struct field UpperOnly.M of type bool
    test.go: upperOnly, swap: json: cannot unmarshal number into Go struct field UpperOnly.M of type bool
    test.go: lowerOnly, data: json: cannot unmarshal bool into Go struct field LowerOnly.m of type int
    test.go: lowerOnly, swap: json: cannot unmarshal bool into Go struct field LowerOnly.m of type int

@taowen taowen closed this as completed in 24bb2ee Feb 14, 2018
@pokehanai
Copy link

Now it works.
Great!

zhenzou pushed a commit to zhenzou/jsoniter that referenced this issue Feb 2, 2022
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

3 participants