{"action":"created","issue":{"url":"https://api.github.com/repos/samselikoff/ember-cli-mirage/issues/223","labels_url":"https://api.github.com/repos/samselikoff/ember-cli-mirage/issues/223/labels{/name}","comments_url":"https://api.github.com/repos/samselikoff/ember-cli-mirage/issues/223/comments","events_url":"https://api.github.com/repos/samselikoff/ember-cli-mirage/issues/223/events","html_url":"https://github.com/samselikoff/ember-cli-mirage/issues/223","id":98867264,"number":223,"title":"JSONAPI factory serialization utility","user":{"login":"maxcal","id":765647,"avatar_url":"https://avatars.githubusercontent.com/u/765647?v=3","gravatar_id":"","url":"https://api.github.com/users/maxcal","html_url":"https://github.com/maxcal","followers_url":"https://api.github.com/users/maxcal/followers","following_url":"https://api.github.com/users/maxcal/following{/other_user}","gists_url":"https://api.github.com/users/maxcal/gists{/gist_id}","starred_url":"https://api.github.com/users/maxcal/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/maxcal/subscriptions","organizations_url":"https://api.github.com/users/maxcal/orgs","repos_url":"https://api.github.com/users/maxcal/repos","events_url":"https://api.github.com/users/maxcal/events{/privacy}","received_events_url":"https://api.github.com/users/maxcal/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":7,"created_at":"2015-08-04T01:10:32Z","updated_at":"2015-09-03T18:47:02Z","closed_at":null,"body":"Thanks @samselikoff for your help on stackoverflow. \r\nWhile solving my own problem I came up with a lightweight helper which makes creating JSONAPI responses a lot simpler:\r\n\r\n```JS\r\nimport Ember from 'ember';\r\nexport default Ember.Object.extend({\r\n  serialize(object) {\r\n    let attributes = Ember.copy(object, true);\r\n    delete attributes.id;\r\n    delete attributes.type;\r\n    return {\r\n      id: object.id,\r\n      type: object.type,\r\n      attributes: attributes\r\n    };\r\n  },\r\n  serializeCollection(collection){\r\n    return {\r\n      data: collection.map(u => { return this.serialize(u); })\r\n    };\r\n  }\r\n});\r\n```\r\n\r\nIt basically takes a straight factory object:\r\n```JS\r\n{\r\n  type: \"users\",\r\n  id: 1,\r\n  name: \"Joe\"\r\n}\r\n```\r\nAnd converts the object into a JSONAPI compatible response:\r\n\r\n```JS\r\n{\r\n  type: \"users\",\r\n  id: 1,\r\n  attributes: { \r\n     name: \"Joe\"\r\n  }\r\n}\r\n```\r\nIs this something that fits into Mirage?\r\n\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/samselikoff/ember-cli-mirage/issues/comments/137539848","html_url":"https://github.com/samselikoff/ember-cli-mirage/issues/223#issuecomment-137539848","issue_url":"https://api.github.com/repos/samselikoff/ember-cli-mirage/issues/223","id":137539848,"user":{"login":"maxcal","id":765647,"avatar_url":"https://avatars.githubusercontent.com/u/765647?v=3","gravatar_id":"","url":"https://api.github.com/users/maxcal","html_url":"https://github.com/maxcal","followers_url":"https://api.github.com/users/maxcal/followers","following_url":"https://api.github.com/users/maxcal/following{/other_user}","gists_url":"https://api.github.com/users/maxcal/gists{/gist_id}","starred_url":"https://api.github.com/users/maxcal/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/maxcal/subscriptions","organizations_url":"https://api.github.com/users/maxcal/orgs","repos_url":"https://api.github.com/users/maxcal/repos","events_url":"https://api.github.com/users/maxcal/events{/privacy}","received_events_url":"https://api.github.com/users/maxcal/received_events","type":"User","site_admin":false},"created_at":"2015-09-03T18:47:02Z","updated_at":"2015-09-03T18:47:02Z","body":"Here is an updated version which handles arrays and single objects through the same interfaces:\r\n\r\n```JS\r\nimport Ember from 'ember';\r\n\r\nlet Helper = Ember.Object.extend({\r\n  serialize(obj_or_ary) {\r\n    let _serialize = function(object){\r\n      let attributes = Ember.copy(object, true);\r\n      delete attributes.id;\r\n      delete attributes.type;\r\n      return {\r\n        id: object.id,\r\n        type: object.type,\r\n        attributes: attributes\r\n      };\r\n    }\r\n    if ($.isArray(obj_or_ary)) {\r\n      return { data: obj_or_ary.map(_serialize); }\r\n    } else {\r\n      return { data: _serialize(obj_or_ary); }\r\n    }\r\n  },\r\n  deserialize(serialized){\r\n    let data = serialized.data;\r\n    let compact = function(obj){\r\n      let o = Ember.copy(obj, true);\r\n      let attributes = o.attributes;\r\n      delete o.attributes;\r\n      return $.extend(o, attributes);\r\n    };\r\n    if ($.isArray(data)) {\r\n      return data.map(compact);\r\n    } else {\r\n      return compact(data);\r\n    }\r\n  }\r\n});\r\n\r\nexport default Helper.create();\r\n```"}}