Proper Object Graphs in JSON

I look into this every five years or so, and I get pretty disappointed whenever I check the current support for serializing and deserializing object graphs with proper reference handling (including circular refs) to and from JSON (or another suitable format, I guess …).

In Go’s builtin JSON encoder, you get a stack overflow:

I’m writing this application now that has a potentially very deep structure with lots of nodes. I have made some “glue code” on the client (Javascript) to keep track of both the references and the identities to the associations, so if I make a change related to a node on, say, level 3 – I only need to re-render 3 nodes (even if the tree has thousands, ReactJS is great for this).

This works, but gets ugly pretty fast.

I’m kind of surprised that this isn’t a standard in 2016. Am I missing something?

Search for data-formats that natively support circular references. I.e. pretty easy to find:

package main

import (


type Node struct {
	ID   string
	Next *Node

func main() {
	send := &Node{"Hello", nil}
	send.Next = send

	data, err := sereal.Marshal(send)
	if err != nil {

	recv := &Node{}
	err = sereal.Unmarshal(data, &recv)
	if err != nil {

	fmt.Printf("%p := %#v\n", send, send)
	fmt.Printf("%p := %#v\n", recv, recv)

Alternatively, just write your own encoding/schema for circular references – e.g. something that encodes to JSON, but flattens everything.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.