Skip to content

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

Closed
@reedom

Description

@reedom

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions