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

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

Closed
meilihao opened this issue Jul 27, 2018 · 5 comments
Closed

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

meilihao opened this issue Jul 27, 2018 · 5 comments

Comments

@meilihao
Copy link

@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"}
@ghost
Copy link

@ghost ghost commented Jul 27, 2018

how do we keep the original '-' meanings?

@mvdan
Copy link
Member

@mvdan 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 json: add tag to support one-way operation for marshal/unmarshal encoding-json: add tag to support one-way operation for marshal/unmarshal Jul 27, 2018
@mvdan mvdan changed the title encoding-json: add tag to support one-way operation for marshal/unmarshal encoding/json: add tag to support one-way operation for marshal/unmarshal Jul 27, 2018
@meilihao
Copy link
Author

@meilihao 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
Copy link
Member

@mvdan 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.

@smyrman
Copy link

@smyrman smyrman commented Oct 29, 2020

Alternative syntax choices

Another syntax to achieve this, could be to introduce two new struct-tag prefixes, e.g."jsonMarshal" and "jsonUnmarshal". I.e.:

type User struct {
	Name     string `json:"name"`
	Password string `jsonMarshal:"password" jsonUnmarshal:"-"``
}

This even allows using different values for encode a decode, when desirable.

Yet another alternative, would be to allow ,nomarshal and ,nounmarshal keywords (or some similar wording):

type User struct {
	Name     string `json:"name"`
	Password string `json:"password,nounmarshal"`
}

This is less flexible then multiple struct tags, but faster and easier to write, and more in line with current struct-tags like ,omitempty (which is already already affecting only marshal?).

Use-case

With either method, this would be even more useful when using embedded structs as a mechanism for including fields into models on the client side of an API where the server has defined read-only fields. In particular so, because custom marshal / unmarshal methods for structs used as embedded fields, does not have the desired effect.

Example:

package modelprofile

type Model struct {
	ID   string `json:"id,nomarshal"`
	ETag string `json:"_etag,nomarshal"`
}

type WriteInfo struct {
	CreatedBy string     `json:"createdBy,nomarshal"`
	UpdatedBy string     `json:"updatedBy,nomarshal"`
	CreatedAt *time.Time `json:"createdAt,nomarshal"`
	UpdatedAt *time.Time `json:"updatedAt,nomarshal"`
}
type User struct {
	modelprofile.Model
	modelprofile.WriteInfo

 	Name string      `json:"name"`
 	Password string `json:"password,nounmarshal"`
}
@meilihao meilihao closed this Nov 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants