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

Bug: struct with map (the only one field) encoded to null #376

Closed
trim21 opened this issue Jul 2, 2022 · 4 comments · Fixed by #377
Closed

Bug: struct with map (the only one field) encoded to null #376

trim21 opened this issue Jul 2, 2022 · 4 comments · Fixed by #377

Comments

@trim21
Copy link
Contributor

trim21 commented Jul 2, 2022

package main

import (
	"fmt"
	"testing"

	"github.com/goccy/go-json"
)

func main() {
	type Container struct {
		V any `json:"value"`
	}
	type MapOnly struct {
		Map map[string]int64 `json:"map"`
	}

	b, err := json.Marshal(Container{MapOnly{}})
	if err != nil {
		panic(err)
	}

	fmt.Println(string(b))
}

output: {"value":null}

@trim21 trim21 changed the title Struct with map encoded to null Bug: struct with map encoded to null Jul 2, 2022
@trim21 trim21 changed the title Bug: struct with map encoded to null Bug: struct with map (only field) encoded to null Jul 2, 2022
@trim21 trim21 changed the title Bug: struct with map (only field) encoded to null Bug: struct with map (the only one field) encoded to null Jul 2, 2022
@orisano
Copy link
Contributor

orisano commented Jul 2, 2022

what is your expectation?

I could not reproduce this problem in v0.9.8.

func TestIssue376(t *testing.T) {
	type Container struct {
		V any `json:"value"`
	}
	type MapOnly struct {
		b   bool
		Map map[string]int64 `json:"map"`
	}
	b, err := json.Marshal(Container{MapOnly{}})
	if err != nil {
		t.Fatal(err)
	}
	got := string(b)
	expected := `{"value":{"map":null}}`
	if got != expected {
		t.Errorf("unexpected result: %v != %v", got, expected)
	}
}

what version did you use?

@trim21
Copy link
Contributor Author

trim21 commented Jul 2, 2022

Oh, sorry, I'm using 0.9.7.

This bug behavior differently in 0.9.8, try this: (remove MapOnly{}.b)

package main

import (
	"fmt"

	"github.com/goccy/go-json"
)

func main() {
	type Container struct {
		V any `json:"value"`
	}
	type MapOnly struct {
		Map map[string]int64 `json:"map"`
	}

	b, err := json.Marshal(Container{MapOnly{}})
	if err != nil {
		panic(err)
	}

	fmt.Println(string(b))
}

@trim21
Copy link
Contributor Author

trim21 commented Jul 2, 2022

@orisano can you re-produce with new example?

I just try this on master branch and it fail.

func TestIssue376(t *testing.T) {
	type Container struct {
		V interface{} `json:"value"`
	}
	type MapOnly struct {
		Map map[string]int64 `json:"map"`
	}
	b, err := json.Marshal(Container{MapOnly{}})
	if err != nil {
		t.Fatal(err)
	}
	got := string(b)
	expected := `{"value":{"map":null}}`
	if got != expected {
		t.Errorf("unexpected result: %v != %v", got, expected)
	}
}

@orisano
Copy link
Contributor

orisano commented Jul 2, 2022

thanks, I reproduced.

orisano added a commit to orisano/go-json that referenced this issue Jul 2, 2022
@goccy goccy closed this as completed in #377 Jul 4, 2022
goccy added a commit that referenced this issue Jul 4, 2022
Fix encoding of directed interface with typed nil
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