{"action":"created","issue":{"url":"https://api.github.com/repos/manastech/crystal/issues/1965","labels_url":"https://api.github.com/repos/manastech/crystal/issues/1965/labels{/name}","comments_url":"https://api.github.com/repos/manastech/crystal/issues/1965/comments","events_url":"https://api.github.com/repos/manastech/crystal/issues/1965/events","html_url":"https://github.com/manastech/crystal/issues/1965","id":123677128,"number":1965,"title":"Convert from Object to JSON::Type for dynamic nodes in json","user":{"login":"qio-io","id":15605488,"avatar_url":"https://avatars.githubusercontent.com/u/15605488?v=3","gravatar_id":"","url":"https://api.github.com/users/qio-io","html_url":"https://github.com/qio-io","followers_url":"https://api.github.com/users/qio-io/followers","following_url":"https://api.github.com/users/qio-io/following{/other_user}","gists_url":"https://api.github.com/users/qio-io/gists{/gist_id}","starred_url":"https://api.github.com/users/qio-io/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/qio-io/subscriptions","organizations_url":"https://api.github.com/users/qio-io/orgs","repos_url":"https://api.github.com/users/qio-io/repos","events_url":"https://api.github.com/users/qio-io/events{/privacy}","received_events_url":"https://api.github.com/users/qio-io/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-12-23T15:21:26Z","updated_at":"2015-12-23T15:28:58Z","closed_at":null,"body":"With the code below:\r\n\r\n\r\n```crystal\r\nrequire \"json\"\r\n\r\nmodule Test\r\n  # TODO Put your code here\r\n  class Test\r\n    # JSON mapping\r\n    JSON.mapping({\r\n      name: {type: String, nilable: false},\r\n      any: {type: JSON::Any, nilable: true}\r\n    })\r\n  end\r\n\r\n  class SubTest\r\n    # JSON mapping\r\n    JSON.mapping({\r\n      test: {type: String, nilable: false},\r\n      test2: {type: String, nilable: false}\r\n    })\r\n  end\r\n\r\nend\r\n\r\ntest = Test::Test.from_json(%(\r\n  {\r\n    \"name\": \"test\",\r\n    \"any\": [\r\n      {\"hello\": \"new\"}\r\n    ]\r\n  }\r\n  ))\r\n\r\nif test.any\r\n  new_array = Array(Test::SubTest).new\r\n  new_array << Test::SubTest.from_json(%(\r\n      {\"test\": \"1\", \"test2\": \"2\"}\r\n    )\r\n    )\r\n\r\n  test.any = new_array\r\n\r\nend\r\n\r\nputs test.to_json\r\n\r\n```\r\n\r\nHow can i use the mapping macros for creating a new node with different object \"schema\"?.\r\nThis is the error i received when trying to cast/convert the object to JSON::Type.\r\n\r\n```llvm\r\n\r\nError in ./src/test.cr:40: no overload matches 'Test::Test#any=' with types Array(Test::SubTest)\r\nOverloads are:\r\n - Test::Test#any=(_any : JSON::Any | ::Nil)\r\n\r\n  test.any = new_array\r\n       ^~~\r\n\r\n```"},"comment":{"url":"https://api.github.com/repos/manastech/crystal/issues/comments/166924286","html_url":"https://github.com/manastech/crystal/issues/1965#issuecomment-166924286","issue_url":"https://api.github.com/repos/manastech/crystal/issues/1965","id":166924286,"user":{"login":"asterite","id":209371,"avatar_url":"https://avatars.githubusercontent.com/u/209371?v=3","gravatar_id":"","url":"https://api.github.com/users/asterite","html_url":"https://github.com/asterite","followers_url":"https://api.github.com/users/asterite/followers","following_url":"https://api.github.com/users/asterite/following{/other_user}","gists_url":"https://api.github.com/users/asterite/gists{/gist_id}","starred_url":"https://api.github.com/users/asterite/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/asterite/subscriptions","organizations_url":"https://api.github.com/users/asterite/orgs","repos_url":"https://api.github.com/users/asterite/repos","events_url":"https://api.github.com/users/asterite/events{/privacy}","received_events_url":"https://api.github.com/users/asterite/received_events","type":"User","site_admin":false},"created_at":"2015-12-23T15:28:58Z","updated_at":"2015-12-23T15:28:58Z","body":"You can do this:\r\n\r\n```crystal\r\nnew_array = Array(JSON::Type).new\r\nnew_array << JSON.parse(%({\"test\": \"1\", \"test2\": \"2\"})).raw\r\ntest.any = JSON::Any.new(new_array)\r\n```\r\n\r\nThe thing is that `test.any` is of type `JSON::Any`, which is just a struct wrapping a `JSON::Type`, which is just a few types: `Nil | Bool | Int64 | Float64 | String | Array(Type) | Hash(String, Type)`. `Test::SubTest` is not part of that union.\r\n\r\nDepending on what you want to do in your code maybe this can be modelled in a different way.\r\n"}}