Bug in "encoding/json" Marshal(), or am I doing something wrong?

232 views
Skip to first unread message

Dmitri Shuralyov

unread,
Feb 5, 2013, 8:39:18 PM2/5/13
to golan...@googlegroups.com
The following program uses 4+ GB RAM on my machine and doesn't terminate (within a minute; running go1.0.3 64-bit on OS X)... It throws "out of memory" on play.golang.org.

http://play.golang.org/p/vZ4Yw4oz5u

Change "WantOutOfMemory" to false and it will run without problems.

Why isn't it able to encode such a simple struct? s.Lhs[0] is much simpler than s.Rhs[0] which works fine.

Is it a bug?

For future reference, any tips how I can debug this type of problem? Is there a go debugger?

minux

unread,
Feb 5, 2013, 8:49:19 PM2/5/13
to Dmitri Shuralyov, golan...@googlegroups.com
On Wed, Feb 6, 2013 at 9:39 AM, Dmitri Shuralyov <shur...@gmail.com> wrote:
The following program uses 4+ GB RAM on my machine and doesn't terminate (within a minute; running go1.0.3 64-bit on OS X)... It throws "out of memory" on play.golang.org.

http://play.golang.org/p/vZ4Yw4oz5u

Change "WantOutOfMemory" to false and it will run without problems.

Why isn't it able to encode such a simple struct? s.Lhs[0] is much simpler than s.Rhs[0] which works fine.

Is it a bug?
no, it's because s.Lhs[0] is self-referential. 

For future reference, any tips how I can debug this type of problem? Is there a go debugger?

Dmitri Shuralyov

unread,
Feb 5, 2013, 9:31:35 PM2/5/13
to golan...@googlegroups.com, Dmitri Shuralyov
Thanks a lot, spew was really helpful. It's just what I've been looking for!

I now see that s.Lhs[0].(*ast.Ident).Obj was the culprit. It points to a huge struct that references the same Obj once again, so it is self-referential.

On this topic, is there something for printing values in a format which is valid Go code and could be compiled to get the same object back? fmt.Printf's %#v is pretty close, but it doesn't follow pointers through and has a few other quirks.

e.g.
Go code:
x := Lang{Name: "Go", Year: 2009, URL: "http", Inner: &Inner{Field1: "Secret!"}}
GoSyntaxDump(x)

Std Output:
Lang{Name: "Go", Year: 2009, URL: "http", Inner: &Inner{Field1: "Secret!"}}

Günter Zöchbauer

unread,
Feb 6, 2013, 11:57:55 AM2/6/13
to golan...@googlegroups.com, Dmitri Shuralyov
I'm currently working on Json to GO code generation tool.
I build a GO-AST from Json-Source and export it as GO-Code.

As a result it should be possible to Marshal that Json to the generated GO data structures
Is this what you want?
It's not yet ready and it is my first GO project but I would share it if it's finished.

minux

unread,
Feb 6, 2013, 12:05:29 PM2/6/13
to Dmitri Shuralyov, golan...@googlegroups.com
On Wed, Feb 6, 2013 at 10:31 AM, Dmitri Shuralyov <shur...@gmail.com> wrote:
On this topic, is there something for printing values in a format which is valid Go code and could be compiled to get the same object back? fmt.Printf's %#v is pretty close, but it doesn't follow pointers through and has a few other quirks.
not aware of any existing solutions, and some objects in Go is not easily serializable
(for example, function closures, channels)

if you just want to serialize a Go object for latter retrieval, encoding/gob might help.
Reply all
Reply to author
Forward
0 new messages