go icon indicating copy to clipboard operation
go copied to clipboard

unmarshal negative number to json.RawMessage use number get wrong result

Open jsl422 opened this issue 4 years ago • 0 comments

here is the code.

package main

import (
	"bytes"
	"encoding/json"
	"fmt"

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

var json2 = jsoniter.ConfigCompatibleWithStandardLibrary

func main() {
	var (
		x       = -1
		jsonStr []byte

		jsonRaw    json.RawMessage
		jsonRawStr []byte

		e1, e2, e3 error
	)
	jsonStr, e1 = json2.Marshal(x)

	d := json2.NewDecoder(bytes.NewBuffer(jsonStr))
	d.UseNumber()

	e2 = d.Decode(&jsonRaw)

	//e2 = utils.JsonUnmarshalBytes(jsonStr, &jsonRaw)

	jsonRawStr, e3 = json2.Marshal(jsonRaw)

	fmt.Printf("result is %#v %#v %#v, e is %#v %#v %#v\n", jsonStr, jsonRaw, jsonRawStr, e1, e2, e3)

}

// result is []byte{0x2d, 0x31} json.RawMessage{0x2d, 0x31, 0x2d} []byte{0x6e, 0x75, 0x6c, 0x6c}, e is <nil> <nil> <nil>.

the raw message result has one more character,not -1

jsl422 avatar Apr 08 '21 14:04 jsl422