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

Defferent behavior from "encoding/json" for MarshalJSON #370

Closed
hidechae opened this issue May 13, 2022 · 0 comments · Fixed by #379
Closed

Defferent behavior from "encoding/json" for MarshalJSON #370

hidechae opened this issue May 13, 2022 · 0 comments · Fixed by #379
Labels
bug Something isn't working

Comments

@hidechae
Copy link

package main

import (
	"database/sql"
	"encoding/json"
	"fmt"
	goccyjson "github.com/goccy/go-json"
)

func main() {
	v := []struct{
		NS NullString
	}{
		{
			NS: NullString{String:"test", Valid: true},
		},
	}

	got, _ := json.Marshal(v)
	fmt.Println(string(got)) // output: [{"NS":"test"}]

	got, _ = goccyjson.Marshal(v)
	fmt.Println(string(got)) // output: [{"NS":{"String":"test","Valid":true}}]
}

type NullString sql.NullString

func (s *NullString) MarshalJSON() ([]byte, error) {
	if !s.Valid {
		return json.Marshal(nil)
	}
	return json.Marshal(s.String)
}

When I change (s *NullString) to (s NullString), it's fine. (and maybe I should do so.)
So, it is not a critical issue, but I would prefer that compatibility be supported.

@goccy goccy added the bug Something isn't working label Jun 29, 2022
orisano added a commit to orisano/go-json that referenced this issue Jul 4, 2022
orisano added a commit to orisano/go-json that referenced this issue Jul 6, 2022
@goccy goccy closed this as completed in #379 Jul 7, 2022
goccy added a commit that referenced this issue Jul 7, 2022
Fix slice/array type encoding with types implementing MarshalJSON
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants