go icon indicating copy to clipboard operation
go copied to clipboard

Cannot unmarshal struct when key is also ASCII escaped

Open Andrew-M-C opened this issue 4 years ago • 3 comments

version: v1.1.10

code:

package main

import (
	"encoding/json"
	"fmt"
	
	jsoniter "github.com/json-iterator/go"
)

type obj struct {
	Msg string `json:"消息"`
}

func main() {
	jsonit := jsoniter.ConfigCompatibleWithStandardLibrary

	// \u6D88\u606F are escaped charecters for Chinese word "消息"
	b := []byte(`{"\u6D88\u606F":"\u6D88\u606F"}`) 

	
	o := obj{}
	json.Unmarshal(b, &o)
	fmt.Printf("got json object: %+v\n", o)

	o = obj{}
	jsonit.Unmarshal(b, &o)
	fmt.Printf("got jsonit object: %+v\n", o)
}

Go output:

got json object: {Msg:消息}
got jsonit object: {Msg:}

But expected:

got json object: {Msg:消息}
got jsonit object: {Msg:消息}

Andrew-M-C avatar Apr 25 '21 12:04 Andrew-M-C

Similar issue exists on https://github.com/goccy/go-json/issues/234 go-json package as well

harshavardhana avatar Jun 02 '21 00:06 harshavardhana

However, official "encoding/json" works well

Andrew-M-C avatar Jun 07 '21 10:06 Andrew-M-C

Similar issue exists on goccy/go-json#234 go-json package as well

This was addressed as well

harshavardhana avatar Jun 07 '21 15:06 harshavardhana