{"action":"created","comment":{"url":"https://api.github.com/repos/square/moshi/pulls/comments/99499529","pull_request_review_id":20178463,"id":99499529,"diff_hunk":"@@ -0,0 +1,126 @@\n+/*\n+ * Copyright (C) 2017 Square, Inc.\n+ *\n+ * Licensed under the Apache License, Version 2.0 (the \"License\");\n+ * you may not use this file except in compliance with the License.\n+ * You may obtain a copy of the License at\n+ *\n+ *      http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing, software\n+ * distributed under the License is distributed on an \"AS IS\" BASIS,\n+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+ * See the License for the specific language governing permissions and\n+ * limitations under the License.\n+ */\n+package com.squareup.moshi.recipes;\n+\n+import com.squareup.moshi.JsonAdapter;\n+import com.squareup.moshi.JsonDataException;\n+import com.squareup.moshi.JsonReader;\n+import com.squareup.moshi.JsonWriter;\n+import com.squareup.moshi.Moshi;\n+import com.squareup.moshi.recipes.models.BlackjackHand;\n+import com.squareup.moshi.recipes.models.Card;\n+import com.squareup.moshi.recipes.models.Player;\n+import com.squareup.moshi.recipes.models.Suit;\n+import java.io.IOException;\n+import java.lang.annotation.Annotation;\n+import java.lang.reflect.Type;\n+import java.util.Collections;\n+import java.util.Map;\n+import java.util.Set;\n+\n+final class Polymorphic {\n+  public static void main(String[] args) throws Exception {\n+    Moshi moshi = new Moshi.Builder().add(PolymorphicBlackjackJsonAdapter.FACTORY)\n+        .add(new CardAdapter())\n+        .build();\n+    String handJson = \"{\\n\"\n+        + \"    \\\"kind\\\": \\\"blackjack-hand\\\",\\n\"\n+        + \"    \\\"data\\\": {\\n\"\n+        + \"      \\\"hidden_card\\\": \\\"6S\\\",\\n\"\n+        + \"      \\\"visible_cards\\\": [\\n\"\n+        + \"        \\\"4C\\\",\\n\"\n+        + \"        \\\"AH\\\"\\n\"\n+        + \"      ]\\n\"\n+        + \"    }\\n\"\n+        + \"  }\";\n+    String playerJson = \"{\\n\"\n+        + \"    \\\"kind\\\": \\\"player\\\",\\n\"\n+        + \"    \\\"data\\\": {\\n\"\n+        + \"      \\\"username\\\": \\\"jesse\\\",\\n\"\n+        + \"      \\\"lucky number\\\": 32\\n\"\n+        + \"    }\\n\"\n+        + \"  }\";\n+    JsonAdapter<BlackjackHand> handAdapter = moshi.adapter(BlackjackHand.class);\n+    JsonAdapter<Player> playerAdapter = moshi.adapter(Player.class);\n+    System.out.println(handAdapter.fromJson(handJson));\n+    System.out.println(playerAdapter.fromJson(playerJson));\n+    System.out.println(handAdapter.toJson(\n+        new BlackjackHand(new Card('A', Suit.SPADES), Collections.<Card>emptyList())));\n+    System.out.println(playerAdapter.toJson(new Player(\"jake\", -1)));\n+  }\n+\n+  public static final class PolymorphicBlackjackJsonAdapter extends JsonAdapter<Object> {\n+    public static final JsonAdapter.Factory FACTORY = new Factory() {\n+      @Override\n+      public JsonAdapter<?> create(Type type, Set<? extends Annotation> annotations, Moshi moshi) {\n+        if (type != BlackjackHand.class && type != Player.class) {\n+          return null;\n+        }\n+        JsonAdapter<BlackjackHand> handAdapter =\n+            moshi.nextAdapter(this, BlackjackHand.class, Collections.<Annotation>emptySet());\n+        JsonAdapter<Player> playerAdapter =\n+            moshi.nextAdapter(this, Player.class, Collections.<Annotation>emptySet());\n+        return new PolymorphicBlackjackJsonAdapter(handAdapter, playerAdapter);\n+      }\n+    };\n+\n+    private final JsonAdapter<BlackjackHand> handAdapter;\n+    private final JsonAdapter<Player> playerAdapter;\n+\n+    PolymorphicBlackjackJsonAdapter(JsonAdapter<BlackjackHand> handAdapter,\n+        JsonAdapter<Player> playerAdapter) {\n+      this.handAdapter = handAdapter;\n+      this.playerAdapter = playerAdapter;\n+    }\n+\n+    @Override public Object fromJson(JsonReader reader) throws IOException {\n+      // noinspection unchecked\n+      Map<String, Object> value = (Map<String, Object>) reader.readJsonValue();\n+      String type = (String) value.get(\"kind\");\n+      Object data = value.get(\"data\");\n+      switch (type) {\n+        case \"blackjack-hand\":\n+          return handAdapter.fromJsonValue(data);\n+        case \"player\":\n+          return playerAdapter.fromJsonValue(data);\n+        default:\n+          throw new JsonDataException(\"Unexpected type: \" + type);\n+      }\n+    }\n+\n+    @Override public void toJson(JsonWriter writer, Object value) throws IOException {\n+      Class<?> cls = value.getClass();\n+      writer.beginObject();\n+      writer.name(\"kind\");\n+      String type;\n+      JsonAdapter adapter;","path":"examples/src/main/java/com/squareup/moshi/recipes/Polymorphic.java","position":109,"original_position":109,"commit_id":"2f6079fda5ff45dfa46f1174e71458f9a340d2f6","original_commit_id":"2f6079fda5ff45dfa46f1174e71458f9a340d2f6","user":{"login":"NightlyNexus","id":4032667,"avatar_url":"https://avatars.githubusercontent.com/u/4032667?v=3","gravatar_id":"","url":"https://api.github.com/users/NightlyNexus","html_url":"https://github.com/NightlyNexus","followers_url":"https://api.github.com/users/NightlyNexus/followers","following_url":"https://api.github.com/users/NightlyNexus/following{/other_user}","gists_url":"https://api.github.com/users/NightlyNexus/gists{/gist_id}","starred_url":"https://api.github.com/users/NightlyNexus/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/NightlyNexus/subscriptions","organizations_url":"https://api.github.com/users/NightlyNexus/orgs","repos_url":"https://api.github.com/users/NightlyNexus/repos","events_url":"https://api.github.com/users/NightlyNexus/events{/privacy}","received_events_url":"https://api.github.com/users/NightlyNexus/received_events","type":"User","site_admin":false},"body":"I don't think I can assign a `JsonAdapter<Foo>` to the parameterized `JsonAdapter<Object>` type.\r\nKotlin can do that.","created_at":"2017-02-05T20:57:44Z","updated_at":"2017-02-05T20:57:44Z","html_url":"https://github.com/square/moshi/pull/256#discussion_r99499529","pull_request_url":"https://api.github.com/repos/square/moshi/pulls/256","_links":{"self":{"href":"https://api.github.com/repos/square/moshi/pulls/comments/99499529"},"html":{"href":"https://github.com/square/moshi/pull/256#discussion_r99499529"},"pull_request":{"href":"https://api.github.com/repos/square/moshi/pulls/256"}}},"pull_request":{"url":"https://api.github.com/repos/square/moshi/pulls/256","id":104707240,"html_url":"https://github.com/square/moshi/pull/256","diff_url":"https://github.com/square/moshi/pull/256.diff","patch_url":"https://github.com/square/moshi/pull/256.patch","issue_url":"https://api.github.com/repos/square/moshi/issues/256","number":256,"state":"open","locked":false,"title":"Add Polymorphic sample","user":{"login":"NightlyNexus","id":4032667,"avatar_url":"https://avatars.githubusercontent.com/u/4032667?v=3","gravatar_id":"","url":"https://api.github.com/users/NightlyNexus","html_url":"https://github.com/NightlyNexus","followers_url":"https://api.github.com/users/NightlyNexus/followers","following_url":"https://api.github.com/users/NightlyNexus/following{/other_user}","gists_url":"https://api.github.com/users/NightlyNexus/gists{/gist_id}","starred_url":"https://api.github.com/users/NightlyNexus/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/NightlyNexus/subscriptions","organizations_url":"https://api.github.com/users/NightlyNexus/orgs","repos_url":"https://api.github.com/users/NightlyNexus/repos","events_url":"https://api.github.com/users/NightlyNexus/events{/privacy}","received_events_url":"https://api.github.com/users/NightlyNexus/received_events","type":"User","site_admin":false},"body":"~This made me realize I think we do need `JsonWriter.writeJsonValue(Object)`.~\r\nNever mind, obviously just use the delegate to make calls to the JsonWriter.","created_at":"2017-02-05T17:29:29Z","updated_at":"2017-02-05T20:57:44Z","closed_at":null,"merged_at":null,"merge_commit_sha":"0166ccc6ec5757a0a9190eedc0dab6c5ba71edcb","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/square/moshi/pulls/256/commits","review_comments_url":"https://api.github.com/repos/square/moshi/pulls/256/comments","review_comment_url":"https://api.github.com/repos/square/moshi/pulls/comments{/number}","comments_url":"https://api.github.com/repos/square/moshi/issues/256/comments","statuses_url":"https://api.github.com/repos/square/moshi/statuses/2f6079fda5ff45dfa46f1174e71458f9a340d2f6","head":{"label":"square:eric/polymorphic","ref":"eric/polymorphic","sha":"2f6079fda5ff45dfa46f1174e71458f9a340d2f6","user":{"login":"square","id":82592,"avatar_url":"https://avatars.githubusercontent.com/u/82592?v=3","gravatar_id":"","url":"https://api.github.com/users/square","html_url":"https://github.com/square","followers_url":"https://api.github.com/users/square/followers","following_url":"https://api.github.com/users/square/following{/other_user}","gists_url":"https://api.github.com/users/square/gists{/gist_id}","starred_url":"https://api.github.com/users/square/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/square/subscriptions","organizations_url":"https://api.github.com/users/square/orgs","repos_url":"https://api.github.com/users/square/repos","events_url":"https://api.github.com/users/square/events{/privacy}","received_events_url":"https://api.github.com/users/square/received_events","type":"Organization","site_admin":false},"repo":{"id":22789601,"name":"moshi","full_name":"square/moshi","owner":{"login":"square","id":82592,"avatar_url":"https://avatars.githubusercontent.com/u/82592?v=3","gravatar_id":"","url":"https://api.github.com/users/square","html_url":"https://github.com/square","followers_url":"https://api.github.com/users/square/followers","following_url":"https://api.github.com/users/square/following{/other_user}","gists_url":"https://api.github.com/users/square/gists{/gist_id}","starred_url":"https://api.github.com/users/square/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/square/subscriptions","organizations_url":"https://api.github.com/users/square/orgs","repos_url":"https://api.github.com/users/square/repos","events_url":"https://api.github.com/users/square/events{/privacy}","received_events_url":"https://api.github.com/users/square/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/square/moshi","description":"A modern JSON library for Android and Java.","fork":false,"url":"https://api.github.com/repos/square/moshi","forks_url":"https://api.github.com/repos/square/moshi/forks","keys_url":"https://api.github.com/repos/square/moshi/keys{/key_id}","collaborators_url":"https://api.github.com/repos/square/moshi/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/square/moshi/teams","hooks_url":"https://api.github.com/repos/square/moshi/hooks","issue_events_url":"https://api.github.com/repos/square/moshi/issues/events{/number}","events_url":"https://api.github.com/repos/square/moshi/events","assignees_url":"https://api.github.com/repos/square/moshi/assignees{/user}","branches_url":"https://api.github.com/repos/square/moshi/branches{/branch}","tags_url":"https://api.github.com/repos/square/moshi/tags","blobs_url":"https://api.github.com/repos/square/moshi/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/square/moshi/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/square/moshi/git/refs{/sha}","trees_url":"https://api.github.com/repos/square/moshi/git/trees{/sha}","statuses_url":"https://api.github.com/repos/square/moshi/statuses/{sha}","languages_url":"https://api.github.com/repos/square/moshi/languages","stargazers_url":"https://api.github.com/repos/square/moshi/stargazers","contributors_url":"https://api.github.com/repos/square/moshi/contributors","subscribers_url":"https://api.github.com/repos/square/moshi/subscribers","subscription_url":"https://api.github.com/repos/square/moshi/subscription","commits_url":"https://api.github.com/repos/square/moshi/commits{/sha}","git_commits_url":"https://api.github.com/repos/square/moshi/git/commits{/sha}","comments_url":"https://api.github.com/repos/square/moshi/comments{/number}","issue_comment_url":"https://api.github.com/repos/square/moshi/issues/comments{/number}","contents_url":"https://api.github.com/repos/square/moshi/contents/{+path}","compare_url":"https://api.github.com/repos/square/moshi/compare/{base}...{head}","merges_url":"https://api.github.com/repos/square/moshi/merges","archive_url":"https://api.github.com/repos/square/moshi/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/square/moshi/downloads","issues_url":"https://api.github.com/repos/square/moshi/issues{/number}","pulls_url":"https://api.github.com/repos/square/moshi/pulls{/number}","milestones_url":"https://api.github.com/repos/square/moshi/milestones{/number}","notifications_url":"https://api.github.com/repos/square/moshi/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/square/moshi/labels{/name}","releases_url":"https://api.github.com/repos/square/moshi/releases{/id}","deployments_url":"https://api.github.com/repos/square/moshi/deployments","created_at":"2014-08-09T15:58:42Z","updated_at":"2017-02-05T15:31:12Z","pushed_at":"2017-02-05T17:51:40Z","git_url":"git://github.com/square/moshi.git","ssh_url":"git@github.com:square/moshi.git","clone_url":"https://github.com/square/moshi.git","svn_url":"https://github.com/square/moshi","homepage":"http://square.github.io/moshi/1.x/moshi/","size":939,"stargazers_count":2361,"watchers_count":2361,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":true,"forks_count":183,"mirror_url":null,"open_issues_count":21,"forks":183,"open_issues":21,"watchers":2361,"default_branch":"master"}},"base":{"label":"square:master","ref":"master","sha":"af09de142d9bb57057a80362104dcf7ff051db64","user":{"login":"square","id":82592,"avatar_url":"https://avatars.githubusercontent.com/u/82592?v=3","gravatar_id":"","url":"https://api.github.com/users/square","html_url":"https://github.com/square","followers_url":"https://api.github.com/users/square/followers","following_url":"https://api.github.com/users/square/following{/other_user}","gists_url":"https://api.github.com/users/square/gists{/gist_id}","starred_url":"https://api.github.com/users/square/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/square/subscriptions","organizations_url":"https://api.github.com/users/square/orgs","repos_url":"https://api.github.com/users/square/repos","events_url":"https://api.github.com/users/square/events{/privacy}","received_events_url":"https://api.github.com/users/square/received_events","type":"Organization","site_admin":false},"repo":{"id":22789601,"name":"moshi","full_name":"square/moshi","owner":{"login":"square","id":82592,"avatar_url":"https://avatars.githubusercontent.com/u/82592?v=3","gravatar_id":"","url":"https://api.github.com/users/square","html_url":"https://github.com/square","followers_url":"https://api.github.com/users/square/followers","following_url":"https://api.github.com/users/square/following{/other_user}","gists_url":"https://api.github.com/users/square/gists{/gist_id}","starred_url":"https://api.github.com/users/square/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/square/subscriptions","organizations_url":"https://api.github.com/users/square/orgs","repos_url":"https://api.github.com/users/square/repos","events_url":"https://api.github.com/users/square/events{/privacy}","received_events_url":"https://api.github.com/users/square/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/square/moshi","description":"A modern JSON library for Android and Java.","fork":false,"url":"https://api.github.com/repos/square/moshi","forks_url":"https://api.github.com/repos/square/moshi/forks","keys_url":"https://api.github.com/repos/square/moshi/keys{/key_id}","collaborators_url":"https://api.github.com/repos/square/moshi/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/square/moshi/teams","hooks_url":"https://api.github.com/repos/square/moshi/hooks","issue_events_url":"https://api.github.com/repos/square/moshi/issues/events{/number}","events_url":"https://api.github.com/repos/square/moshi/events","assignees_url":"https://api.github.com/repos/square/moshi/assignees{/user}","branches_url":"https://api.github.com/repos/square/moshi/branches{/branch}","tags_url":"https://api.github.com/repos/square/moshi/tags","blobs_url":"https://api.github.com/repos/square/moshi/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/square/moshi/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/square/moshi/git/refs{/sha}","trees_url":"https://api.github.com/repos/square/moshi/git/trees{/sha}","statuses_url":"https://api.github.com/repos/square/moshi/statuses/{sha}","languages_url":"https://api.github.com/repos/square/moshi/languages","stargazers_url":"https://api.github.com/repos/square/moshi/stargazers","contributors_url":"https://api.github.com/repos/square/moshi/contributors","subscribers_url":"https://api.github.com/repos/square/moshi/subscribers","subscription_url":"https://api.github.com/repos/square/moshi/subscription","commits_url":"https://api.github.com/repos/square/moshi/commits{/sha}","git_commits_url":"https://api.github.com/repos/square/moshi/git/commits{/sha}","comments_url":"https://api.github.com/repos/square/moshi/comments{/number}","issue_comment_url":"https://api.github.com/repos/square/moshi/issues/comments{/number}","contents_url":"https://api.github.com/repos/square/moshi/contents/{+path}","compare_url":"https://api.github.com/repos/square/moshi/compare/{base}...{head}","merges_url":"https://api.github.com/repos/square/moshi/merges","archive_url":"https://api.github.com/repos/square/moshi/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/square/moshi/downloads","issues_url":"https://api.github.com/repos/square/moshi/issues{/number}","pulls_url":"https://api.github.com/repos/square/moshi/pulls{/number}","milestones_url":"https://api.github.com/repos/square/moshi/milestones{/number}","notifications_url":"https://api.github.com/repos/square/moshi/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/square/moshi/labels{/name}","releases_url":"https://api.github.com/repos/square/moshi/releases{/id}","deployments_url":"https://api.github.com/repos/square/moshi/deployments","created_at":"2014-08-09T15:58:42Z","updated_at":"2017-02-05T15:31:12Z","pushed_at":"2017-02-05T17:51:40Z","git_url":"git://github.com/square/moshi.git","ssh_url":"git@github.com:square/moshi.git","clone_url":"https://github.com/square/moshi.git","svn_url":"https://github.com/square/moshi","homepage":"http://square.github.io/moshi/1.x/moshi/","size":939,"stargazers_count":2361,"watchers_count":2361,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":true,"forks_count":183,"mirror_url":null,"open_issues_count":21,"forks":183,"open_issues":21,"watchers":2361,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/square/moshi/pulls/256"},"html":{"href":"https://github.com/square/moshi/pull/256"},"issue":{"href":"https://api.github.com/repos/square/moshi/issues/256"},"comments":{"href":"https://api.github.com/repos/square/moshi/issues/256/comments"},"review_comments":{"href":"https://api.github.com/repos/square/moshi/pulls/256/comments"},"review_comment":{"href":"https://api.github.com/repos/square/moshi/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/square/moshi/pulls/256/commits"},"statuses":{"href":"https://api.github.com/repos/square/moshi/statuses/2f6079fda5ff45dfa46f1174e71458f9a340d2f6"}}}}
{"actor":{"display_login":"NightlyNexus"}}