Go json 踩坑記錄

chiri發表於2021-06-11

JSON,一種輕量級的資料交換語言,該語言以易於讓人閱讀的文字為基礎,用來傳輸由屬性值或者序列性的值組成的資料物件。現已經被廣泛應用,Go 當然也提供了完備的支援,通過 encoding/json 便可方便的序列化及反序列化 JSON 資料。但是也有些關鍵點需要額外注意下。

Go 可使用 json.Marshal() 便捷的獲取 JSON 資料,檢視該函式對應的 godoc 文件,裡面有這麼一段話:

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.Marshal() 在進行序列化時,會進行 HTMLEscape 編碼,會將 “<”, “>”, “&”, U+2028, 及 U+2029 轉碼成 “\u003c”,”\u003e”, “\u0026”, “\u2028”, 和 “\u2029”。這在正常使用時是沒有問題的,但如果在對接第三方需要對 JSON 字串進行取摘要比對時,如果一方未進行 HTMLEscape 編碼,取出的摘要便會天差地別。上述文件中也給出瞭解決方法,通過 SetEscapeHTML(false) 來禁用便可。方法如下:

bf := bytes.NewBuffer([]byte{})
jsonEncoder := json.NewEncoder(bf)
jsonEncoder.SetEscapeHTML(false)
_ = jsonEncoder.Encode(body)

jsonStr := bf.String()

但是這樣使用仍然會有些問題,json.Encoder.Encode() 會在 JSON 字串後接一個 換行符,該函式原始碼如下:

// Encode writes the JSON encoding of v to the stream,
// followed by a newline character.
//
// See the documentation for Marshal for details about the
// conversion of Go values to JSON.
func (enc *Encoder) Encode(v interface{}) error {
    if enc.err != nil {
        return enc.err
    }
    e := newEncodeState()
    err := e.marshal(v, encOpts{escapeHTML: enc.escapeHTML})
    if err != nil {
        return err
    }

    // Terminate each value with a newline.
    // This makes the output look a little nicer
    // when debugging, and some kind of space
    // is required if the encoded value was a number,
    // so that the reader knows there aren't more
    // digits coming.
    e.WriteByte('\n')

    b := e.Bytes()
    if enc.indentPrefix != "" || enc.indentValue != "" {
        if enc.indentBuf == nil {
            enc.indentBuf = new(bytes.Buffer)
        }
        enc.indentBuf.Reset()
        err = Indent(enc.indentBuf, b, enc.indentPrefix, enc.indentValue)
        if err != nil {
            return err
        }
        b = enc.indentBuf.Bytes()
    }
    if _, err = enc.w.Write(b); err != nil {
        enc.err = err
    }
    encodeStatePool.Put(e)
    return err
}

可以看出,該函式在進行序列化後又寫入了一個 \n 字元。如果不需要該字元,則需要額外的將其剔除:

jsonStr := string(bf.Bytes()[:bf.bf.Len()])

轉載自 Go json 踩坑記錄

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章