Skip to content

Compatibility issue with native JSON encoder: failed to convert null of string field to pointer of integer #478

Closed
@zhenchangxc

Description

@zhenchangxc

The below code works well with native encoder but not the json-iterator. When running with json-iterator, similar error will be returned when trying to unmarshal null into integer pointer:

stringModeNumberDecoder: expect ", but found n, ......


import (
	"encoding/json"
	"fmt"
)

type Adhoc struct {
	ID *int64 `json:"id,string"`
}

func main() {
	fmt.Println("Hello, playground")
	target := &Adhoc{}
	if err := json.Unmarshal([]byte("{\"any\": 123, \"id\": null}"), target); err != nil {
		fmt.Print(err)
		return
	}
	fmt.Println(target.ID)
	if err := json.Unmarshal([]byte(`{"any": null}`), target); err != nil {
		fmt.Print(err)
		return
	}
	fmt.Println(target.ID)
	if err := json.Unmarshal([]byte(`{"id": "123"}`), target); err != nil {
		fmt.Print(err)
		return
	}
	fmt.Println(*target.ID)
}

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