{"action":"created","issue":{"url":"https://api.github.com/repos/jsog/jsog/issues/11","labels_url":"https://api.github.com/repos/jsog/jsog/issues/11/labels{/name}","comments_url":"https://api.github.com/repos/jsog/jsog/issues/11/comments","events_url":"https://api.github.com/repos/jsog/jsog/issues/11/events","html_url":"https://github.com/jsog/jsog/issues/11","id":117168090,"number":11,"title":"Calling JSOG.decode on a JSOG-encoded object with circular references twice causes a stack overflow","user":{"login":"moodysalem","id":7897876,"avatar_url":"https://avatars.githubusercontent.com/u/7897876?v=3","gravatar_id":"","url":"https://api.github.com/users/moodysalem","html_url":"https://github.com/moodysalem","followers_url":"https://api.github.com/users/moodysalem/followers","following_url":"https://api.github.com/users/moodysalem/following{/other_user}","gists_url":"https://api.github.com/users/moodysalem/gists{/gist_id}","starred_url":"https://api.github.com/users/moodysalem/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/moodysalem/subscriptions","organizations_url":"https://api.github.com/users/moodysalem/orgs","repos_url":"https://api.github.com/users/moodysalem/repos","events_url":"https://api.github.com/users/moodysalem/events{/privacy}","received_events_url":"https://api.github.com/users/moodysalem/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-11-16T16:52:49Z","updated_at":"2015-11-16T19:27:39Z","closed_at":null,"body":"Background:\r\nI'm using backbone models and collections with JSOG. I defined the parse method on the backbone models and collections to use JSOG to parse server responses. If you define the parse method on both the collection and the model, the collection parse gets called on the array received from the server, and the model parse gets called on each object in the parsed collection.\r\n\r\nWith the first decode, a JSON object with circular references is created. With the second decode, a stack trace exception occurs due to the circular references.\r\n\r\nI fixed this by adding a key to every decoded object, __jsogObjectDecoded, and checking for the absence of the key before attempting to decode the object.\r\n\r\n      decodeObject = function (encoded) {\r\n        var id, key, ref, result, value;\r\n        if (encoded[ JSOG_OBJECT_DECODED ] === true) {\r\n          return encoded;\r\n        }\r\n        ref = encoded[ '@ref' ];\r\n        if (ref != null) {\r\n          ref = ref.toString();\r\n        }\r\n        if (ref != null) {\r\n          return found[ ref ];\r\n        }\r\n        result = {};\r\n        id = encoded[ '@id' ];\r\n        if (id != null) {\r\n          id = id.toString();\r\n        }\r\n        if (id) {\r\n          found[ id ] = result;\r\n        }\r\n        for (key in encoded) {\r\n          value = encoded[ key ];\r\n          if (key !== '@id') {\r\n            result[ key ] = doDecode(value);\r\n          }\r\n        }\r\n        result[ JSOG_OBJECT_DECODED ] = true;\r\n        return result;\r\n      };"},"comment":{"url":"https://api.github.com/repos/jsog/jsog/issues/comments/157143765","html_url":"https://github.com/jsog/jsog/issues/11#issuecomment-157143765","issue_url":"https://api.github.com/repos/jsog/jsog/issues/11","id":157143765,"user":{"login":"moodysalem","id":7897876,"avatar_url":"https://avatars.githubusercontent.com/u/7897876?v=3","gravatar_id":"","url":"https://api.github.com/users/moodysalem","html_url":"https://github.com/moodysalem","followers_url":"https://api.github.com/users/moodysalem/followers","following_url":"https://api.github.com/users/moodysalem/following{/other_user}","gists_url":"https://api.github.com/users/moodysalem/gists{/gist_id}","starred_url":"https://api.github.com/users/moodysalem/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/moodysalem/subscriptions","organizations_url":"https://api.github.com/users/moodysalem/orgs","repos_url":"https://api.github.com/users/moodysalem/repos","events_url":"https://api.github.com/users/moodysalem/events{/privacy}","received_events_url":"https://api.github.com/users/moodysalem/received_events","type":"User","site_admin":false},"created_at":"2015-11-16T19:27:39Z","updated_at":"2015-11-16T19:27:39Z","body":"I think it's a nice feature for the functions in the library to be idempotent out of the box."}}