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

Unmarshal of RawMessage contains extra whitespace #511

Closed fraenky8 closed 3 years ago

fraenky8 commented 4 years ago

The unmarshalling of a json.RawMessage or jsoniter.RawMessage does not ignore whitespace in front of a the json object:

package main

import (
    "encoding/json"
    "fmt"

    jsoniter "github.com/json-iterator/go"
)

type T struct {
    Name string
    Info json.RawMessage
    JI   jsoniter.RawMessage
}

// Note the extra whitespace at the `ji` (jsoniter) RawMessage key - for demonstration purposes
var tData = []byte(`{"name": "foo", "info": {"a":1}, "ji":   {"b":1}}`)

func main() {
    var t1 T
    json.Unmarshal(tData, &t1)

    fmt.Printf("%+v\n", t1)
    fmt.Printf("%q\n", string(t1.Info))
    fmt.Printf("%q\n", string(t1.JI))

    ji := jsoniter.ConfigCompatibleWithStandardLibrary

    var t2 T
    ji.Unmarshal(tData, &t2)

    fmt.Printf("%+v\n", t2)
    fmt.Printf("%q\n", string(t2.Info))
    fmt.Printf("%q\n", string(t2.JI))
}

https://play.golang.org/p/Wcao2ty9jJw

Output:

{Name:foo Info:[123 34 97 34 58 49 125] JI:[]}
"{\"a\":1}"           <---- no whitespace at json.RawMessage
""                    <---- json.Unmarshal does not know how to unmarshal the jsoniter.RawMessage which is ok
{Name:foo Info:[32 123 34 97 34 58 49 125] JI:[32 32 32 123 34 98 34 58 49 125]}
" {\"a\":1}"         <----- extra whitespace (32 ASCII)
"   {\"b\":1}"       <----- note the artificial added whitespaces here at the jsoniter.RawMessage for demonstration purposes
fraenky8 commented 3 years ago

Fixed in https://github.com/json-iterator/go/releases/tag/v1.1.11