Skip to content

Inconsistent error message for unmarshalling #183

Closed
@sudo-suhas

Description

@sudo-suhas

Here's some code which demonstrates the inconsistency for error message between encoding/json and jsoniter:

package main

import (
	"encoding/json"
	"fmt"

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

type myStruct struct {
	MyField string `json:"myField"`
}

func main() {
	bytes := []byte(`{ "myField": 0 }`)

	var v myStruct
	err := json.Unmarshal(bytes, &v)
	fmt.Printf("Error from encoding/json: \n\t%+v \n", err)

	err = jsoniter.Unmarshal(bytes, &v)
	fmt.Printf("Error from jsoniter.Unmarshal: \n\t%+v \n", err)

	err = jsoniter.ConfigFastest.Unmarshal(bytes, &v)
	fmt.Printf("Error from jsoniter.ConfigFastest.Unmarshal: \n\t%+v \n", err)
}

Output:

Error from encoding/json: 
	json: cannot unmarshal number into Go struct field myStruct.myField of type string 
Error from jsoniter.Unmarshal: 
	main.myStruct: MyField: ReadString: expects " or n, parsing 14 ...yField": 0... at { "myField": 0 } 
Error from jsoniter.ConfigFastest.Unmarshal: 
	main.myStruct: MyField: ReadString: expects " or n, parsing 14 ...yField": 0... at { "myField": 0 }

It would be ideal if I could get the error message same as encoding/json but I am fine with slight difference in the error message. However, the error from jsoniter includes the entire JSON string. In my case this string was quite large and I had difficulty in understanding the source of the problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions