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
Closed

Comments

@wzshiming
Copy link

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
Copy link
Contributor

crvv 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
Copy link
Author

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

@crvv
Copy link
Contributor

crvv 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
Copy link
Author

I see. Thank you

@golang golang locked and limited conversation to collaborators Jun 11, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants