{"action":"created","comment":{"url":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/comments/43512281","id":43512281,"diff_hunk":"@@ -0,0 +1,120 @@\n+module ActiveModel\n+  class Serializer\n+    module Adapter\n+      class JsonApi\n+        module Deserialization\n+          module_function\n+\n+          # Parse a Hash or ActionController::Parameters representing a JSON API document\n+          # into an ActiveRecord-ready hash.\n+          #\n+          # @param [Hash|Object implementing `to_h`] document\n+          # @param [Hash] options\n+          #   fields: Array of symbols and a Hash. Specify whitelisted fields, optionally\n+          #     specifying the attribute name on the model.\n+          # @return [Hash] ActiveRecord-ready hash\n+          #\n+          def parse(document, options = {})\n+            fields = parse_fields(options[:fields])\n+\n+            hash = {}\n+\n+            document = document.to_h unless document.is_a?(Hash)\n+            primary_data = document.fetch('data', {})\n+            included_data = document.fetch('included', {})\n+\n+            hash.merge!(parse_primary_data(primary_data, fields))\n+            hash.merge!(parse_included_data(included_data, fields))\n+\n+            hash\n+          end\n+\n+          def parse_included_data(included_data, fields)\n+            data = {}\n+            included_data.each do |included_resource|\n+              parsed = parse_primary_data(included_resource, fields)\n+              # parsed should only have one key\n+              # so there is no possibility of missing other keys when\n+              # calling first\n+              # NOTE: key is always going to be singular\n+              key = parsed.keys.first\n+              plural_key = key.pluralize\n+              parsed_attributes = parsed[key]\n+              # on multiple iterations of parsing the same type\n+              # the key in data is changed to be plural,\n+              # so we need to check for the existence of both\n+              # singular and plural keys\n+              exists = data.keys.include?(key) || data.keys.include?(plural_key)\n+\n+              if exists\n+                old_data = data[key] || data[plural_key]\n+                if old_data.is_a?(Hash)\n+                  data.delete(key)\n+                  data[plural_key] = [old_data, parsed_attributes]\n+                elsif old_data.is_a?(Array)\n+                  data[plural_key] << parsed_attributes\n+                end\n+              else\n+                data.merge! parsed\n+              end\n+            end\n+\n+            puts '--------------------------'\n+            puts data\n+            puts '----------------------------'","path":"lib/active_model/serializer/adapter/json_api/deserialization.rb","position":64,"original_position":64,"commit_id":"2e86ef31868c445fb537fd1cd348d9ac8605f470","original_commit_id":"2e86ef31868c445fb537fd1cd348d9ac8605f470","user":{"login":"NullVoxPopuli","id":199018,"avatar_url":"https://avatars.githubusercontent.com/u/199018?v=3","gravatar_id":"","url":"https://api.github.com/users/NullVoxPopuli","html_url":"https://github.com/NullVoxPopuli","followers_url":"https://api.github.com/users/NullVoxPopuli/followers","following_url":"https://api.github.com/users/NullVoxPopuli/following{/other_user}","gists_url":"https://api.github.com/users/NullVoxPopuli/gists{/gist_id}","starred_url":"https://api.github.com/users/NullVoxPopuli/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/NullVoxPopuli/subscriptions","organizations_url":"https://api.github.com/users/NullVoxPopuli/orgs","repos_url":"https://api.github.com/users/NullVoxPopuli/repos","events_url":"https://api.github.com/users/NullVoxPopuli/events{/privacy}","received_events_url":"https://api.github.com/users/NullVoxPopuli/received_events","type":"User","site_admin":false},"body":":-)\r\n\r\nyeah, I don't think I'm going to make any updates to this until the deserialization PR gets merged.\r\nThen I can apply the suggestions @beauby had","created_at":"2015-10-30T15:08:08Z","updated_at":"2015-10-30T15:08:08Z","html_url":"https://github.com/rails-api/active_model_serializers/pull/1198#discussion_r43512281","pull_request_url":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/1198","_links":{"self":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/comments/43512281"},"html":{"href":"https://github.com/rails-api/active_model_serializers/pull/1198#discussion_r43512281"},"pull_request":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/1198"}}},"pull_request":{"url":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/1198","id":45969085,"html_url":"https://github.com/rails-api/active_model_serializers/pull/1198","diff_url":"https://github.com/rails-api/active_model_serializers/pull/1198.diff","patch_url":"https://github.com/rails-api/active_model_serializers/pull/1198.patch","issue_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1198","number":1198,"state":"open","locked":false,"title":"Sideloading Wrapper for the Attributes/JSON Adapters","user":{"login":"NullVoxPopuli","id":199018,"avatar_url":"https://avatars.githubusercontent.com/u/199018?v=3","gravatar_id":"","url":"https://api.github.com/users/NullVoxPopuli","html_url":"https://github.com/NullVoxPopuli","followers_url":"https://api.github.com/users/NullVoxPopuli/followers","following_url":"https://api.github.com/users/NullVoxPopuli/following{/other_user}","gists_url":"https://api.github.com/users/NullVoxPopuli/gists{/gist_id}","starred_url":"https://api.github.com/users/NullVoxPopuli/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/NullVoxPopuli/subscriptions","organizations_url":"https://api.github.com/users/NullVoxPopuli/orgs","repos_url":"https://api.github.com/users/NullVoxPopuli/repos","events_url":"https://api.github.com/users/NullVoxPopuli/events{/privacy}","received_events_url":"https://api.github.com/users/NullVoxPopuli/received_events","type":"User","site_admin":false},"body":"Related: #1084 #1107 #1088 \r\n\r\nSo, in order to preserve functionality implemented in the attributes/json adapters, I decided to still use the attributes adapter to handle the rendering of the hash/json (so we get all the features (include, etc)).\r\n\r\nIt is a little more overhead than sideloading while iterating over the associations, but I think this could be the least likely to produce bugs, as it relies on pre-existing, thoroughly tested code.\r\n\r\nthe purpose is for those who for some reason can't use json-api, and need sideloaded data (for example, in an ember app - which doesn't like nested relationships)","created_at":"2015-09-25T01:33:45Z","updated_at":"2015-10-30T15:08:08Z","closed_at":null,"merged_at":null,"merge_commit_sha":"c816dab269c1d3b663fa4b4324e04fffcbec8a1a","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/1198/commits","review_comments_url":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/1198/comments","review_comment_url":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/comments{/number}","comments_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1198/comments","statuses_url":"https://api.github.com/repos/rails-api/active_model_serializers/statuses/2e86ef31868c445fb537fd1cd348d9ac8605f470","head":{"label":"NullVoxPopuli:sideloaded-json","ref":"sideloaded-json","sha":"2e86ef31868c445fb537fd1cd348d9ac8605f470","user":{"login":"NullVoxPopuli","id":199018,"avatar_url":"https://avatars.githubusercontent.com/u/199018?v=3","gravatar_id":"","url":"https://api.github.com/users/NullVoxPopuli","html_url":"https://github.com/NullVoxPopuli","followers_url":"https://api.github.com/users/NullVoxPopuli/followers","following_url":"https://api.github.com/users/NullVoxPopuli/following{/other_user}","gists_url":"https://api.github.com/users/NullVoxPopuli/gists{/gist_id}","starred_url":"https://api.github.com/users/NullVoxPopuli/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/NullVoxPopuli/subscriptions","organizations_url":"https://api.github.com/users/NullVoxPopuli/orgs","repos_url":"https://api.github.com/users/NullVoxPopuli/repos","events_url":"https://api.github.com/users/NullVoxPopuli/events{/privacy}","received_events_url":"https://api.github.com/users/NullVoxPopuli/received_events","type":"User","site_admin":false},"repo":{"id":25101596,"name":"active_model_serializers","full_name":"NullVoxPopuli/active_model_serializers","owner":{"login":"NullVoxPopuli","id":199018,"avatar_url":"https://avatars.githubusercontent.com/u/199018?v=3","gravatar_id":"","url":"https://api.github.com/users/NullVoxPopuli","html_url":"https://github.com/NullVoxPopuli","followers_url":"https://api.github.com/users/NullVoxPopuli/followers","following_url":"https://api.github.com/users/NullVoxPopuli/following{/other_user}","gists_url":"https://api.github.com/users/NullVoxPopuli/gists{/gist_id}","starred_url":"https://api.github.com/users/NullVoxPopuli/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/NullVoxPopuli/subscriptions","organizations_url":"https://api.github.com/users/NullVoxPopuli/orgs","repos_url":"https://api.github.com/users/NullVoxPopuli/repos","events_url":"https://api.github.com/users/NullVoxPopuli/events{/privacy}","received_events_url":"https://api.github.com/users/NullVoxPopuli/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/NullVoxPopuli/active_model_serializers","description":"ActiveModel::Serializer implementation and Rails hooks","fork":true,"url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers","forks_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/forks","keys_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/keys{/key_id}","collaborators_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/teams","hooks_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/hooks","issue_events_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/issues/events{/number}","events_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/events","assignees_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/assignees{/user}","branches_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/branches{/branch}","tags_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/tags","blobs_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/git/refs{/sha}","trees_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/git/trees{/sha}","statuses_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/statuses/{sha}","languages_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/languages","stargazers_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/stargazers","contributors_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/contributors","subscribers_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/subscribers","subscription_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/subscription","commits_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/commits{/sha}","git_commits_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/git/commits{/sha}","comments_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/comments{/number}","issue_comment_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/issues/comments{/number}","contents_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/contents/{+path}","compare_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/compare/{base}...{head}","merges_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/merges","archive_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/downloads","issues_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/issues{/number}","pulls_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/pulls{/number}","milestones_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/milestones{/number}","notifications_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/labels{/name}","releases_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/releases{/id}","created_at":"2014-10-12T00:59:37Z","updated_at":"2014-10-15T23:46:13Z","pushed_at":"2015-10-29T13:56:10Z","git_url":"git://github.com/NullVoxPopuli/active_model_serializers.git","ssh_url":"git@github.com:NullVoxPopuli/active_model_serializers.git","clone_url":"https://github.com/NullVoxPopuli/active_model_serializers.git","svn_url":"https://github.com/NullVoxPopuli/active_model_serializers","homepage":"","size":2370,"stargazers_count":0,"watchers_count":0,"language":"Ruby","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":1,"forks":1,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"rails-api:master","ref":"master","sha":"ecc3b35517a7a4a3de52a1928fabb1813aebc75e","user":{"login":"rails-api","id":2820360,"avatar_url":"https://avatars.githubusercontent.com/u/2820360?v=3","gravatar_id":"","url":"https://api.github.com/users/rails-api","html_url":"https://github.com/rails-api","followers_url":"https://api.github.com/users/rails-api/followers","following_url":"https://api.github.com/users/rails-api/following{/other_user}","gists_url":"https://api.github.com/users/rails-api/gists{/gist_id}","starred_url":"https://api.github.com/users/rails-api/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rails-api/subscriptions","organizations_url":"https://api.github.com/users/rails-api/orgs","repos_url":"https://api.github.com/users/rails-api/repos","events_url":"https://api.github.com/users/rails-api/events{/privacy}","received_events_url":"https://api.github.com/users/rails-api/received_events","type":"Organization","site_admin":false},"repo":{"id":2889230,"name":"active_model_serializers","full_name":"rails-api/active_model_serializers","owner":{"login":"rails-api","id":2820360,"avatar_url":"https://avatars.githubusercontent.com/u/2820360?v=3","gravatar_id":"","url":"https://api.github.com/users/rails-api","html_url":"https://github.com/rails-api","followers_url":"https://api.github.com/users/rails-api/followers","following_url":"https://api.github.com/users/rails-api/following{/other_user}","gists_url":"https://api.github.com/users/rails-api/gists{/gist_id}","starred_url":"https://api.github.com/users/rails-api/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rails-api/subscriptions","organizations_url":"https://api.github.com/users/rails-api/orgs","repos_url":"https://api.github.com/users/rails-api/repos","events_url":"https://api.github.com/users/rails-api/events{/privacy}","received_events_url":"https://api.github.com/users/rails-api/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/rails-api/active_model_serializers","description":"ActiveModel::Serializer implementation and Rails hooks","fork":false,"url":"https://api.github.com/repos/rails-api/active_model_serializers","forks_url":"https://api.github.com/repos/rails-api/active_model_serializers/forks","keys_url":"https://api.github.com/repos/rails-api/active_model_serializers/keys{/key_id}","collaborators_url":"https://api.github.com/repos/rails-api/active_model_serializers/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/rails-api/active_model_serializers/teams","hooks_url":"https://api.github.com/repos/rails-api/active_model_serializers/hooks","issue_events_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/events{/number}","events_url":"https://api.github.com/repos/rails-api/active_model_serializers/events","assignees_url":"https://api.github.com/repos/rails-api/active_model_serializers/assignees{/user}","branches_url":"https://api.github.com/repos/rails-api/active_model_serializers/branches{/branch}","tags_url":"https://api.github.com/repos/rails-api/active_model_serializers/tags","blobs_url":"https://api.github.com/repos/rails-api/active_model_serializers/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/rails-api/active_model_serializers/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/rails-api/active_model_serializers/git/refs{/sha}","trees_url":"https://api.github.com/repos/rails-api/active_model_serializers/git/trees{/sha}","statuses_url":"https://api.github.com/repos/rails-api/active_model_serializers/statuses/{sha}","languages_url":"https://api.github.com/repos/rails-api/active_model_serializers/languages","stargazers_url":"https://api.github.com/repos/rails-api/active_model_serializers/stargazers","contributors_url":"https://api.github.com/repos/rails-api/active_model_serializers/contributors","subscribers_url":"https://api.github.com/repos/rails-api/active_model_serializers/subscribers","subscription_url":"https://api.github.com/repos/rails-api/active_model_serializers/subscription","commits_url":"https://api.github.com/repos/rails-api/active_model_serializers/commits{/sha}","git_commits_url":"https://api.github.com/repos/rails-api/active_model_serializers/git/commits{/sha}","comments_url":"https://api.github.com/repos/rails-api/active_model_serializers/comments{/number}","issue_comment_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/comments{/number}","contents_url":"https://api.github.com/repos/rails-api/active_model_serializers/contents/{+path}","compare_url":"https://api.github.com/repos/rails-api/active_model_serializers/compare/{base}...{head}","merges_url":"https://api.github.com/repos/rails-api/active_model_serializers/merges","archive_url":"https://api.github.com/repos/rails-api/active_model_serializers/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/rails-api/active_model_serializers/downloads","issues_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues{/number}","pulls_url":"https://api.github.com/repos/rails-api/active_model_serializers/pulls{/number}","milestones_url":"https://api.github.com/repos/rails-api/active_model_serializers/milestones{/number}","notifications_url":"https://api.github.com/repos/rails-api/active_model_serializers/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/rails-api/active_model_serializers/labels{/name}","releases_url":"https://api.github.com/repos/rails-api/active_model_serializers/releases{/id}","created_at":"2011-12-01T06:39:26Z","updated_at":"2015-10-30T14:12:22Z","pushed_at":"2015-10-30T14:27:30Z","git_url":"git://github.com/rails-api/active_model_serializers.git","ssh_url":"git@github.com:rails-api/active_model_serializers.git","clone_url":"https://github.com/rails-api/active_model_serializers.git","svn_url":"https://github.com/rails-api/active_model_serializers","homepage":"","size":8161,"stargazers_count":3088,"watchers_count":3088,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":717,"mirror_url":null,"open_issues_count":176,"forks":717,"open_issues":176,"watchers":3088,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/1198"},"html":{"href":"https://github.com/rails-api/active_model_serializers/pull/1198"},"issue":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1198"},"comments":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1198/comments"},"review_comments":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/1198/comments"},"review_comment":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/1198/commits"},"statuses":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/statuses/2e86ef31868c445fb537fd1cd348d9ac8605f470"}}}}