-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: encoding/json support Unmarshal type interface #23549
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
Comments
There is nothing that And So really, you should either unmarshal into |
If use json.RawMessage, and you write switc/case for AValue/BValue. package main
import (
"encoding/json"
"fmt"
)
type Interface interface {
Inter()
}
type A struct {
AValue string
}
func (a *A) Inter() {
}
type B struct {
BValue string
}
func (b *B) Inter() {
}
type Something struct {
Value interface{}
}
func (s *Something) UnmarshalJSON(b []byte) error {
var r map[string]*json.RawMessage
err := json.Unmarshal(b, &r)
if err != nil {
return err
}
for k, v := range r {
switch k {
case "AValue":
var a A
if err = json.Unmarshal([]byte(*v), &a.AValue); err != nil {
return err
}
s.Value = a
case "BValue":
var b B
if err = json.Unmarshal([]byte(*v), &b.BValue); err != nil {
return err
}
s.Value = b
}
}
return nil
}
func main() {
var i Something
err := json.Unmarshal([]byte("{\"BValue\":\"B\"}"), &i)
if err != nil {
fmt.Println(err)
}
fmt.Println(i.Value.(B).BValue)
} |
As @mvdan said, there's nothing that can be done here. You can't unmarshal into a nil interface - what methods should be used? |
Change https://golang.org/cl/107196 mentions this issue: |
This is just the first step in attempting to make all network connection have timeouts as a "safe default". TCP keepalives only protect against certain classes of network and host issues (e.g. server/OS crash), but do nothing against application-level issues (e.g. an application that accepts connections but then fails to serve requests). The actual keep-alive duration (15s) is chosen to cause broken connections to be closed after 2~3 minutes (depending on the OS, see golang#23549 for details). We don't make the actual default value part of the public API for a number of reasons: - because it's not very useful by itself: as discussed in golang#23549 the actual "timeout" after which the connection is torn down is duration*(KEEPCNT+1), and we use the OS-wide value for KEEPCNT because there's currently no way to set it from Go. - because it may change in the future: if users need to rely on a specific value they should explicitly set this value instead of relying on the default. Fixes golang#23459 Change-Id: I348c03be97588d5001e6de0f377e7a93b51957fd
This is just the first step in attempting to make all network connection have timeouts as a "safe default". TCP keepalives only protect against certain classes of network and host issues (e.g. server/OS crash), but do nothing against application-level issues (e.g. an application that accepts connections but then fails to serve requests). The actual keep-alive duration (15s) is chosen to cause broken connections to be closed after 2~3 minutes (depending on the OS, see #23549 for details). We don't make the actual default value part of the public API for a number of reasons: - because it's not very useful by itself: as discussed in #23549 the actual "timeout" after which the connection is torn down is duration*(KEEPCNT+1), and we use the OS-wide value for KEEPCNT because there's currently no way to set it from Go. - because it may change in the future: if users need to rely on a specific value they should explicitly set this value instead of relying on the default. Fixes #23459 Change-Id: I348c03be97588d5001e6de0f377e7a93b51957fd Reviewed-on: https://go-review.googlesource.com/c/107196 Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
error:json: cannot unmarshal object into Go value of type main.Interface.
A and B implemented Unmarshaler :
The same error,also not ok.
But Marshaler could marshaled.
The text was updated successfully, but these errors were encountered: