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

在 struct 中嵌套 jsonvalue.V 之后,json.Marshal 时指定 option 不生效 #29

Closed
Andrew-M-C opened this issue Mar 19, 2024 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@Andrew-M-C
Copy link
Owner

playground:

package main

import (
	"encoding/json"
	"fmt"

	jsonvalue "github.com/Andrew-M-C/go.jsonvalue"
)

type myStruct struct {
	Ext *jsonvalue.V `json:"ext"`
}

func (s myStruct) MarshalJSON() ([]byte, error) {
	w := myStructWrapper(s)
	return jsonvalue.New(w).Marshal(jsonvalue.OptSetSequence())
}

type myStructWrapper myStruct

func main() {
	st := myStruct{
		Ext: jsonvalue.NewObject(),
	}

	st.Ext.SetString("1000").At("A")
	st.Ext.SetString("2000").At("B")

	const expected = `{"ext":{"A":"1000","B":"2000"}}`
	for i := range 2000 {
		b, _ := json.Marshal(st)
		if string(b) != expected {
			fmt.Println(string(b))
			fmt.Println(expected)

			panic(fmt.Sprintf("%d - diff!", i))
		}
	}
}
@Andrew-M-C Andrew-M-C added the bug Something isn't working label Mar 19, 2024
@Andrew-M-C Andrew-M-C self-assigned this Mar 19, 2024
Andrew-M-C added a commit that referenced this issue Mar 22, 2024
Andrew-M-C added a commit that referenced this issue Mar 22, 2024
Andrew-M-C added a commit that referenced this issue Mar 22, 2024
@Andrew-M-C
Copy link
Owner Author

Fixed in v1.3.7.

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

No branches or pull requests

1 participant