Chartjs-JSON cyclic object value when posting data in ChartJS

0👍

This wasn’t trivial at all. As many questions & answers regarding cyclic objects in JSON are posted, none of them are very especific about what the solution is. For example, those which use the replacer parameter of JSON.stringify() don’t work at all (at least in this case), as the data will be loss.

So, for me the solution was dojox.json.href from the Dojo javascript library. Here’s some documentation.

It’s not the whole thing, but here’s some code in case someone need it. First you serialize the object:

require(["dojox/json/ref"], function(){
    let data = { 
            datasets: dojox.json.ref.toJson(...) 
    }
    $.post('/save',
    ...
});

And then you deserialize it:

require(["dojox/json/ref"], function(){
      data = dojox.json.ref.fromJson(...);      
});

You can import the library just like this:

<script src="//ajax.googleapis.com/ajax/libs/dojo/1.13.0/dojo/dojo.js"></script>

This is the only way I’ve been capable to post the JSON object without loosing data.

Hope it helps!

Leave a comment