- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在尝试编写一个简单的自定义编码(marshal)拆收器,但失败了。请注意,我有一个具有三个功能的界面。 Happy
和 Sad
结构都通过嵌入实现所有三个必需功能的 emotion
结构来实现此接口(interface)。
问题是当我在指向 Happy
或 的指针上调用
,我不明白为什么。您可以在 Go Playground 中重现确切的代码库或者只是看看下面。您会注意到,虽然 json.Unmarshal()
时,UnmarshalJSON
没有被调用伤心MarshalJSON
被正确调用,但 UnmarshalJSON
却没有。
type Emotion interface {
String() string
MarshalJSON() ([]byte, error)
UnmarshalJSON(data []byte) error
}
type emotion struct {
status string
}
func (s emotion) String() string {
return s.status
}
func (s emotion) MarshalJSON() ([]byte, error) {
fmt.Println("MarshalJSON is overriden: I am called fine")
x := struct {
Status string
}{
Status: s.String(),
}
return json.Marshal(x)
}
func (s *emotion) UnmarshalJSON(data []byte) error {
fmt.Println("MarshalJSON is overriden: I am never called")
y := struct {
Status string
}{
Status: "",
}
err := json.Unmarshal(data, &y)
if err != nil {
return err
}
s.status = y.Status
return nil
}
type Happy struct {
*emotion
}
// Job is not in any detention
type Sad struct {
*emotion
}
func main() {
x := Happy{&emotion{status: "happy"}}
jsonX, _ := json.Marshal(x)
var y Emotion
err := json.Unmarshal(jsonX, &y)
fmt.Printf("%v", err)
}
最佳答案
您不能解码为抽象接口(interface)类型。接口(interface)值只是一个指向类型的指针(关联该类型的方法)——它背后没有存储空间——因为抽象类型无法知道它将来可能拥有的任何具体值的确切大小。
使用具体的值类型(也实现了该接口(interface))将起作用:
y2 := emotion{}
err = json.Unmarshal(jsonX, &y2)
Playground :https://play.golang.org/p/8aCEjLgfKVQ
MarshalJSON is overriden: I am called fine
EXPECTED ERROR, Can't unmarshal into a non-concrete value: json: cannot unmarshal object into Go value of type main.Emotion
MarshalJSON is overriden: I am (fixed) and now called
SHOULD NOT ERROR: <nil>
VALUE: happy
关于go - 如何正确覆盖 UnmarshalJSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56469322/
我正在尝试编写一个简单的自定义编码(marshal)拆收器,但失败了。请注意,我有一个具有三个功能的界面。 Happy 和 Sad 结构都通过嵌入实现所有三个必需功能的 emotion 结构来实现此接
这有效:http://play.golang.org/p/-Kv3xAguDR . 这会导致堆栈溢出:http://play.golang.org/p/1-AsHFj51O . 我不明白为什么。在这种
我在其他语言(如 Java)中创建自己的类型作为枚举值。我为此类型实现了反序列化 UnmarshalJSON 方法,发现 marshal 无法转换为自定义类型 package main import
有人可以帮我看看这里出了什么问题吗?由于某种原因,输出不一样,我不明白为什么。 type rTime time.Time func (rt *rTime) UnmarshalJSON(data []b
所以,我有结构 P。我需要将一些 json 数据解码到 P,但有时它会出现嵌入式结构,Embedded。在任何一种情况下,我都从 API 中解码 json 并需要格式化“Formatted”字段。在嵌
在decode.go ,它提到: // To unmarshal JSON into a value implementing the Unmarshaler interface, // Unmars
我有一个简单的类型,它在 Go 中实现了子类型整数常量到字符串的转换,反之亦然。我希望能够自动将 JSON 中的字符串解码为这种类型的值。我不能,因为 UnmarshalJSON 没有给我返回或修改标
我想 UnmarshalJSON 包含如下接口(interface)的结构: type Filterer interface { Filter(s string) error } type F
当您将 JSON 解码为 []interface{} 时,除了 bool、int 和 string 等标准类型之外,还有什么方法可以自动检测类型吗? 我注意到以下内容,假设我编码 [uuid.UUID
我有一个 struct,它嵌入了一个指向另一个 struct 的嵌入式指针。当我使用默认的 json.Unmarshal 行为时,它工作得很好。但是当我为 embedded struct 的类型实现
我想要使用 base64 在 JSON 中编码和解码的 byte slice RawURLEncoding 而不是 StdEncoding。没有明显的方法可以通过 encoding/json pack
我得到了以下自定义类型: type TimeWithoutZone struct { time.Time } 编码(marshal)处理工作正常: const timeWithoutZoneF
如何在结构中创建方法 UnmarshalJSON,在内部使用 json.Unmarshal 而不会导致堆栈溢出? package xapo type Xapo struct {} func (x Xa
我正在尝试解码具有嵌入式类型的结构。当嵌入类型具有 UnmarshalJSON 方法时,外部类型的解码失败: https://play.golang.org/p/Y_Tt5O8A1Q package
我想执行一些额外的步骤来初始化我的实现 UnmarshalJSON 中的数据结构。在该实现中调用 json.Unmarshal(b, type) 自然会导致堆栈溢出。 JSON 解码器不断尝试查找,如
我的应用程序中有以下结构 // foo.go type FooList struct { Fools []*foo `json:"list"` // maybe Req *h
Go 提供了 encoding/json.Unmarshaler 接口(interface),因此类型可以控制它们从 JSON 解码的方式。在几乎所有情况下,编码后的 JSON 值都直接传递给 Unm
我是一名优秀的程序员,十分优秀!