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

proposal: encoding/json: add options to allow marshalling of ignored JSON fields #29426

Closed
JosefWN opened this issue Dec 26, 2018 · 3 comments
Closed

Comments

@JosefWN
Copy link

JosefWN commented Dec 26, 2018

Sometimes I find myself wanting to marshal and unmarshal ignored JSON fields (in the general case they should be ignored, but in certain cases they shouldn't). All ways I've come up with to get around this limitation feel kind of clunky, I try very hard to avoid reflection when I can. Feel free to correct me if there is a better solution out there already.

What version of Go are you using (go version)?

$ go version
1.11.4

Suggestion

It would be cool if we could embed structs and then cast them, as such:

package main

import (
  "fmt"
)

type Person struct {
  Name string `json:"name"`
  Secret string `json:"-"`
  Age  int `json:"age"`
}

type Person2 struct {
  Name string `json:"name"`
  Secret string `json:"secret"`
  Age  int `json:"age"`
}

type Person3 struct {
  Person
}

type Person4 struct {
  Person
  Secret string `json:"secret"`
}

func main() {
  p := Person{"SomeDude", "MySecret", 13}
  fmt.Println(Person2(p)) // Works, but code duplication
  fmt.Println(Person3(p)) // Does not work
  fmt.Println(Person4(p)) // Does not work
}

I'm guessing it could potentially be solved at compile time by substituting the embedded field with the overriding field, while maintaining the order of the fields in the struct.

Another option would be to add options to the JSON marshaller, where one could be to ignore ignored fields. What do you guys think?

@odeke-em odeke-em changed the title Marshalling ignored JSON fields encoding/json: marshalling ignored JSON fields Dec 27, 2018
@odeke-em odeke-em changed the title encoding/json: marshalling ignored JSON fields proposal: encoding/json: add options to allow marshalling of ignored JSON fields Dec 27, 2018
@gopherbot gopherbot added this to the Proposal milestone Dec 27, 2018
@odeke-em
Copy link
Member

Thank you for filing this issue @Puffton and welcome to the Go project!

I'll page some experts to help take a look @dsnet @bradfitz @fraenkel

@dsnet
Copy link
Member

dsnet commented Dec 27, 2018

See my comment here about my concern with the proliferation of top-level json options. The problem is that the json package already allows custom unmarshalers, but there is currently no way to plumb top-level options down to custom unmarshalers. Any top-level option that is added will cause divergent behavior between the two to be even more noticeable.

@rsc
Copy link
Contributor

rsc commented Jan 9, 2019

Sorry, ignored means ignored. Otherwise we need a "really ignored" for when you're printing the things that are only "kind of ignored". Like exit vs atexit vs quick_exit vs at_quick_exit in C/C++.

@rsc rsc closed this as completed Jan 9, 2019
@golang golang locked and limited conversation to collaborators Jan 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants