Skip to content
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

Open
jhwz opened this issue Aug 12, 2021 · 6 comments
Open

Marshal with case preference #271

jhwz opened this issue Aug 12, 2021 · 6 comments

Comments

@jhwz
Copy link

jhwz commented Aug 12, 2021

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

type mystruct struct {
   FieldOne string
   FieldTwo string
} 

the output will be

{
  "FieldOne" : "",
  "FieldTwo" : ""
}

but if we could specify an option to convert to say camelcase (even something as simple as lowercase the first letter) to get

{
  "fieldOne" : "",
  "fieldTwo" : ""
}

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.

@goccy goccy added enhancement New feature or request feature request and removed enhancement New feature or request labels Aug 12, 2021
@goccy goccy added the enhancement New feature or request label Nov 18, 2021
@helphi
Copy link

helphi commented Dec 16, 2021

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 json.UnMarshalSmart

@helphi
Copy link

helphi commented Dec 17, 2021

I did some job for this, https://github.com/xgomod/json

@goccy
Copy link
Owner

goccy commented Nov 14, 2022

I am concerned from a performance point of view that this option affects the runtime code path. (e.g. #371 )
Performance can get worse with each additional branch.

@goccy goccy removed the enhancement New feature or request label Nov 14, 2022
@jhwz
Copy link
Author

jhwz commented Nov 14, 2022

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.

@ilxqx
Copy link

ilxqx commented Nov 5, 2023

This feature is very important because our general project specification is to output JSON using camelCase. It is tedious and meaningless to add tag json: "camelCaseName" for each struct field.

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,
    // ...
}

@melochale
Copy link

really looking forward this feature!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants