{"action":"created","issue":{"url":"https://api.github.com/repos/sylvainpolletvillard/ObjectModel/issues/25","repository_url":"https://api.github.com/repos/sylvainpolletvillard/ObjectModel","labels_url":"https://api.github.com/repos/sylvainpolletvillard/ObjectModel/issues/25/labels{/name}","comments_url":"https://api.github.com/repos/sylvainpolletvillard/ObjectModel/issues/25/comments","events_url":"https://api.github.com/repos/sylvainpolletvillard/ObjectModel/issues/25/events","html_url":"https://github.com/sylvainpolletvillard/ObjectModel/issues/25","id":174093970,"number":25,"title":"Serialization and deserialization arrays of objects","user":{"login":"vbarbarosh","id":352973,"avatar_url":"https://avatars.githubusercontent.com/u/352973?v=3","gravatar_id":"","url":"https://api.github.com/users/vbarbarosh","html_url":"https://github.com/vbarbarosh","followers_url":"https://api.github.com/users/vbarbarosh/followers","following_url":"https://api.github.com/users/vbarbarosh/following{/other_user}","gists_url":"https://api.github.com/users/vbarbarosh/gists{/gist_id}","starred_url":"https://api.github.com/users/vbarbarosh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vbarbarosh/subscriptions","organizations_url":"https://api.github.com/users/vbarbarosh/orgs","repos_url":"https://api.github.com/users/vbarbarosh/repos","events_url":"https://api.github.com/users/vbarbarosh/events{/privacy}","received_events_url":"https://api.github.com/users/vbarbarosh/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-08-30T18:42:02Z","updated_at":"2016-08-30T19:33:44Z","closed_at":null,"body":"Suppose I have a container for different types. How can I save this container into JSON and then restore it, so, that its values will be of the same type as it was before serialization?\r\n\r\n```javascript\r\nvar Model = require('objectmodel');\r\n\r\nvar Type1 = Model({\r\n    title: String,\r\n    contents: String\r\n}).defaults({title: 'title 1', contents: 'contents 1'});\r\n\r\nvar Type2 = Model({\r\n    title: String,\r\n    contents: String\r\n}).defaults({title: 'title 2', contents: 'contents 2'});\r\n\r\nvar Container = Model({\r\n    items: Model.Array(Type1, Type2)\r\n}).defaults({items: []});\r\n\r\nvar a = new Container();\r\n\r\na.items.push(new Type1, new Type2);\r\n\r\nconsole.log(JSON.stringify(a));\r\n```\r\n\r\nIn the example above I intentionally made two types containing the same fields. Duck typing cannot work here. But, what is the solution?"},"comment":{"url":"https://api.github.com/repos/sylvainpolletvillard/ObjectModel/issues/comments/243553293","html_url":"https://github.com/sylvainpolletvillard/ObjectModel/issues/25#issuecomment-243553293","issue_url":"https://api.github.com/repos/sylvainpolletvillard/ObjectModel/issues/25","id":243553293,"user":{"login":"sylvainpolletvillard","id":566536,"avatar_url":"https://avatars.githubusercontent.com/u/566536?v=3","gravatar_id":"","url":"https://api.github.com/users/sylvainpolletvillard","html_url":"https://github.com/sylvainpolletvillard","followers_url":"https://api.github.com/users/sylvainpolletvillard/followers","following_url":"https://api.github.com/users/sylvainpolletvillard/following{/other_user}","gists_url":"https://api.github.com/users/sylvainpolletvillard/gists{/gist_id}","starred_url":"https://api.github.com/users/sylvainpolletvillard/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sylvainpolletvillard/subscriptions","organizations_url":"https://api.github.com/users/sylvainpolletvillard/orgs","repos_url":"https://api.github.com/users/sylvainpolletvillard/repos","events_url":"https://api.github.com/users/sylvainpolletvillard/events{/privacy}","received_events_url":"https://api.github.com/users/sylvainpolletvillard/received_events","type":"User","site_admin":false},"created_at":"2016-08-30T19:33:44Z","updated_at":"2016-08-30T19:33:44Z","body":"Serializing in JSON necessarily implies that you lose the type information, except if you store it manually with your data, then retrieve it with a custom parsing function. Automatic model casting does not work with union types at the moment as seen in #24 , so the Container instance will not try to cast a plain object into either Type1 or Type2.\r\n\r\nIf I add support for automatic model casting with union types, this is the kind of edge cases that can become very tricky. My first idea would be check every type until the object matches the definition. In this example, this would result in an array full of Type1, which is probably not desirable. How do you think model casting should behave here ?"}}
{"actor":{"display_login":"sylvainpolletvillard"}}