-
-
Notifications
You must be signed in to change notification settings - Fork 139
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
Marshal with case preference #271
Comments
For 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, _ := json.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, _ := json.MarshalSmart(user)
fmt.Println(string(b)) prefer output: // output of json.Marshal
{
"Name": "MJ",
"Age": 51,
"Email": "mj@qq.com",
"QQ": "20090625"
}
// output of json.MarshalSmart
{
"name": "MJ",
"age": 51,
"email": "mj@qq.com",
"QQ": "20090625"
} the same as |
I did some job for this, https://github.com/xgomod/json |
I am concerned from a performance point of view that this option affects the runtime code path. (e.g. #371 ) |
Yeah that's understandable. It's a hard feature to get right for every use case and I don't have any ideas about how you might implement it. If others don't need this then we may as well close this issue. |
This feature is very important because our general project specification is to output JSON using camelCase. It is tedious and meaningless to add tag Similarly, as a comparison, Rust's serde library is very user-friendly: #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")] // This attr directly solves the critical problem
pub struct Model {
pub id: String,
pub name: String,
// ...
} |
really looking forward this feature! |
Would it be possible to create an option for the encoder to be able to specify the case style the struct fields are marshalled with?
For example, if we have
the output will be
but if we could specify an option to convert to say camelcase (even something as simple as lowercase the first letter) to get
it would be much nicer than manually specifying json tags! Not sure how it would look in terms of performance. Obviously a general solution is a callback/transformation function of some kind, or could just support camelCase as that would be fast.
The text was updated successfully, but these errors were encountered: