Skip to content
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

One behavior diff with encoding/json #251

Closed
Rainshaw opened this issue Jun 30, 2022 · 4 comments · Fixed by #254
Closed

One behavior diff with encoding/json #251

Rainshaw opened this issue Jun 30, 2022 · 4 comments · Fixed by #254

Comments

@Rainshaw
Copy link

Hello, I'm trying to add sonic support in gin . And I have found that sonic has a different behavior with encoding/json.

when try to use newDecoder().decode func on an empty string, encoding/json would return an EOF error, while sonic return nothing and an empty struct.

As addressed in https://stackoverflow.com/questions/18419428/what-is-the-minimum-valid-json , an empty string is not a valid json string, so could you please make this behavior the same with encoding/json?

@chenzhuoyu
Copy link
Member

@RainshawGao Could you please provide a minimal repro?

@Rainshaw
Copy link
Author

Rainshaw commented Jul 1, 2022

sure!

package main

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

	"github.com/bytedance/sonic"

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

func main() {
	type test struct {
		Name string `json:"name"`
	}
	var data = ``
	test1 := &test{}
	err := json.NewDecoder(bytes.NewBufferString(data)).Decode(test1)
	if err != nil {
		fmt.Printf("std json err: %v\n", err)
	} else {
		fmt.Printf("std json ok: %v\n", test1)
	}

	err = jsoniter.ConfigCompatibleWithStandardLibrary.NewDecoder(bytes.NewBufferString(data)).Decode(test1)
	if err != nil {
		fmt.Printf("josniter err: %v\n", err)
	} else {
		fmt.Printf("josniter ok\n")
	}

	err = sonic.ConfigStd.NewDecoder(bytes.NewBufferString(data)).Decode(test1)
	if err != nil {
		fmt.Printf("sonic err: %v\n", err)
	} else {
		fmt.Printf("sonic ok\n")
	}
}

run this gives

std json err: EOF
josniter err: EOF
sonic ok

@chenzhuoyu
Copy link
Member

@RainshawGao Thanks, we will investigate on this.

@chenzhuoyu
Copy link
Member

@RainshawGao Solved in #254.

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

Successfully merging a pull request may close this issue.

2 participants