json-iterator / go

A high-performance 100% compatible drop-in replacement of "encoding/json"
http://jsoniter.com/migrate-from-go-std.html
MIT License
13.43k stars 1.03k forks source link

`json.RawMessage` incompatible behavior #602

Open xwjdsh opened 2 years ago

xwjdsh commented 2 years ago

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() } ```
guoxuncn commented 2 years ago

jsoniter.RawMessage