Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
zhenchangxc opened this issue Jul 20, 2020 · 4 comments

Comments

@zhenchangxc
Copy link

zhenchangxc commented Jul 20, 2020

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)
}
@zhenchangxc zhenchangxc changed the title Compatibility issue with native JSON encoder: failed to convert string field value null to pointer of integer Compatibility issue with native JSON encoder: failed to convert null of string field to pointer of integer Jul 20, 2020
@nikolaydubina
Copy link
Contributor

nikolaydubina commented Jul 20, 2020

Based on example above, here is example when jsoniter fails. Playground.

package main

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

var ji = jsoniter.ConfigCompatibleWithStandardLibrary

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

func main() {
	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)
	
	// jsoniter
	if err := ji.Unmarshal([]byte("{\"any\": 123, \"id\": null}"), target); err != nil {
		fmt.Print(err)
		return
	}
	fmt.Println(target.ID)
	if err := ji.Unmarshal([]byte(`{"any": null}`), target); err != nil {
		fmt.Print(err)
		return
	}
	fmt.Println(target.ID)
	if err := ji.Unmarshal([]byte(`{"id": "123"}`), target); err != nil {
		fmt.Print(err)
		return
	}
	fmt.Println(*target.ID)
}

@nikolaydubina
Copy link
Contributor

Output error,
image

@nikolaydubina
Copy link
Contributor

cc: @taowen

@nikolaydubina
Copy link
Contributor

@zhenchangxc feel free to try again ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants