go icon indicating copy to clipboard operation
go copied to clipboard

Auto lowercase support

Open helphi opened this issue 4 years ago • 1 comments

Could json-iterator support auto lowercase json field?

code example:

type User struct {
	ID       int `json:"-"`
	Name     string
	Age      int
	Email    string
	QQNumber string `json:"QQ"`
}

user := User{
	ID:       1,
	Name:     "MJ",
	Age:      51,
	Email:    "[email protected]",
	QQNumber: "20090625",
}

b, _ := jsoniter.Marshal(user)
fmt.Println(string(b))

// we need a function like `MarshalSmart`, it can auto lowercase the first letter of json fileds if the struct field has no json tag defined.
b, _ := jsoniter.MarshalSmart(user)
fmt.Println(string(b))

prefer output:


// output of jsoniter.Marshal

{
	"Name": "MJ",
	"Age": 51,
	"Email": "[email protected]",
	"QQ": "20090625"
}

// output of jsoniter.MarshalSmart

{
	"name": "MJ",
	"age": 51,
	"email": "[email protected]",
	"QQ": "20090625"
}

the same as jsoniter.UnMarshalSmart

helphi avatar Dec 15 '21 13:12 helphi

It is supported by the extension. see extra.naming_strategy.go

zhenzou avatar Dec 28 '21 04:12 zhenzou