Open xwjdsh opened 2 years ago
version: v1.1.12
Go Playground: https://go.dev/play/p/v_zY8DxCkg6
jsoniter.RawMessage
version: v1.1.12
Go Playground: https://go.dev/play/p/v_zY8DxCkg6
code
``` package main import ( "bytes" "encoding/json" "fmt" jsoniter "github.com/json-iterator/go" ) var jsoniterObj = jsoniter.ConfigCompatibleWithStandardLibrary type Data struct { T json.RawMessage } type T struct { Value int } var t = &T{Value: 1} func main() { fmt.Println("json:", jsonResult()) fmt.Println("jsoniter:", jsoniterResult()) } func panicIfErr(err error) { if err != nil { panic(err) } } func jsonResult() string { buf := &bytes.Buffer{} enc := json.NewEncoder(buf) panicIfErr(enc.Encode(t)) d := &Data{T: buf.Bytes()} buf.Reset() enc = json.NewEncoder(buf) panicIfErr(enc.Encode(d)) return buf.String() } func jsoniterResult() string { buf := &bytes.Buffer{} enc := jsoniterObj.NewEncoder(buf) panicIfErr(enc.Encode(t)) d := &Data{T: buf.Bytes()} buf.Reset() enc = jsoniterObj.NewEncoder(buf) panicIfErr(enc.Encode(d)) return buf.String() } ```