encoding/json: struct field implementing Unmarshaler #2170
Closed
Comments
I believe that you shouldn't define Unmarshal that way because it modifies the receiver, but I'll let rsc respond. Owner changed to @rsc. Status changed to Accepted. |
I'm only using it this way against single value types (such as those defined ala: "type Foob uint64") in order to allow me to forcibly alter the way specific fields are marshaled and unmarshaled without having to reimplement the struct marshaler. Primarily I'm using this for textual representations in JSON for what are effectively enums. |
Corrected patch to actually do the right thing: Checks CanAddr() to see if a pointer can be acquired, and then checks to see if an Unmarshaler is available for pointers to that type. Failing that, does the usual logic. Attachments:
|
I have a simple case that reproduces the problem I was having. Maybe should be a separate issue. package main import ( "fmt" "json" "os" ) type T int func (t *T) MarshalJSON() ([]byte, os.Error) { return []byte(`"T"`), nil } func main() { var t struct{T T} b, _ := json.Marshal(&t) fmt.Printf("%s\n", b) } The output is {"T": 0} I expect {"T": "T"} |
This is a patch for the second issue, it just checks if *T is a Marshaler. Attachments:
|
Owner changed to builder@golang.org. |
I can take a look at this. Owner changed to @dsymonds. Status changed to Accepted. |
This issue was closed by revision bf89d58. Status changed to Fixed. |
This issue was closed.
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
by akunokuroneko:
Attachments:
The text was updated successfully, but these errors were encountered: