json-iterator / go

A high-performance 100% compatible drop-in replacement of "encoding/json"
http://jsoniter.com/migrate-from-go-std.html
MIT License
13.34k stars 1.02k forks source link

Auto lowercase support #595

Open helphi opened 2 years ago

helphi commented 2 years ago

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:    "mj@qq.com",
    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": "mj@qq.com",
    "QQ": "20090625"
}

// output of jsoniter.MarshalSmart

{
    "name": "MJ",
    "age": 51,
    "email": "mj@qq.com",
    "QQ": "20090625"
}

the same as jsoniter.UnMarshalSmart

zhenzou commented 2 years ago

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