6

I hava JSON object :

[#1={id:"2012-05-04", title:"Scheduled", start:(new Date(1336096800000)), source:{events:[#1#], className:[]}, _id:"2012-05-04", _start:(new Date(1336089600000)), end:null, _end:null, allDay:true, className:[]}]

I try to stringify it :

var test = JSON.stringify(resourceVacation, censor(resourceVacation));

function censor(censor) {
    return (function() {
        var i = 0;
        return function(key, value) {
            if (i !== 0 && typeof(censor) === 'object' && typeof(value) == 'object' && censor == value)
                return '[Circular]';

            ++i; // so we know we aren't using the original object anymore

            return value;
        }
    })(censor);
}

I use censor as mentioned here :Chrome sendrequest error: TypeError: Converting circular structure to JSONn

However I get the following exception over the browser:

Uncaught TypeError: Converting circular structure to JSON

Here is the Java Script object: enter image description here

I got the previous JSON object using toSource() at Mozilla browser. Any idea how to fix it !!

============================UPDATE========================

Actually I need to share with you the scnerio from the beginning: 1 -Initially: I have a form and at the end I build java script object which is :

#1=[{id:"2012-05-03", title:"Scheduled", start:(new Date(1336010400000)), source:{events:#1#, className:[]}, _id:"2012-05-03", _start:(new Date(1336003200000)), end:null, _end:null, allDay:true, className:[]}]

This object is stringified normally ... NOTE THAT IT"S typical to the one that would fire exception later.

2- Then later I delete objects from this array using :

function deleteVacation(day) {
    for (var index = 0; index < resourceVacation.length; index++) {
        if (resourceVacation[index].id == day)

            resourceVacation.splice(index,1);
    }

3-When I try to stringify that array after I deleted a single object , I get the mentioned exception. So .. anu ideas why does it passed the first time and failed 2nd time !!

Community
  • 1
  • 1
Echo
  • 2,959
  • 15
  • 52
  • 65
  • Validate your JSON. It seems to be invalid: http://www.jsonlint.org – Rene Pot May 08 '12 at 19:13
  • Actually what I already have is array of object and I used obj.toSource() to get that json . – Echo May 08 '12 at 19:16
  • I have updated my post.Please feed me back if you have any concern. – Echo May 08 '12 at 19:20
  • 1
    There's no such thing as a "JSON object". JSON is a format for *strings*. What you have is just an "object", til it's serialized into valid JSON. – cHao May 08 '12 at 19:27
  • That is a valuable info.Thx cHao. Do you have an idea how could I overcome this issue? – Echo May 08 '12 at 19:33
  • I just had a circular object throw an error because it was referencing itself (a property was a copy of the whole object). – Kevin Beal Apr 08 '13 at 20:36

2 Answers2

8

You can't JSON encode date objects.

From json.org : "A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested."

dontGoPlastic
  • 1,772
  • 14
  • 22
  • 1
    Iam not following you here, can you elaborate more ! – Echo May 08 '12 at 19:27
  • 2
    @Echo: It's pretty simple. Values in JSON are limited in what types they can be. The answer says that dates are not one of those types. If you want a date, one easy way to get around that is to turn the date into a string or number. Depending on what you need it for, one or the other might be better. – cHao May 08 '12 at 19:34
  • Actually, I think this might only be part of the problem. The error (and that other answer you linked to) is saying that you're trying to encode an object that has circular references. I'd try to remove the `start` and `_start` values, and if that doesn't fix it, remove the arrays or object properties. If that fixes it, check to make sure that either of the arrays or objects you removed don't reference anything up 'higher in the chain'. – dontGoPlastic May 08 '12 at 19:53
  • 1
    Hmm, I'm not too sure. Naming a function `delete` is kind of scary, though. If you're using Chrome, I'd try the 'pause on uncaught exceptions' feature (click the stop sign/pause icon in the js debugging tools until it's purple). When the error gets thrown, it should break and give you a chance to see the current state of your program. – dontGoPlastic May 08 '12 at 21:37
  • Frankly this is a new feature I just know.Thx for sharing it with me.Regarding the state of my program before the exception,it is as I have mentioned .What driving me to nuts that I can add objects to this array and it stringifies normally.The issues just appears when I delete an object from the array.Please if you have any assumptions into you mind,just tell me. – Echo May 08 '12 at 23:35
  • It sounds to me that the splice is a red herring, but I can't really say for certain with the info we have. Is there a test page up anywhere that we can check out? – dontGoPlastic May 09 '12 at 15:25
  • I've found out the issue which was related to a JQuery plugin I was using.Frankly,I'm not very sure the cause of this problem yet but as the exception states that the object I try to stringify is self referencing itself.... Thanks for your assistance. – Echo May 14 '12 at 09:34
8

The problem is the source - object which is a circular reference.

You should create a copy of the object without the source object.

That's how I solved the issue in FullCalendar.

Community
  • 1
  • 1
user2102131
  • 81
  • 1
  • 1