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 !!

6
  • Validate your JSON. It seems to be invalid: jsonlint.org Commented May 8, 2012 at 19:13
  • Actually what I already have is array of object and I used obj.toSource() to get that json . Commented May 8, 2012 at 19:16
  • I have updated my post.Please feed me back if you have any concern. Commented May 8, 2012 at 19:20
  • 1
    <pedantry> 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. </pedantry> Commented May 8, 2012 at 19:27
  • That is a valuable info.Thx cHao. Do you have an idea how could I overcome this issue? Commented May 8, 2012 at 19:33

2 Answers 2

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."

Sign up to request clarification or add additional context in comments.

7 Comments

Iam not following you here, can you elaborate more !
@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.
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'.
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.
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.
|
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.

1 Comment

is there anyone that can explain me how to achive this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.