Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unmarshal of RawMessage contains extra whitespace #511

Closed
fraenky8 opened this issue Nov 6, 2020 · 1 comment
Closed

Unmarshal of RawMessage contains extra whitespace #511

fraenky8 opened this issue Nov 6, 2020 · 1 comment

Comments

@fraenky8
Copy link

fraenky8 commented Nov 6, 2020

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
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant