ricks
ricks

Reputation: 3324

Javascript keeps adding backslashes when using stringify

I just upgraded to Ruby 5.1.4 and some of my previously working javascript code stopped working and i cant seem to figure out why.

Every time i set the properties of an object like this

    FLOW.nodes.forEach( function( node ) {
    _nodes.push({
        "step_type":            node.type,
        "internal_id":          node.getId({ useMap: useMap }),
        "step_attributes" : {
            "shape":                node.shape,
            "icon":                 node.icon,
            "title":                node.title,
            "subtitle":             node.subTitle,
            "connectors":           JSON.stringify( node.connectors ),
            "tags":                 JSON.stringify( node.tags ),
            "allowed_connections":  JSON.stringify( node.allowedConnections ),
            "position":             JSON.stringify( node.position ),
            "configured":           node.configured,
            "socket":               JSON.stringify( node.socket ),
            "buttons":              JSON.stringify( node.buttons ),
            "color":                node.color
        },
        "properties":            JSON.stringify( FLOW.getNodeProperties( node.getId() ) )
    });
});

every-time i save it keeps adding more backslashes to properties and i cant figure out why

here is what it prints out like

"properties": "\"{\\\"tags\\\": \\\"erewerwre\\\",\\\"tagged_present\\\": true,\\\"tagged_future\\\": false,\\\"tagged_present_future\\\": false,\\\"remove_from_other_flows\\\": false}\""

If it runs through there again it will keep adding more backslashes.

any idea how i can fix this?

Upvotes: 1

Views: 239

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074969

Looking at the code and result, FLOW.getNodeProperties is returning a string containing JSON. By applying JSON.stringify to it, you're encoding it a second time, e.g.:

function getNodeProperties() {
  return JSON.stringify({some: "property"});
}
var result = {
  properties: JSON.stringify(getNodeProperties())
};
console.log(result.properties);

Just remove your JSON.stringify call:

"properties": FLOW.getNodeProperties( node.getId() )

Upvotes: 1

Related Questions