1

I want to convert the html tag objects to json object in the javascript in order to send them to the server from the javascript. As i have to save these objects at the Ruby on Rails server. These HTML objects is the canvas tag object and the graphics objects created using CAKE API. I have used the stringify function but it is not working. Here is my code:

window.onload=function()
{
    var CAKECanvas = new Canvas(document.body, 1000,1000);
    var canvas=CAKECanvas.canvas;
    var text=document.createElement('textarea');
    text.id="text";
    text.rows="100";
    text.cols="200";
    document.body.appendChild(text);
    canvas.style.borderStyle="solid";
    canvas.style.borderColor="black";
var rect= new Circle();
    rect.radius=100;
    rect.centered=true;
    rect.cx=Math.random() * 500;
    rect.cy= Math.random() * 300;
    rect.stroke= false;
    rect.fill= "red";
    rect.xDir = Math.random() > 0.5?1:-1;
rect.yDir = Math.random() > 0.5?1:-1;
    var obj=new Object;
    var count = 0,k;
    for (k in rect)
        {
            if (rect.hasOwnProperty(k))
                {
                    count++;
                    obj[k]=rect[k];
                }
        }
    alert(count);
rect.addFrameListener(function(t, dt)
    {
                this.cx += this.xDir * 50 * dt/1000;
        this.cy += this.yDir * 50 * dt/1000;
        if (this.cx > 550)
        {
            this.xDir = -1;
        }
        if (this.cx < 50)
        {
            this.xDir = 1;
        }
        if (this.cy > 350)
        {
            this.yDir = -1;
        }
        if (this.cy < 50)
        {
            this.yDir = 1;
        }
    }
);

CAKECanvas.append(rect);
    var carAsJSON = JSON.stringify(obj); /////////////////NOT CONVERTING THE OBJECT OBJ     INTO JSON OBJECT
}
7
  • Do you have an example page? Did you include json2.js in your HTML? What's the error? Commented May 23, 2010 at 11:37
  • @Marcel: there is no example page. stringify function is not working. It is converting it to json object. No I have not include json2,js Commented May 23, 2010 at 11:48
  • But what's the error? If JSON.stringify is not defined somewhere (e.g., in json2.js or a native browser's implementation), it won't work. Commented May 23, 2010 at 11:50
  • @Marcel: I have included the json2.js but is not converting the object obj into json object. Commented May 23, 2010 at 11:58
  • When I try to run your code from within an empty HTML page, I already get an error on line 3 (new Canvas): “Canvas is not defined”. Commented May 23, 2010 at 12:00

1 Answer 1

1

Only primitive values (strings, dates, booleans, numbers) and objects and array structures are possible to serialize into JSON. This mean that other host-objects like RegExp or Canvas cannot be serialized.

In short, JSON is limited to data ('information').

So, you will either have to save the created markup using .innerHTML, or save the data so that it can be recreated.

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

7 Comments

@Sean: Can I directly send javascript objects to server. if it is then how.
No, you have to pass it using a format that is common to both the client- and the server-side programming environments. As of now, that is JSON. See json.org for the description of what JSON is.
@cooldude: But, as Sean already said, you can't stringify anything else than data (so, not functions or HTML elements).
No, javascript objects are just that, objects. They live in the memory of the client just as serverside objects live in the memory of the server. These objects has nothing in common except for the notion of what an 'object' is and so to 'pass' an object from one to the other, these have to agree on some sort of common language. this language is JSON. The sender has to serialize (stringify) the object, and the recipient has to deserialize (parse) it.
Think of it as moving a chair over the internet. You cannot transfer the real chair, but you can transfer the characteristics (type of material, height, width, length etc...) of the chair so that it can be rebuilt on the other end. That is what serializing/deserializing is about.
|

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.