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

encoding/json: add tag to support one-way operation for marshal/unmarshal #26636

Open
meilihao opened this Issue Jul 27, 2018 · 4 comments

Comments

Projects
None yet
4 participants
@meilihao

meilihao commented Jul 27, 2018

What did you do?

Now if the field tag is "-", the field is always omitted. sometime, we need one-way operation for marshal/unmarshal.

What did you expect to see?

add <- for unmarshal, omit marshal; add -> for marshal, omit unmarshal.

tag "<-"

{
    "name":"joy",
    "password":"123456"
}
type User struct {
    Name string `json:"name"`
    Password string `json:"password,<-"`
}
u := new(User)

json.Unmarshal([]byte(raw), u)
fmt.Println(u)

data, _ := json.Marshal(u) // omit filed "Password"
fmt.Println(string(data))

output:

&{joy 123456}
{"name":"joy"}

tag "->"

{
    "name":"joy",
    "password":"123456"
}
type User struct {
    Name string `json:"name"`
    Password string `json:"password,->"`
}
u := new(User)

json.Unmarshal([]byte(raw), u) // omit filed "Password"
fmt.Println(u)

u.Password = "123456"
data, _ := json.Marshal(u)
fmt.Println(string(data))

output:

&{joy}
{"name":"joy", "password":"123456"}
@ctriple

This comment has been minimized.

Contributor

ctriple commented Jul 27, 2018

how do we keep the original '-' meanings?

@mvdan

This comment has been minimized.

Member

mvdan commented Jul 27, 2018

This is a substantial change to the json package and should be a proposal: https://github.com/golang/proposal

Having said that, a couple of ways to do this without adding features come to mind:

  • Have multiple versions of the same struct for marshaling and unmarshalilng, with different json field tags. Converting values between these is permitted. Will require some boilerplate if the fields in question are nested, though.
  • Manually ignore the fields. If one wants to ignore the field when marshaling, set it as omitempty and temporarily zero it. If one wants to ignore the field when unmarshaling, reset the field to its previous value.

@mvdan mvdan changed the title from json: add tag to support one-way operation for marshal/unmarshal to encoding-json: add tag to support one-way operation for marshal/unmarshal Jul 27, 2018

@mvdan mvdan changed the title from encoding-json: add tag to support one-way operation for marshal/unmarshal to encoding/json: add tag to support one-way operation for marshal/unmarshal Jul 27, 2018

@meilihao

This comment has been minimized.

meilihao commented Jul 30, 2018

@ctriple I think - is stay the same as before.

@mvdan :

  1. Have multiple versions of the same struct for marshaling and unmarshalilng, with different json field tags is tedious.
  2. Manually ignore the fields is tedious too.
@mvdan

This comment has been minimized.

Member

mvdan commented Jul 30, 2018

Most of the changes that would make the language more complex are to avoid some degree of code verbosity, so that's not a point that stands on its own. The advantages of this proposal must outweigh the disadvantages. To do that, this issue should be a proposal, like I suggested above.

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