1. 程式人生 > 實用技巧 >json包中的Marshal&Unmarshal 文件譯本

json包中的Marshal&Unmarshal 文件譯本

Marshal

func Marshal(v interface{})([]byte, error)

  

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 <指令碼>標籤中,字串應該使用HTMLEscape進行編碼,將"<"、">"、"&"、U+2028和U+2029替換為"\u003c"、"\u003e"、"\u0026"、"\u2028 "和"\u2029"。當使用編碼器時,可以通過呼叫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切片編碼為空JSON值。

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.

Struct值編碼為JSON物件。每個匯出的結構體欄位都會成為JSON物件的成員k:v,使用欄位名作為物件的KEY,除非該欄位因下面給出的原因之一被省略。

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.

每個結構體欄位的編碼可以通過儲存在結構體欄位(field)標籤(tag)中 "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 "-,".

作為一種特殊情況,如果欄位標籤是"-",欄位總是被省略。請注意,欄位的名稱在JSON中為"-"的欄位標籤仍可使用標籤"-,"生成,這種帶選項的。

Examples of struct field tags and their meanings:

結構欄位標籤的示例及其含義。

// Field appears in JSON as key "myName".  
//欄位在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.
//欄位在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.
//欄位在JSON中作為鍵 "Field"(預設)出現,但如果為空,則跳過該欄位。注意前面的逗號 Field int `json:",omitempty"` // Field is ignored by this package.
//這個包忽略了這個欄位 Field int `json:"-"` // Field appears in JSON as key "-".
//欄位在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的形式儲存在一個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.

匿名結構欄位通常被當作其內部匯出的欄位在外部結構中的欄位來進行調集,但要遵守下一段中所描述的通常的Go可見性規則。一個匿名結構體欄位如果在它的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規則所選擇的巢狀層次),則適用以下額外規則。

1) 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標記的,則只考慮標記的欄位,即使有多個未標記的欄位,否則會發生衝突

2) If there is exactly one field (tagged or not according to the first rule), that is selected.

如果只有一個欄位(無論是否根據第一條規則被標記),該欄位被選中。

3) 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標籤"-"。

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物件。Map的鍵型別必須是字串、整數型別或實現 encoding.TextMarshaler。通過應用以下規則對Map鍵進行排序,並將其作為JSON物件鍵使用,但須遵守上面為字串值描述的UTF-8脅迫。

- keys of any string type are used directly

- 任何字串型別的鍵都可以直接使用

- encoding.TextMarshalers are marshaled
- 編碼.TextMarshalers是被調集的。 - integer keys are converted to strings
- 整數鍵被轉換為字串

  

Pointer values encode as the value pointed to. A nil pointer encodes as the null JSON value.

指標值編碼為所指向的值。nil指標編碼為空JSON值。

Interface values encode as the value contained in the interface. A nil interface value encodes as the null JSON value.

介面值編碼為介面中包含的值。無介面值編碼為空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會導致錯誤。

Unmarshal

func Unmarshal(data []byte, v interface{}) error