- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
Go 提供了 encoding/json.Unmarshaler
接口(interface),因此类型可以控制它们从 JSON 解码的方式。在几乎所有情况下,编码后的 JSON 值都直接传递给 UnmarshalJSON
方法,但如果 Unmarshaler
是一个指针并且 JSON 值为 null
。在这种情况下,指针设置为 nil
而根本不调用 UnmarshalJSON
。这是一个例子:
package main
import (
"encoding/json"
"fmt"
)
type T string
func (v *T) UnmarshalJSON(b []byte) error {
if b[0] == 'n' {
*v = "null"
} else {
*v = "not null"
}
return nil
}
func main() {
var a struct {
T T
PT1 *T
PT2 *T
}
a.PT1 = nil // just to be explicit
a.PT2 = new(T)
err := json.Unmarshal([]byte(`{"T":null,"PT1":"foo","PT2":null}`), &a)
if err != nil {
panic(err)
}
fmt.Printf("a.T is %#v\n", a.T)
if a.PT1 == nil {
fmt.Println("a.PT1 is nil")
} else {
fmt.Printf("a.PT1 points to %#v\n", *a.PT1)
}
if a.PT2 == nil {
fmt.Println("a.PT2 is nil")
} else {
fmt.Printf("a.PT2 points to %#v\n", *a.PT2)
}
}
我希望它能打印出来
a.T is "null"
a.PT1 points to "not null"
a.PT2 points to "null"
相反,它打印
a.T is "null"
a.PT1 points to "not null"
a.PT2 is nil
所以json.Unmarshal
为a.PT1
分配了一个新的T
,它最初是nil
。但是它将 a.PT2
设置为 nil
而没有调用 UnmarshalJSON
,即使 a.PT2
不是 无
。为什么?
最佳答案
这是因为将指针设置为 nil
是处理 JSON null
的最常见方式,而 UnmarshalJSON
则没有办法> *T
的方法自行完成。如果在这种情况下调用了 UnmarshalJSON
,则必须定义 (**T).UnmarshalJSON
以将 *T
设置为 无
。这会使最常见的情况变得非常尴尬。
如果你不想让 JSON null
变成 Go nil
,就不要使用指针。
关于json - 在 Go 中,为什么 JSON null 有时不会传递给 UnmarshalJSON 进行解码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34163625/
我正在尝试编写一个简单的自定义编码(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
我是一名优秀的程序员,十分优秀!