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

marshal an object with not export self pointer reference will run forever #31

Closed
bronze1man opened this issue Jan 11, 2022 · 4 comments · Fixed by #43
Closed

marshal an object with not export self pointer reference will run forever #31

bronze1man opened this issue Jan 11, 2022 · 4 comments · Fixed by #43

Comments

@bronze1man
Copy link
Contributor

bronze1man commented Jan 11, 2022

package main

import (
	"github.com/hjson/hjson-go"
	"fmt"
)

func main(){
	type Node struct{
		self *Node
	}
	var obj Node
	obj.self = &obj
	b,err:=hjson.Marshal(obj)
	if err!=nil{
		panic(err)
	}
	fmt.Println(string(b))
}

It should ignore all not export field.

bronze1man added a commit to bronze1man/hjson-go that referenced this issue Jan 11, 2022
This was referenced Jan 11, 2022
@trobro
Copy link
Member

trobro commented Jan 13, 2022

Currently hjson-go does marshal all private struct members. Removing that behavior would be a breaking change. I would rather mention in the Readme.md that circular references will cause the Marshal function to loop forever.

@bronze1man
Copy link
Contributor Author

Thanks, I do not know that reflect can read private field value yet.
Ok.I will maintain my version of hjson-go to fit my need.

@trobro
Copy link
Member

trobro commented Jan 14, 2022

I realize now that there is no documentation of the fact that Hjson supports json-annotation of structs. You can choose to skip the field self in the struct like this, which fixes the problem with the infinite loop:

type Node struct{
	self *Node `json:"-"`
}

@bronze1man
Copy link
Contributor Author

The current behavior is not consistent with json.Marshal of such an object.
New user will be surprised by this behavior

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