json
22 分钟阅读
Package json implements encoding and decoding of JSON as defined in RFC 7159. The mapping between JSON and Go values is described in the documentation for the Marshal and Unmarshal functions.
json包实现了RFC 7159中定义的JSON编解码。JSON和Go值之间的映射在Marshal和Unmarshal函数的文档中有描述。
See “JSON and Go” for an introduction to this package: https://golang.org/doc/articles/json_and_go.html
有关此包的介绍,请参见JSON和Go。
Example (CustomMarshalJSON)
|
|
Example(TextMarshalJSON)
|
|
常量
This section is empty.
变量
This section is empty.
函数
func Compact
|
|
Compact appends to dst the JSON-encoded src with insignificant space characters elided.
Compact
函数将JSON编码的src附加到dst中,省略了不重要的空格字符。
func HTMLEscape
|
|
HTMLEscape appends to dst the JSON-encoded src with <
, >
, &
, U+2028 and U+2029 characters inside string literals changed to \u003c
, \u003e
, \u0026
, \u2028
, \u2029
so that the JSON will be safe to embed inside HTML <script>
tags. For historical reasons, web browsers don’t honor standard HTML escaping within <script>
tags, so an alternative JSON encoding must be used.
HTMLEscape
函数将JSON编码的src附加到dst中,将字符串文字内的<
、>
、&
、U+2028和U+2029字符更改为\u003c
、\u003e
、\u0026
、\u2028
、\u2029
,以使JSON可以安全地嵌入HTML <script>
标记中。由于历史原因,Web浏览器不支持在<script>
标记中使用标准的HTML转义,因此必须使用替代的JSON编码。
HTMLEscape Example
|
|
func Indent
|
|
Indent appends to dst an indented form of the JSON-encoded src. Each element in a JSON object or array begins on a new, indented line beginning with prefix followed by one or more copies of indent according to the indentation nesting. The data appended to dst does not begin with the prefix nor any indentation, to make it easier to embed inside other formatted JSON data. Although leading space characters (space, tab, carriage return, newline) at the beginning of src are dropped, trailing space characters at the end of src are preserved and copied to dst. For example, if src has no trailing spaces, neither will dst; if src ends in a trailing newline, so will dst.
Indent
函数将JSON编码的src的缩进形式附加到dst中。JSON对象或数组中的每个元素都在新的缩进行上开始,该行以prefix
开头,后跟一个或多个indent
的副本,具体取决于缩进嵌套。附加到dst的数据不以prefix
或任何缩进开始,以使其更容易嵌入其他格式化的JSON数据中。虽然src
开头的前导空格字符(空格、制表符、回车、换行符)会被删除,但src
末尾的尾随空格字符会被保留并复制到dst
中。例如,如果src没有尾随空格,则dst
也没有;如果src
以尾随换行符结束,则dst也是如此。
Indent Example
|
|
func Marshal
|
|
Marshal returns the JSON encoding of v.
Marshal
函数返回v的JSON编码。
Marshal traverses the value v recursively. If an encountered value implements the Marshaler interface and is not a nil pointer, Marshal calls its MarshalJSON method to produce JSON. If no MarshalJSON method is present but the value implements encoding.TextMarshaler instead, Marshal calls its MarshalText method and encodes the result as a JSON string. The nil pointer exception is not strictly necessary but mimics a similar, necessary exception in the behavior of UnmarshalJSON.
Marshal
函数递归遍历值v。如果遇到的值实现了Marshaler
接口并且不是nil指针,则Marshal调用其MarshalJSON
方法以生成JSON。如果没有MarshalJSON
方法但该值代替实现了encoding.TextMarshaler
,则Marshal
调用其MarshalText
方法并将结果编码为JSON字符串。nil指针异常并不是严格必要的,但模仿了在UnmarshalJSON
的行为中必要的类似异常。
Otherwise, Marshal uses the following type-dependent default encodings:
否则,Marshal
使用以下类型相关的默认编码:
Boolean values encode as JSON booleans.
布尔值被编码为JSON布尔值。
Floating point, integer, and Number values encode as JSON numbers.
浮点数、整数和Number类型的值被编码为JSON数字。
String values encode as JSON strings coerced to valid UTF-8, replacing invalid bytes with the Unicode replacement rune. So that the JSON will be safe to embed inside HTML <script>
tags, the string is encoded using HTMLEscape, which replaces “<
”, “>
”, “&
”, U+2028
, and U+2029
are escaped to “\u003c
”,"\u003e
", “\u0026
”, “\u2028
”, and “\u2029
”. This replacement can be disabled when using an Encoder, by calling SetEscapeHTML(false).
字符串类型的值被编码为JSON字符串,被强制转换为有效的UTF-8编码,无效的字节会被替换为Unicode替换符。为了将JSON安全地嵌入到HTML的<script>
标签中,使用HTMLEscape对字符串进行编码,将"<
", “>
”, “&
”, U+2028
, 和 U+2029
转义为"\u003c
", “\u003e
”, “\u0026
”, “\u2028
“和”\u2029
"。使用Encoder
时,可以通过调用SetEscapeHTML(false)
来禁用此替换。
Array and slice values encode as JSON arrays, except that []byte encodes as a base64-encoded string, and a nil slice encodes as the null JSON value.
数组和切片类型的值被编码为JSON数组,但[]byte
类型的值会被编码为base64编码的字符串,而nil切片类型的值会被编码为null。
Struct values encode as JSON objects. Each exported struct field becomes a member of the object, using the field name as the object key, unless the field is omitted for one of the reasons given below.
结构体类型的值被编码为JSON对象。每个导出的结构体字段都成为对象的一个成员,使用字段名作为对象键,除非由于以下原因之一而省略该字段。
The encoding of each struct field can be customized by the format string stored under the “json” key in the struct field’s tag. The format string gives the name of the field, possibly followed by a comma-separated list of options. The name may be empty in order to specify options without overriding the default field name.
每个结构体字段的编码可以通过存储在该字段标记的"json"键下的格式字符串进行自定义。格式字符串给出字段名,可能后跟一个用逗号分隔的选项列表。如果不想覆盖默认的字段名,则字段名可以为空。
The “omitempty” option specifies that the field should be omitted from the encoding if the field has an empty value, defined as false, 0, a nil pointer, a nil interface value, and any empty array, slice, map, or string.
“omitempty"选项指定如果该字段具有空值(定义为false、0、nil指针、nil接口值以及任何空数组、切片、映射或字符串),则应该从编码中省略该字段。
As a special case, if the field tag is “-”, the field is always omitted. Note that a field with name “-” can still be generated using the tag “-,”.
作为特殊情况,如果字段标记为”-",则始终省略该字段。请注意,具有名称”-“的字段仍然可以使用标记”-,“生成。
Examples of struct field tags and their meanings:
以下是结构体字段标记及其含义的示例:
// Field appears in JSON as key "myName".
// Field 出现在JSON中作为键"myName"。
Field int `json:"myName"`
// Field appears in JSON as key "myName" and
// the field is omitted from the object if its value is empty,
// as defined above.
// Field 出现在JSON中作为键"myName",
// 如果其值为空,将从对象中省略该字段。
Field int `json:"myName,omitempty"`
// Field appears in JSON as key "Field" (the default), but
// the field is skipped if empty.
// Note the leading comma.
// Field 出现在JSON中作为键"Field"(默认值),
// 但如果为空则跳过该字段。
// 请注意前导逗号。
Field int `json:",omitempty"`
// Field is ignored by this package.
// 此包忽略 Field。
Field int `json:"-"`
// Field appears in JSON as key "-".
// Field 出现在JSON中作为键"-"。
Field int `json:"-,"`
The “string” option signals that a field is stored as JSON inside a JSON-encoded string. It applies only to fields of string, floating point, integer, or boolean types. This extra level of encoding is sometimes used when communicating with JavaScript programs:
“string"选项表示字段存储在JSON编码的字符串中。它仅适用于字符串、浮点数、整数或布尔类型的字段。在与JavaScript程序通信时,有时会使用这种额外的编码级别:
Int64String int64 `json:",string"`
The key name will be used if it’s a non-empty string consisting of only Unicode letters, digits, and ASCII punctuation except quotation marks, backslash, and comma.
如果键名是由Unicode字母、数字和ASCII标点(引号、反斜杠和逗号除外)组成的非空字符串,则将使用该键名。
Anonymous struct fields are usually marshaled as if their inner exported fields were fields in the outer struct, subject to the usual Go visibility rules amended as described in the next paragraph. An anonymous struct field with a name given in its JSON tag is treated as having that name, rather than being anonymous. An anonymous struct field of interface type is treated the same as having that type as its name, rather than being anonymous.
匿名结构字段通常被解释为其内部导出字段在外部结构体中的字段,但受到下一段中描述的修饰性可见性规则的影响。具有在其JSON标签中给出的名称的匿名结构字段将被视为具有该名称,而不是匿名的。接口类型的匿名结构字段将与其类型相同,而不是匿名的。
The Go visibility rules for struct fields are amended for JSON when deciding which field to marshal or unmarshal. If there are multiple fields at the same level, and that level is the least nested (and would therefore be the nesting level selected by the usual Go rules), the following extra rules apply:
当决定哪个字段进行编组或取消编组时,针对JSON修饰性可见性规则修正结构字段的Go可见性规则。如果同一级别存在多个字段,并且该级别是最不嵌套的(因此将是常规Go规则选择的嵌套级别),则以下额外规则适用:
- Of those fields, if any are JSON-tagged, only tagged fields are considered, even if there are multiple untagged fields that would otherwise conflict.
- 对于这些字段,如果有任何字段具有JSON标记,则只考虑带有标记的字段,即使有多个未标记的字段也会冲突。
- If there is exactly one field (tagged or not according to the first rule), that is selected.
- 如果恰好有一个字段(根据第一条规则是否带有标记),则选择该字段。
- Otherwise there are multiple fields, and all are ignored; no error occurs.
- 否则将忽略所有字段;不会发生错误。
Handling of anonymous struct fields is new in Go 1.1. Prior to Go 1.1, anonymous struct fields were ignored. To force ignoring of an anonymous struct field in both current and earlier versions, give the field a JSON tag of “-”.
对匿名结构字段的处理是Go 1.1的新内容。在Go 1.1之前,匿名结构字段被忽略。要在当前版本和早期版本中强制忽略匿名结构字段,请给该字段一个”-“的JSON标签。
在Go 1.1中,对匿名结构字段的处理是新的。在Go 1.1之前,匿名结构字段被忽略。要在当前版本和早期版本中强制忽略匿名结构字段,请给该字段一个JSON标记”-"。
Map values encode as JSON objects. The map’s key type must either be a string, an integer type, or implement encoding.TextMarshaler. The map keys are sorted and used as JSON object keys by applying the following rules, subject to the UTF-8 coercion described for string values above:
Map值编码为JSON对象。映射的键类型必须是字符串、整数类型或实现encoding.TextMarshaler。映射键按以下规则排序并用作JSON对象键,但要受上述字符串值的UTF-8强制规定:
keys of any string type are used directly
任何字符串类型的键都直接使用。
encoding.TextMarshalers are marshaled
encoding.TextMarshalers已被解组。
integer keys are converted to strings
整数键被转换为字符串。
Pointer values encode as the value pointed to. A nil pointer encodes as the null JSON value.
指针值编码为指向的值。空指针编码为null JSON值。指针值编码为所指向的值。一个nil指针编码为空的JSON值。
Interface values encode as the value contained in the interface. A nil interface value encodes as the null JSON value.
接口值编码为接口中包含的值。空接口值编码为null JSON值。
Channel, complex, and function values cannot be encoded in JSON. Attempting to encode such a value causes Marshal to return an UnsupportedTypeError.
通道、复数和函数值不能编码为 JSON。尝试对此类值进行编码将导致 Marshal 返回 UnsupportedTypeError。
JSON cannot represent cyclic data structures and Marshal does not handle them. Passing cyclic structures to Marshal will result in an error.
JSON 不能表示循环数据结构,Marshal 也无法处理它们。将循环结构传递给 Marshal 将导致错误。
Marshal Example
|
|
func MarshalIndent
|
|
MarshalIndent is like Marshal but applies Indent to format the output. Each JSON element in the output will begin on a new line beginning with prefix followed by one or more copies of indent according to the indentation nesting.
MarshalIndent
函数类似于 Marshal
函数,但应用 Indent
以格式化输出。输出中的每个 JSON 元素都将在新行上开始,以前缀开头,后跟一个或多个 indent
副本,具体取决于缩进嵌套。
MarshalIndent Example
|
|
func Unmarshal
|
|
Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v. If v is nil or not a pointer, Unmarshal returns an InvalidUnmarshalError.
Unmarshal
函数解析 JSON 编码的数据并将结果存储在 v 所指向的值中。如果 v 为 nil 或不是指针,则 Unmarshal 返回 InvalidUnmarshalError。
Unmarshal uses the inverse of the encodings that Marshal uses, allocating maps, slices, and pointers as necessary, with the following additional rules:
Unmarshal
函数使用 Marshal函数所使用的编码的反向方式,根据以下附加规则分配映射、切片和指针:
To unmarshal JSON into a pointer, Unmarshal first handles the case of the JSON being the JSON literal null. In that case, Unmarshal sets the pointer to nil. Otherwise, Unmarshal unmarshals the JSON into the value pointed at by the pointer. If the pointer is nil, Unmarshal allocates a new value for it to point to.
要将 JSON 反序列化为指针,Unmarshal
首先处理 JSON 为 JSON 文字 null 的情况。在这种情况下,Unmarshal
将指针设置为 nil。否则,Unmarshal
将 JSON 反序列化为指针所指向的值。如果指针为 nil,则 Unmarshal
为其分配一个新值。
To unmarshal JSON into a value implementing the Unmarshaler interface, Unmarshal calls that value’s UnmarshalJSON method, including when the input is a JSON null. Otherwise, if the value implements encoding.TextUnmarshaler and the input is a JSON quoted string, Unmarshal calls that value’s UnmarshalText method with the unquoted form of the string.
要将 JSON 反序列化为实现 Unmarshaler 接口的值,Unmarshal
函数调用该值的 UnmarshalJSON
方法,包括输入为 JSON null 的情况。否则,如果该值实现 encoding.TextUnmarshaler
并且输入是 JSON 引号字符串,则 Unmarshal
会使用字符串的未引用形式调用该值的 UnmarshalText
方法。
To unmarshal JSON into a struct, Unmarshal matches incoming object keys to the keys used by Marshal (either the struct field name or its tag), preferring an exact match but also accepting a case-insensitive match. By default, object keys which don’t have a corresponding struct field are ignored (see Decoder.DisallowUnknownFields for an alternative).
要将 JSON 反序列化为结构体,Unmarshal 将传入的对象键与 Marshal 使用的键进行匹配(即结构字段名或其标记),优先选择完全匹配,但也接受大小写不敏感的匹配。默认情况下,没有对应结构体字段的对象键将被忽略(有关另一种方式,请参见 Decoder.DisallowUnknownFields
)。
To unmarshal JSON into an interface value, Unmarshal stores one of these in the interface value:
要将 JSON 反序列化为接口值,Unmarshal 将以下其中之一存储在接口值中:
bool, for JSON booleans
对于 JSON 布尔值,为 bool 类型;
float64, for JSON numbers
对于 JSON 数字,为 float64 类型;
string, for JSON strings
对于 JSON 字符串,为 string 类型;
[]interface{}, for JSON arrays
对于 JSON 数组,为 []interface{} 类型;
map[string]interface{}, for JSON objects
对于 JSON 对象,为 map[string]interface{} 类型;
nil for JSON null
对于 JSON null,为 nil 类型。
To unmarshal a JSON array into a slice, Unmarshal resets the slice length to zero and then appends each element to the slice. As a special case, to unmarshal an empty JSON array into a slice, Unmarshal replaces the slice with a new empty slice.
要将 JSON 数组反序列化为切片,Unmarshal 将切片长度重置为零,然后将每个元素附加到切片中。作为一个特例,要将空 JSON 数组反序列化为切片,Unmarshal 将该切片替换为一个新的空切片。
To unmarshal a JSON array into a Go array, Unmarshal decodes JSON array elements into corresponding Go array elements. If the Go array is smaller than the JSON array, the additional JSON array elements are discarded. If the JSON array is smaller than the Go array, the additional Go array elements are set to zero values.
将JSON数组解码到Go数组中,Unmarshal将JSON数组元素解码为对应的Go数组元素。如果Go数组小于JSON数组,则将多余的JSON数组元素丢弃。如果JSON数组小于Go数组,则将多余的Go数组元素设置为零值。
To unmarshal a JSON object into a map, Unmarshal first establishes a map to use. If the map is nil, Unmarshal allocates a new map. Otherwise Unmarshal reuses the existing map, keeping existing entries. Unmarshal then stores key-value pairs from the JSON object into the map. The map’s key type must either be any string type, an integer, implement json.Unmarshaler, or implement encoding.TextUnmarshaler.
将JSON对象解码为映射时,Unmarshal首先创建一个映射以使用。如果映射为nil,则Unmarshal将分配一个新的映射。否则,Unmarshal将重用现有的映射,保留现有的条目。然后,Unmarshal将JSON对象中的键值对存储到映射中。映射的键类型必须是任何字符串类型、整数、实现json.Unmarshaler或实现encoding.TextUnmarshaler。
If the JSON-encoded data contain a syntax error, Unmarshal returns a SyntaxError.
如果JSON编码的数据包含语法错误,则Unmarshal返回一个SyntaxError。
If a JSON value is not appropriate for a given target type, or if a JSON number overflows the target type, Unmarshal skips that field and completes the unmarshaling as best it can. If no more serious errors are encountered, Unmarshal returns an UnmarshalTypeError describing the earliest such error. In any case, it’s not guaranteed that all the remaining fields following the problematic one will be unmarshaled into the target object.
如果JSON值不适合给定的目标类型,或者JSON数字溢出了目标类型,则Unmarshal跳过该字段,并尽可能完成解组。如果没有遇到更严重的错误,Unmarshal将返回一个UnmarshalTypeError,描述最早出现的这种错误。在任何情况下,不能保证在问题字段后面的所有剩余字段都会解组到目标对象中。
The JSON null value unmarshals into an interface, map, pointer, or slice by setting that Go value to nil. Because null is often used in JSON to mean “not present,” unmarshaling a JSON null into any other Go type has no effect on the value and produces no error.
JSON null值通过将Go值设置为nil来解组到接口、映射、指针或切片中。因为null通常在JSON中用于表示"不存在”,将JSON null解组成任何其他Go类型对该值没有影响,并且不会产生错误。
When unmarshaling quoted strings, invalid UTF-8 or invalid UTF-16 surrogate pairs are not treated as an error. Instead, they are replaced by the Unicode replacement character U+FFFD
.
解组引用字符串时,无效的UTF-8或无效的UTF-16代理对不会被视为错误。相反,它们将被替换为Unicode替换字符U+FFFD
。
Unmarshal Example
|
|
func Valid <- go1.9
|
|
Valid
函数判断数据 data
是否是有效的 JSON 编码。
Valid Example
|
|
类型
type Decoder
|
|
Valid reports whether data is a valid JSON encoding.
Decoder
从输入流中读取和解码 JSON 值。
Example
A Decoder reads and decodes JSON values from an input stream.
这个例子使用了 Decoder
来解码一系列不同的 JSON 值流。
|
|
func NewDecoder
|
|
NewDecoder returns a new decoder that reads from r.
NewDecoder
函数返回一个从 r 读取数据的新的 Decoder。
The decoder introduces its own buffering and may read data from r beyond the JSON values requested.
Decoder
引入了自己的缓冲,可能会从 r 中读取超出请求的 JSON 值的数据。
(*Decoder) Buffered <- go1.1
|
|
Buffered returns a reader of the data remaining in the Decoder’s buffer. The reader is valid until the next call to Decode.
Buffered
方法返回 Decoder 缓冲区中剩余的数据的 reader。该 reader 在下次调用 Decode 之前有效。
(*Decoder) Decode
|
|
Decode reads the next JSON-encoded value from its input and stores it in the value pointed to by v.
Decode
方法从输入中读取下一个 JSON 编码的值并将其存储在 v 所指向的值中。
See the documentation for Unmarshal for details about the conversion of JSON into a Go value.
有关 JSON 转换为 Go 值的详细信息,请参见 Unmarshal 的文档。
Decode Example
This example uses a Decoder to decode a streaming array of JSON objects.
这个示例使用解码器(Decoder)来解码一个 JSON 对象流数组。
|
|
(*Decoder) DisallowUnknownFields <- go1.10
|
|
DisallowUnknownFields causes the Decoder to return an error when the destination is a struct and the input contains object keys which do not match any non-ignored, exported fields in the destination.
DisallowUnknownFields
方法会导致在目标对象为结构体且输入包含在目标中不存在的、未被忽略的公开字段的对象键时,解码器返回一个错误。
(*Decoder) InputOffset <- go1.14
|
|
InputOffset returns the input stream byte offset of the current decoder position. The offset gives the location of the end of the most recently returned token and the beginning of the next token.
InputOffset
方法返回当前解码器位置的输入流字节偏移量。该偏移量给出了最近返回的标记的结束位置和下一个标记的开始位置。
(*Decoder) More <- go1.5
|
|
More reports whether there is another element in the current array or object being parsed.
More方法报告当前正在解析的数组或对象中是否还有另一个元素。
(*Decoder) Token <- go1.5
|
|
Token returns the next JSON token in the input stream. At the end of the input stream, Token returns nil, io.EOF.
Token
方法返回输入流中的下一个JSON标记。在输入流的末尾,Token
方法返回nil
和io.EOF
。
Token guarantees that the delimiters [ ]
{ }
it returns are properly nested and matched: if Token encounters an unexpected delimiter in the input, it will return an error.
Token
方法保证它返回的分隔符[ ]
{}
是正确嵌套和匹配的:如果Token方法在输入中遇到意外的分隔符,则它将返回一个错误。
The input stream consists of basic JSON values—bool, string, number, and null—along with delimiters [ ] { } of type Delim to mark the start and end of arrays and objects. Commas and colons are elided.
输入流由基本的JSON值——布尔值、字符串、数字和null——以及类型为Delim的[ ]
{}
分隔符组成,用于标记数组和对象的开始和结束。逗号和冒号被省略。
Token Example
This example uses a Decoder to decode a stream of distinct JSON values.
这个例子使用 Decoder 来解码一个由不同的 JSON 值组成的流。
|
|
(*Decoder) UseNumber <- go1.1
|
|
UseNumber causes the Decoder to unmarshal a number into an interface{}
as a Number instead of as a float64.
UseNumber方法会导致解码器将数字解组为Number类型的interface{}
,而不是解组为float64。
type Delim <- go1.5
|
|
A Delim is a JSON array or object delimiter, one of [ ] { or }.
Delim
是JSON数组或对象分隔符,其中之一是[ ]
{
或 }
。
(Delim) String <- go1.5
|
|
type Encoder
|
|
An Encoder writes JSON values to an output stream.
Encoder
将JSON值写入输出流。
func NewEncoder
|
|
NewEncoder returns a new encoder that writes to w.
NewEncoder
函数返回一个新的编码器,它将写入到 w。
(*Encoder) Encode
|
|
Encode writes the JSON encoding of v to the stream, followed by a newline character.
Encode
方法将 v
的 JSON 编码写入流中,随后加上一个换行符。
See the documentation for Marshal for details about the conversion of Go values to JSON.
有关将 Go 值转换为 JSON 的详细信息,请参见 Marshal
的文档。
(*Encoder) SetEscapeHTML <- go1.7
|
|
SetEscapeHTML specifies whether problematic HTML characters should be escaped inside JSON quoted strings. The default behavior is to escape &, <, and > to \u0026, \u003c, and \u003e to avoid certain safety problems that can arise when embedding JSON in HTML.
SetEscapeHTML
方法指定是否在 JSON 引用字符串内部转义有问题的 HTML 字符。默认行为是将 &
、<
和 >
转义为 \u0026
、\u003c
和 \u003e
,以避免在 HTML 中嵌入 JSON 时出现某些安全问题。
In non-HTML settings where the escaping interferes with the readability of the output, SetEscapeHTML(false) disables this behavior.
在非 HTML 环境中,当转义干扰输出的可读性时,SetEscapeHTML(false)
将禁用此行为。
(*Encoder) SetIndent <- go1.7
|
|
SetIndent instructs the encoder to format each subsequent encoded value as if indented by the package-level function Indent(dst, src, prefix, indent). Calling SetIndent("", “”) disables indentation.
SetIndent
方法指示编码器将每个后续编码值格式化为由 package-level 函数 Indent(dst, src, prefix, indent)
缩进的形式。调用 SetIndent("", "")
禁用缩进。
type InvalidUTF8Error <-DEPRECATED
|
|
Before Go 1.2, an InvalidUTF8Error was returned by Marshal when attempting to encode a string value with invalid UTF-8 sequences. As of Go 1.2, Marshal instead coerces the string to valid UTF-8 by replacing invalid bytes with the Unicode replacement rune U+FFFD.
Deprecated: No longer used; kept for compatibility.
(*InvalidUTF8Error) Error
|
|
type InvalidUnmarshalError
|
|
An InvalidUnmarshalError describes an invalid argument passed to Unmarshal. (The argument to Unmarshal must be a non-nil pointer.)
InvalidUnmarshalError
描述传递给 Unmarshal
的无效实参。(传递给 Unmarshal
的实参必须是非 nil 指针。)
(*InvalidUnmarshalError) Error
|
|
type Marshaler
|
|
Marshaler is the interface implemented by types that can marshal themselves into valid JSON.
Marshaler
是一种类型的接口,这种类型可以将自身编组为有效的 JSON。
type MarshalerError
|
|
A MarshalerError represents an error from calling a MarshalJSON or MarshalText method.
MarshalerError
表示调用 MarshalJSON 或 MarshalText 方法时的错误。
(*MarshalerError) Error
|
|
(*MarshalerError) Unwrap <- go1.13
|
|
Unwrap returns the underlying error.
Unwrap
方法返回基本的错误。
type Number <- go1.1
|
|
A Number represents a JSON number literal.
Number
类型表示JSON数值字面。
(Number) Float64 <- go1.1
|
|
Float64 returns the number as a float64.
Float64
方法将数值转换为float64。
(Number) Int64 <- go1.1
|
|
Int64 returns the number as an int64.
Int64
方法将数值转换为int64。
(Number) String <- go1.1
|
|
String returns the literal text of the number.
String
方法返回数值的文字表示形式。
type RawMessage
|
|
RawMessage is a raw encoded JSON value. It implements Marshaler and Unmarshaler and can be used to delay JSON decoding or precompute a JSON encoding.
RawMessage
是一个原始编码的JSON值。它实现了Marshaler和Unmarshaler接口,可以用于延迟JSON解码或预先计算JSON编码。
Example(Marshal)
This example uses RawMessage to use a precomputed JSON during marshal.
这个示例使用 RawMessage 来在编组时使用预计算的 JSON。
|
|
Example (Unmarshal)
This example uses RawMessage to delay parsing part of a JSON message.
这个示例使用 RawMessage
来延迟解析 JSON 消息的一部分。
|
|
(RawMessage) MarshalJSON
|
|
MarshalJSON returns m as the JSON encoding of m.
MarshalJSON
方法返回m的JSON编码。
(*RawMessage) UnmarshalJSON
|
|
UnmarshalJSON sets *m to a copy of data.
UnmarshalJSON
方法将data的一个副本设置为*m
。
type SyntaxError
|
|
A SyntaxError is a description of a JSON syntax error. Unmarshal will return a SyntaxError if the JSON can’t be parsed.
SyntaxError
描述了一个JSON语法错误。如果JSON不能被解析,Unmarshal函数将返回SyntaxError。
(*SyntaxError) Error
|
|
type Token <- go1.5
|
|
A Token holds a value of one of these types:
Token
保存以下类型之一的值:
Delim, for the four JSON delimiters [ ] { }
bool, for JSON booleans
float64, for JSON numbers
Number, for JSON numbers
string, for JSON string literals
nil, for JSON null
type UnmarshalFieldError <- DEPRECATED
|
|
An UnmarshalFieldError describes a JSON object key that led to an unexported (and therefore unwritable) struct field.
Deprecated: No longer used; kept for compatibility.
(*UnmarshalFieldError) Error
|
|
type UnmarshalTypeError
|
|
An UnmarshalTypeError describes a JSON value that was not appropriate for a value of a specific Go type.
UnmarshalTypeError
描述了一个不适合特定Go类型值的JSON值。
(*UnmarshalTypeError) Error
|
|
type Unmarshaler
|
|
Unmarshaler is the interface implemented by types that can unmarshal a JSON description of themselves. The input can be assumed to be a valid encoding of a JSON value. UnmarshalJSON must copy the JSON data if it wishes to retain the data after returning.
Unmarshaler
是由可以解码JSON描述的类型实现的接口。可以假定输入是JSON值的有效编码。如果想在返回后保留数据,UnmarshalJSON
必须复制JSON数据。
By convention, to approximate the behavior of Unmarshal itself, Unmarshalers implement UnmarshalJSON([]byte(“null”)) as a no-op.
按照惯例,为了近似于Unmarshal
本身的行为,Unmarshalers
将UnmarshalJSON([]byte("null"))
实现为无操作。
type UnsupportedTypeError
|
|
An UnsupportedTypeError is returned by Marshal when attempting to encode an unsupported value type.
在尝试对不受支持的值类型进行编码时,Marshal
返回UnsupportedTypeError
。
(*UnsupportedTypeError) Error
|
|
type UnsupportedValueError
|
|
An UnsupportedValueError is returned by Marshal when attempting to encode an unsupported value.
在尝试对不受支持的值进行编码时,Marshal
返回UnsupportedValueError
。
(*UnsupportedValueError) Error
|
|