You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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"
)
typePersonstruct {
Namestring`json:"name"`Secretstring`json:"-"`Ageint`json:"age"`
}
typePerson2struct {
Namestring`json:"name"`Secretstring`json:"secret"`Ageint`json:"age"`
}
typePerson3struct {
Person
}
typePerson4struct {
PersonSecretstring`json:"secret"`
}
funcmain() {
p:=Person{"SomeDude", "MySecret", 13}
fmt.Println(Person2(p)) // Works, but code duplicationfmt.Println(Person3(p)) // Does not workfmt.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?
The text was updated successfully, but these errors were encountered:
odeke-em
changed the title
Marshalling ignored JSON fields
encoding/json: marshalling ignored JSON fields
Dec 27, 2018
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
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.
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++.
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
)?Suggestion
It would be cool if we could embed structs and then cast them, as such:
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?
The text was updated successfully, but these errors were encountered: