go icon indicating copy to clipboard operation
go copied to clipboard

JSON iter doesn't work properly with nil RawMessage

Open mjnovice opened this issue 4 years ago • 3 comments

package main

import (
	"encoding/json"
	"fmt"

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

func main() {
	type A struct {
		Rm jsoniter.RawMessage
	}

	a := A{}
	b, e := jsoniter.Marshal(a)
	fmt.Println("jsoniter: ",b, e)
	e = jsoniter.Unmarshal(b, &a)
	fmt.Println("jsoniter: ",e)

	b, e = json.Marshal(a)
	fmt.Println("json: ",b, e)
	e = json.Unmarshal(b, &a)
	fmt.Println("json: ",e)
}

https://play.golang.org/p/RoBEyZNxk-R

mjnovice avatar Jun 24 '20 20:06 mjnovice

Fully symmetric comparison validates issue: https://play.golang.org/p/ydVcBDQsF5l

funny-falcon avatar Jul 07 '20 13:07 funny-falcon

Hello. It's brake behavior to unmarshal null value into RawMessage

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

NOMORECOFFEE avatar Sep 21 '21 20:09 NOMORECOFFEE

@AllenX2018 As mentioned by the preceding commenter, the changes introduced in https://github.com/json-iterator/go/commit/5bce16d299eb21bb5a80aa37151d805c37acd79d broke compatibility with unmarshaling of null into a RawMessage: the result is a nil byte slice, but the standard library would store the bytes null instead.

Would it be possible to address this issue?

tiit-clarifai avatar Jan 23 '23 18:01 tiit-clarifai