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))
}
{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
The unmarshalling of a
json.RawMessage
orjsoniter.RawMessage
does not ignore whitespace in front of a the json object:https://play.golang.org/p/Wcao2ty9jJw
Output: