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

encoding/json: Data referenced in the loop cannot be processed correctly #25818

Closed
wzshiming opened this issue Jun 11, 2018 · 4 comments

Comments

Projects
None yet
2 participants
@wzshiming
Copy link

commented Jun 11, 2018

I think this should be a problem, but I don't know how to solve it

package main

import (
	"encoding/json"
)

type A struct {
	P *A
}

func main() {
	a := &A{}
	a.P = a
	json.Marshal(a)
	// runtime: goroutine stack exceeds 1000000000-byte limit
	// fatal error: stack overflow
}
@crvv

This comment has been minimized.

Copy link
Contributor

commented Jun 11, 2018

https://golang.org/pkg/encoding/json/

JSON cannot represent cyclic data structures and Marshal does not handle them. Passing cyclic structures to Marshal will result in an infinite recursion.

Please read https://stackoverflow.com/questions/3010357/define-cyclic-data-structures

@wzshiming

This comment has been minimized.

Copy link
Author

commented Jun 11, 2018

I know that's the reason, but I wonder if this needs to be dealt with

@crvv

This comment has been minimized.

Copy link
Contributor

commented Jun 11, 2018

The document says "Passing cyclic structures to Marshal will result in an infinite recursion".
And the behavior is correct according to the document.
So there isn't any bug here.

@wzshiming

This comment has been minimized.

Copy link
Author

commented Jun 11, 2018

I see. Thank you

@wzshiming wzshiming closed this Jun 11, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.