{"id":10592,"date":"2016-06-17T10:00:17","date_gmt":"2016-06-17T01:00:17","guid":{"rendered":"http:\/\/www.techscore.com\/blog\/?p=10592"},"modified":"2018-11-14T16:33:45","modified_gmt":"2018-11-14T07:33:45","slug":"java-jackson-polymorphic-deserialization","status":"publish","type":"post","link":"https:\/\/www.techscore.com\/blog\/2016\/06\/17\/java-jackson-polymorphic-deserialization\/","title":{"rendered":"Java : Jackson \u306b\u3088\u308b JSON \u30c7\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u6642\u306e\u578b\u89e3\u6c7a\u65b9\u6cd5"},"content":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3002\u677e\u672c\u3067\u3059\u3002<\/p>\n<p>\u30dd\u30ea\u30e2\u30fc\u30d5\u30a3\u30ba\u30e0\u306f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u6307\u5411\u8a00\u8a9e\u306e\u9b45\u529b\u306e\u3072\u3068\u3064\u3067\u3059\u304c\u3001\u3053\u308c\u304c\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068 JSON \u306e\u30de\u30c3\u30d4\u30f3\u30b0\u3092\u8907\u96d1\u306b\u3059\u308b\u306a\u3042\u3068\u5ea6\u3005\u611f\u3058\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u6c4e\u5316\u3055\u308c\u305f\u578b\u3068\u3057\u3066\u5b9a\u7fa9\u3055\u308c\u305f\u30d7\u30ed\u30d1\u30c6\u30a3\uff08\u30d5\u30a3\u30fc\u30eb\u30c9\uff09\u3092\u6301\u3064\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092 JSON \u304b\u3089\u30c7\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u3059\u308b\u306b\u306f\u3001\u578b\u89e3\u6c7a\u306b\u95a2\u3059\u308b\u5b9a\u7fa9\u3084\u30ed\u30b8\u30c3\u30af\u3092\u30b3\u30fc\u30c9\u3084\u8a2d\u5b9a\u3068\u3057\u3066\u7d44\u307f\u8fbc\u3080\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002\u3053\u3053\u306b\u3001\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u306e\u8a2d\u8a08\u3068\u3001JSON \u306e\u30a8\u30f3\u30c6\u30a3\u30c6\u30a3\u8a2d\u8a08\u306e\u9593\u3067\u30c8\u30ec\u30fc\u30c9\u30aa\u30d5\u304c\u767a\u751f\u3059\u308b\u3053\u3068\u304c\u3042\u308a\u3001\u30b1\u30fc\u30b9\u306b\u5fdc\u3058\u305f\u6700\u9069\u306a\u8a2d\u8a08\uff0f\u5b9f\u88c5\u304c\u5fc5\u8981\u306b\u306a\u308b\u308f\u3051\u3067\u3059\u3002<\/p>\n<p>\u79c1\u304c\u6271\u3046\u30bd\u30d5\u30c8\u30a6\u30a7\u30a2\u306e\u30e9\u30a4\u30d5\u30b5\u30a4\u30af\u30eb\u306e\u7279\u6027\u4e0a\u3001\u7e70\u308a\u8fd4\u3055\u308c\u308b\u8ffd\u52a0\u958b\u767a\u306b\u5bfe\u3057\u3001\u4fdd\u5b88\u6027\uff0f\u62e1\u5f35\u6027\u3092\u7dad\u6301\u3059\u308b\u3053\u3068\u306f\u5927\u304d\u306a\u95a2\u5fc3\u3054\u3068\u3067\u3059\u3002\u30af\u30e9\u30b9\u95a2\u4fc2\u306e\u59a5\u5f53\u6027\u3084\u7406\u89e3\u3057\u3084\u3059\u3055\u3001\u5909\u66f4\u6642\u306e\u5f71\u97ff\u7bc4\u56f2\u306e\u62bd\u51fa\u3057\u3084\u3059\u3055\u3001\u30af\u30e9\u30b9\u6570\u3001\u30b3\u30fc\u30c9\u91cf\u3001\u5b9f\u88c5\u96e3\u6613\u5ea6\u306a\u3069\u3001\u69d8\u3005\u306a\u30c8\u30ec\u30fc\u30c9\u30aa\u30d5\u30b9\u30e9\u30a4\u30c0\u30fc\u3092\u982d\u306b\u63cf\u304d\u306a\u304c\u3089\u3001\u6700\u9069\u3060\u3068\u8003\u3048\u308b\u9078\u629e\u3092\u884c\u3044\u307e\u3059\u3002<\/p>\n<p>\u3053\u306e\u300cJSON \u30c7\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u6642\u306e\u578b\u89e3\u6c7a\u300d\u306b\u3069\u306e\u3088\u3046\u306a\u65b9\u5f0f\u3092\u63a1\u7528\u3059\u308b\u304b\u3068\u3044\u3046\u8a2d\u8a08\u3001\u5b9f\u88c5\u3082\u3001\u305d\u3093\u306a\u9078\u629e\u306b\u982d\u3092\u60a9\u307e\u3059\u3072\u3068\u3064\u306a\u306e\u3067\u3059\u3002<\/p>\n<p>\u3068\u3001\u80cc\u666f\u3092\u8aac\u660e\u3059\u308b\u3068\u5c11\u3005\u5927\u3052\u3055\u306a\u66f8\u304d\u51fa\u3057\u306b\u306a\u308a\u307e\u3057\u305f\u304c\u3001JSON \u30e9\u30a4\u30d6\u30e9\u30ea\u306e Jackson \u3092\u4f7f\u3063\u305f\u578b\u89e3\u6c7a\u65b9\u6cd5\u306e\u9078\u629e\u80a2\u3092\u3044\u304f\u3064\u304b\u3054\u7d39\u4ecb\u3057\u3088\u3046\u3068\u3044\u3046\u8a18\u4e8b\u3067\u3059\u3002<\/p>\n<h2>\u578b\u89e3\u6c7a\u306b\u5931\u6557\u3059\u308b\u3063\u3066\u3069\u3046\u3044\u3046\u3053\u3068\uff1f<\/h2>\n<p>\u6b21\u306e\u30b3\u30fc\u30c9\u306f Java + Jackson \u3092\u4f7f\u3063\u305f\u30b5\u30f3\u30d7\u30eb\u3067\u3059\u3002<\/p>\n<p>shape \u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u578b\u304c Shape \u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u3067\u3042\u308a\u3001\u5b9f\u884c\u6642\u306b\u30d0\u30a4\u30f3\u30c7\u30a3\u30f3\u30b0\u3055\u308c\u308b\u306e\u306f\u3001\u305d\u306e\u5b9f\u88c5\u30af\u30e9\u30b9\u3067\u3042\u308b Rectangle \u3084 Triangle \u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3067\u3059\u3002<\/p>\n<pre class=\"lang:java decode:true\">\r\npackage com.techscore.blog.example;\r\n\r\nimport com.fasterxml.jackson.databind.ObjectMapper;\r\n\r\npublic class Example0 {\r\n\r\n    public Shape shape;\r\n\r\n    static interface Shape {}\r\n\r\n    static class Rectangle implements Shape {\r\n        public int width;\r\n        public int height;\r\n    }\r\n\r\n    static class Triangle implements Shape {\r\n        public int base;\r\n        public int height;\r\n    }\r\n\r\n    public static void main(String[] args) throws Exception {\r\n        Rectangle rectangle = new Rectangle();\r\n        rectangle.width = 100;\r\n        rectangle.height = 200;\r\n\r\n        Example0 example = new Example0();\r\n        example.shape = rectangle;\r\n\r\n        ObjectMapper mapper = new ObjectMapper();\r\n\r\n        System.out.println(\"\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u7d50\u679c\uff1a\");\r\n        String json = mapper.writeValueAsString(example);\r\n        System.out.println(json);\r\n\r\n        System.out.println(\"\u30c7\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u306f\u5931\u6557\u3059\u308b\uff1a\");\r\n        Example0 result = mapper.readValue(json, Example0.class);\r\n    }\r\n}\r\n<\/pre>\n<p>\u5b9f\u884c\u3059\u308b\u3068\u3001\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u306b\u306f\u6210\u529f\u3057\u307e\u3059\u304c\u3001\u30c7\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u306f\u578b\u89e3\u6c7a\u304c\u3067\u304d\u305a\u306b\u5931\u6557\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true\">\r\n\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u7d50\u679c\uff1a\r\n{\"shape\":{\"width\":100,\"height\":200}}\r\n\u30c7\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u306f\u5931\u6557\u3059\u308b\uff1a\r\nException in thread \"main\" com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.techscore.blog.example.Example0$Shape, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information\r\n at [Source: {\"shape\":{\"width\":100,\"height\":200}}; line: 1, column: 2] (through reference chain: com.techscore.blog.example.Example0[\"shape\"])\r\n\tat com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)\r\n\tat com.fasterxml.jackson.databind.DeserializationContext.instantiationException(DeserializationContext.java:889)\r\n\tat com.fasterxml.jackson.databind.deser.AbstractDeserializer.deserialize(AbstractDeserializer.java:139)\r\n\tat com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:520)\r\n\tat com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:101)\r\n\tat com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:256)\r\n\tat com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:125)\r\n\tat com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3702)\r\n\tat com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2714)\r\n\tat com.techscore.blog.example.Example0.main(Example0.java:36)\r\n<\/pre>\n<p>\u4f8b\u5916\u30e1\u30c3\u30bb\u30fc\u30b8\u306b\u3042\u308b\u3088\u3046\u306b\u3001shape \u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u30c7\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u3057\u3088\u3046\u3068\u3057\u305f\u3051\u3069 Shape \u306f\u62bd\u8c61\u578b\uff08\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\uff09\u3060\u304b\u3089\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u5316\u3067\u304d\u306a\u3044\u3088\uff01\u3068\u6012\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u3053\u306e\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u306e\u5834\u5408\u3001shape \u30d5\u30a3\u30fc\u30eb\u30c9\u306f Rectangle \u30af\u30e9\u30b9\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3068\u3057\u3066\u30c7\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u3055\u308c\u308b\u306e\u304c\u3001\u671f\u5f85\u3059\u308b\u52d5\u4f5c\u3067\u3059\u3002<\/p>\n<h2>\u3010\u578b\u89e3\u6c7a\u65b9\u6cd51\u3011 @JsonTypeInfo(use = Id.CLASS)<\/h2>\n<p>\u3082\u3063\u3068\u3082\u7c21\u5358\u304a\u624b\u8efd\u306a\u65b9\u6cd5\u3067\u3059\u3002<\/p>\n<p>\u5148\u307b\u3069\u578b\u89e3\u6c7a\u306b\u5931\u6557\u3057\u305f\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3092\u6539\u9020\u3057\u3001@JsonTypeInfo \u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3092\u4f7f\u3063\u3066 Shape \u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u306b\u578b\u89e3\u6c7a\u65b9\u6cd5\u3092\u5b9a\u7fa9\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:java decode:true\">\r\npackage com.techscore.blog.example;\r\n\r\nimport com.fasterxml.jackson.annotation.JsonTypeInfo;\r\nimport com.fasterxml.jackson.annotation.JsonTypeInfo.Id;\r\nimport com.fasterxml.jackson.databind.ObjectMapper;\r\n\r\npublic class Example1 {\r\n\r\n    public Shape shape;\r\n\r\n    @JsonTypeInfo(use = Id.CLASS)\r\n    static interface Shape {}\r\n\r\n    static class Rectangle implements Shape {\r\n        public int width;\r\n        public int height;\r\n    }\r\n\r\n    static class Triangle implements Shape {\r\n        public int base;\r\n        public int height;\r\n    }\r\n\r\n    public static void main(String[] args) throws Exception {\r\n        Rectangle rectangle = new Rectangle();\r\n        rectangle.width = 100;\r\n        rectangle.height = 200;\r\n\r\n        Example1 example = new Example1();\r\n        example.shape = rectangle;\r\n\r\n        ObjectMapper mapper = new ObjectMapper();\r\n\r\n        System.out.println(\"\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u7d50\u679c\uff1a\");\r\n        String json = mapper.writeValueAsString(example);\r\n        System.out.println(json);\r\n\r\n        System.out.println(\"\u30c7\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u7d50\u679c\u306b\u542b\u307e\u308c\u308b shape \u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u578b\uff1a\");\r\n        Example1 result = mapper.readValue(json, Example1.class);\r\n        System.out.println(result.shape.getClass().getName());\r\n    }\r\n}\r\n<\/pre>\n<p>@JsonTypeInfo \u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u306e use \u5c5e\u6027\u306b Id.CLASS \u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u3067\u3001Shape \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u3057\u305f JSON \u5185\u306b \"@class\" \u3068\u3044\u3046\u30ad\u30fc\u304c\u8ffd\u52a0\u3055\u308c\u3001\u305d\u306e\u5024\u3068\u3057\u3066\u5b9f\u88c5\u30af\u30e9\u30b9\u540d\u304c\u66f8\u304d\u51fa\u3055\u308c\u307e\u3059\u3002\u30c7\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u51e6\u7406\u3067\u306f\u3053\u306e\u5024\u306b\u57fa\u3065\u3044\u3066\u578b\u89e3\u6c7a\u304c\u884c\u308f\u308c\u307e\u3059\u3002<\/p>\n<p>\u5b9f\u884c\u7d50\u679c\u306f\u3053\u308c\u3002<\/p>\n<pre class=\"lang:bash decode:true\">\r\n\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u7d50\u679c\uff1a\r\n{\"shape\":{\"@class\":\"com.techscore.blog.example.Example1$Rectangle\",\"width\":100,\"height\":200}}\r\n\u30c7\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u7d50\u679c\u306b\u542b\u307e\u308c\u308b shape \u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u578b\uff1a\r\ncom.techscore.blog.example.Example1$Rectangle\r\n<\/pre>\n<p>\u30af\u30e9\u30b9\u540d\u304c\u51fa\u529b\u3055\u308c\u3066\u3057\u307e\u3046\u3053\u3068\u304c\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u9762\u3067\u826f\u304f\u306a\u3044\u30b1\u30fc\u30b9\u3082\u3042\u308a\u305d\u3046\u3067\u3059\u3002\u305d\u3053\u304c\u554f\u984c\u306b\u306a\u3089\u306a\u3044\u306a\u3089\u3001\u5b9f\u88c5\u30af\u30e9\u30b9\u3092\u8ffd\u52a0\u3057\u3066\u3082\u30b3\u30fc\u30c9\u5909\u66f4\u306e\u5fc5\u8981\u304c\u306a\u304f\u3001\u62e1\u5f35\u6027\u306f\u826f\u3055\u305d\u3046\u3067\u3059\u3002<\/p>\n<p>\u4fdd\u5b88\u9762\u3092\u8003\u3048\u308b\u3068\u3001\u30af\u30e9\u30b9\u8a2d\u8a08\u5909\u66f4\u3092\u963b\u5bb3\u3059\u308b\u8981\u56e0\u306b\u3082\u306a\u308b\u306e\u3067\u6ce8\u610f\u304c\u5fc5\u8981\u3067\u3059\u3002\u4f8b\u3048\u3070\u5b9f\u88c5\u30af\u30e9\u30b9\u306e\u30af\u30e9\u30b9\u540d\u304c\u5909\u66f4\u3055\u308c\u305f\u5834\u5408\u3001\u53e4\u3044\u30af\u30e9\u30b9\u540d\u304c\u4f7f\u308f\u308c\u305f JSON \u3092\u30c7\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u3057\u3088\u3046\u3068\u3059\u308b\u3068\u3001\u578b\u89e3\u6c7a\u306b\u5931\u6557\u3057\u3066\u3057\u307e\u3044\u307e\u3059\u3002<\/p>\n<h2>\u3010\u578b\u89e3\u6c7a\u65b9\u6cd52\u3011 @JsonTypeInfo(use = Id.NAME) + @JsonSubTypes<\/h2>\n<p>\u3053\u308c\u3082\u7c21\u5358\u306a\u65b9\u6cd5\u3067\u3059\u3002<\/p>\n<p>\u578b\u89e3\u6c7a\u65b9\u6cd51\u3068\u306e\u9055\u3044\u306f\u3001Shape \u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u306b\u4ed8\u3051\u3089\u308c\u305f @JsonTypeInfo \u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u306e use \u5c5e\u6027\u5024\u304c Id.NAME \u306b\u5909\u308f\u308a\u3001\u3055\u3089\u306b @JsonSubTypes \u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u304c\u8ffd\u52a0\u3055\u308c\u305f\u3053\u3068\u3067\u3059\u3002<\/p>\n<pre class=\"lang:java decode:true\">\r\npackage com.techscore.blog.example;\r\n\r\nimport com.fasterxml.jackson.annotation.JsonSubTypes;\r\nimport com.fasterxml.jackson.annotation.JsonSubTypes.Type;\r\nimport com.fasterxml.jackson.annotation.JsonTypeInfo;\r\nimport com.fasterxml.jackson.annotation.JsonTypeInfo.Id;\r\nimport com.fasterxml.jackson.databind.ObjectMapper;\r\n\r\npublic class Example2 {\r\n\r\n    public Shape shape;\r\n\r\n    @JsonTypeInfo(use = Id.NAME)\r\n    @JsonSubTypes({\r\n        @Type(name = \"R\", value = Rectangle.class),\r\n        @Type(name = \"T\", value = Triangle.class)\r\n    })\r\n    static interface Shape {}\r\n\r\n    static class Rectangle implements Shape {\r\n        public int width;\r\n        public int height;\r\n    }\r\n\r\n    static class Triangle implements Shape {\r\n        public int base;\r\n        public int height;\r\n    }\r\n\r\n    public static void main(String[] args) throws Exception {\r\n        Rectangle rectangle = new Rectangle();\r\n        rectangle.width = 100;\r\n        rectangle.height = 200;\r\n\r\n        Example2 example = new Example2();\r\n        example.shape = rectangle;\r\n\r\n        ObjectMapper mapper = new ObjectMapper();\r\n\r\n        System.out.println(\"\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u7d50\u679c\uff1a\");\r\n        String json = mapper.writeValueAsString(example);\r\n        System.out.println(json);\r\n\r\n        System.out.println(\"\u30c7\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u7d50\u679c\u306b\u542b\u307e\u308c\u308b shape \u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u578b\uff1a\");\r\n        Example2 result = mapper.readValue(json, Example2.class);\r\n        System.out.println(result.shape.getClass().getName());\r\n    }\r\n}\r\n<\/pre>\n<p>\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u5f8c\u306e JSON \u5185\u306b \"@type\" \u3068\u3044\u3046\u30ad\u30fc\u304c\u8ffd\u52a0\u3055\u308c\u3001\u305d\u306e\u5024\u306b\u8b58\u5225\u540d\u304c\u66f8\u304d\u51fa\u3055\u308c\u307e\u3059\u3002\u30c7\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u6642\u306e\u578b\u89e3\u6c7a\u306f\u3001@JsonSubTypes \u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u306b\u5b9a\u7fa9\u3055\u308c\u305f\u8b58\u5225\u540d\u3068\u5b9f\u88c5\u30af\u30e9\u30b9\u540d\u306e\u30de\u30c3\u30d4\u30f3\u30b0\u304c\u4f7f\u308f\u308c\u307e\u3059\u3002<\/p>\n<p>\u5b9f\u884c\u7d50\u679c\u3067\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true\">\r\n\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u7d50\u679c\uff1a\r\n{\"shape\":{\"@type\":\"R\",\"width\":100,\"height\":200}}\r\n\u30c7\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u7d50\u679c\u306b\u542b\u307e\u308c\u308b shape \u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u578b\uff1a\r\ncom.techscore.blog.example.Example2$Rectangle\r\n<\/pre>\n<p>\u578b\u89e3\u6c7a\u65b9\u6cd51\u3068\u6bd4\u3079\u3001\u8b58\u5225\u540d\u3092\u4f7f\u3046\u3053\u3068\u306b\u3088\u3063\u3066\u4fdd\u5b88\u6027\u304c\u9ad8\u307e\u308a\u3001\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u3078\u306e\u61f8\u5ff5\u3082\u7121\u304f\u306a\u3063\u3066\u3044\u307e\u3059\u3002\u305d\u306e\u5f15\u304d\u63db\u3048\u3068\u3057\u3066\u3001\u5b9f\u88c5\u30af\u30e9\u30b9\u304c\u8ffd\u52a0\u3055\u308c\u308b\u5ea6\u306b @JsonSubTypes \u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u306b\u30de\u30c3\u30d4\u30f3\u30b0\u3092\u8ffd\u52a0\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u304f\u306a\u3063\u305f\u70b9\u304c\u9762\u5012\u306a\u3068\u3053\u308d\u3067\u3059\u3002<\/p>\n<p>\u3053\u306e\u65b9\u6cd5\u3067\u306f @JsonSubTypes \u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u306e\u5b9a\u7fa9\u306b\u3088\u308a\u3001\u89aa\u30af\u30e9\u30b9\uff08\u3053\u3053\u3067\u306f\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u3067\u3042\u308b Shape\uff09\u306e\u5b9a\u7fa9\u5185\u306b\u3001\u5b50\u30af\u30e9\u30b9\uff08\u5b9f\u88c5\u30af\u30e9\u30b9\uff09\u3078\u306e\u53c2\u7167\u304c\u51fa\u6765\u3066\u3057\u307e\u3044\u307e\u3059\u3002\u3053\u306e\u76f8\u4e92\u30ea\u30f3\u30af\u304c\u6c17\u306b\u306a\u308b\u306a\u3089\u3001\u3053\u306e\u65b9\u6cd5\u306f\u304a\u52e7\u3081\u3067\u304d\u307e\u305b\u3093\u3002\u5b50\u30af\u30e9\u30b9\u304c\u89aa\u30af\u30e9\u30b9\u306e\u5185\u90e8\u30af\u30e9\u30b9\u3068\u3057\u3066\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u308b\u3088\u3046\u306a\u30b1\u30fc\u30b9\u306a\u3089\u6700\u9069\u3067\u3057\u3087\u3046\u3002<\/p>\n<h2>\u3010\u578b\u89e3\u6c7a\u65b9\u6cd53\u3011 @JsonTypeInfo(use = Id.NAME) + SubtypeResolver<\/h2>\n<p>\u578b\u89e3\u6c7a\u65b9\u6cd52\u3068\u4f3c\u3066\u3044\u307e\u3059\u304c\u3001\u30de\u30c3\u30d4\u30f3\u30b0\u306e\u5b9a\u7fa9\u306b @JsonSubTypes \u3092\u4f7f\u308f\u305a\u3001ObjectMapper \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b NamedType \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068\u3057\u3066\u767b\u9332\u3059\u308b\u3053\u3068\u3067\u4ee3\u66ff\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:java decode:true\">\r\npackage com.techscore.blog.example;\r\n\r\nimport com.fasterxml.jackson.annotation.JsonTypeInfo;\r\nimport com.fasterxml.jackson.annotation.JsonTypeInfo.Id;\r\nimport com.fasterxml.jackson.databind.ObjectMapper;\r\nimport com.fasterxml.jackson.databind.jsontype.NamedType;\r\n\r\npublic class Example3 {\r\n\r\n    public Shape shape;\r\n\r\n    @JsonTypeInfo(use = Id.NAME)\r\n    static interface Shape {}\r\n\r\n    static class Rectangle implements Shape {\r\n        public int width;\r\n        public int height;\r\n    }\r\n\r\n    static class Triangle implements Shape {\r\n        public int base;\r\n        public int height;\r\n    }\r\n\r\n    public static void main(String[] args) throws Exception {\r\n        Rectangle rectangle = new Rectangle();\r\n        rectangle.width = 100;\r\n        rectangle.height = 200;\r\n\r\n        Example3 example = new Example3();\r\n        example.shape = rectangle;\r\n\r\n        ObjectMapper mapper = new ObjectMapper();\r\n        mapper.registerSubtypes(\r\n                new NamedType(Rectangle.class, \"R\"),\r\n                new NamedType(Triangle.class, \"T\"));\r\n\r\n        System.out.println(\"\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u7d50\u679c\uff1a\");\r\n        String json = mapper.writeValueAsString(example);\r\n        System.out.println(json);\r\n\r\n        System.out.println(\"\u30c7\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u7d50\u679c\u306b\u542b\u307e\u308c\u308b shape \u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u578b\uff1a\");\r\n        Example3 result = mapper.readValue(json, Example3.class);\r\n        System.out.println(result.shape.getClass().getName());\r\n    }\r\n}\r\n<\/pre>\n<p>JSON \u3078\u306e\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u7d50\u679c\u306f\u578b\u89e3\u6c7a\u65b9\u6cd52\u3068\u540c\u3058\u3067\u3059\u3002<\/p>\n<p>\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3067\u306f\u7c21\u7d20\u5316\u3059\u308b\u305f\u3081\u3001NamedType \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092 ObjectMapper \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u76f4\u63a5\u767b\u9332\u3057\u3066\u3044\u307e\u3059\u304c\u3001\u3053\u306e NamedType \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306f registerSubtypes() \u30e1\u30bd\u30c3\u30c9\u5185\u90e8\u3067 SubtypeResolver \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u8ffd\u52a0\u3055\u308c\u307e\u3059\u3002\u30c7\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u6642\u306e\u578b\u89e3\u6c7a\u306f\u3001\u3053\u306e SubtypeResolver \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u8a2d\u5b9a\u3055\u308c\u305f\u8b58\u5225\u540d\u3068\u5b9f\u88c5\u30af\u30e9\u30b9\u540d\u306e\u30de\u30c3\u30d4\u30f3\u30b0\u304c\u4f7f\u308f\u308c\u307e\u3059\u3002<\/p>\n<p>\u5b9f\u884c\u7d50\u679c\u3067\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true\">\r\n\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u7d50\u679c\uff1a\r\n{\"shape\":{\"@type\":\"R\",\"width\":100,\"height\":200}}\r\n\u30c7\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u7d50\u679c\u306b\u542b\u307e\u308c\u308b shape \u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u578b\uff1a\r\ncom.techscore.blog.example.Example3$Rectangle\r\n<\/pre>\n<p>\u3053\u306e\u65b9\u6cd5\u3067\u306f\u3001\u578b\u89e3\u6c7a\u65b9\u6cd52\u3067\u6c17\u306b\u306a\u3063\u305f\u76f8\u4e92\u30ea\u30f3\u30af\u554f\u984c\u304c\u89e3\u6d88\u3057\u307e\u3059\u3002\u307e\u305f\u3001\u8b58\u5225\u540d\uff0f\u5b9f\u88c5\u30af\u30e9\u30b9\u540d\u306e\u30de\u30c3\u30d4\u30f3\u30b0\u5b9a\u7fa9\u306b\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3092\u4f7f\u308f\u306a\u3044\u306e\u3067\u3001\u30de\u30c3\u30d4\u30f3\u30b0\u306e\u8a2d\u5b9a\u3092\u67d4\u8edf\u306b\u3067\u304d\u308b\u3068\u3044\u3046\u30e1\u30ea\u30c3\u30c8\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u3053\u308c\u3092\u63a1\u7528\u3057\u305f\u30af\u30e9\u30b9\u306e\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\uff0f\u30c7\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u306b\u306f\u3001\u30de\u30c3\u30d4\u30f3\u30b0\u304c\u5b9a\u7fa9\u3055\u308c\u305f ObjectMapper \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u5fc5\u305a\u4f7f\u3046\u3001\u3068\u3044\u3046\u5b9f\u88c5\u30eb\u30fc\u30eb\u3092\u3069\u3046\u3084\u3063\u3066\u5fb9\u5e95\u3059\u308b\u304b\u304c\u30dd\u30a4\u30f3\u30c8\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h2>\u3010\u578b\u89e3\u6c7a\u65b9\u6cd54\u3011 @JsonTypeInfo + @JsonTypeIdResolver<\/h2>\n<p>\u591a\u5c11\u3001\u9762\u5012\u3067\u3059\u304c\u975e\u5e38\u306b\u67d4\u8edf\u306a\u578b\u89e3\u6c7a\u65b9\u6cd5\u3067\u3059\u3002<\/p>\n<p>Shape \u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u306b @JsonTypeIdResolver \u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3092\u3064\u3051\u3001\u578b\u89e3\u6c7a\u3092\u62c5\u3046\u30af\u30e9\u30b9\u304c\u3069\u308c\u3067\u3042\u308b\u304b\u3092\u5b9a\u7fa9\u3057\u3066\u3044\u307e\u3059\uff08\u30b5\u30f3\u30d7\u30eb\u3067\u306f ShapeTypeIdResolver \u30af\u30e9\u30b9\uff09\u3002<\/p>\n<pre class=\"lang:java decode:true\">\r\npackage com.techscore.blog.example;\r\n\r\nimport com.fasterxml.jackson.annotation.JsonTypeInfo;\r\nimport com.fasterxml.jackson.annotation.JsonTypeInfo.Id;\r\nimport com.fasterxml.jackson.databind.DatabindContext;\r\nimport com.fasterxml.jackson.databind.JavaType;\r\nimport com.fasterxml.jackson.databind.ObjectMapper;\r\nimport com.fasterxml.jackson.databind.annotation.JsonTypeIdResolver;\r\nimport com.fasterxml.jackson.databind.jsontype.TypeIdResolver;\r\nimport com.fasterxml.jackson.databind.type.TypeFactory;\r\n\r\npublic class Example4 {\r\n\r\n    public Shape shape;\r\n\r\n    @JsonTypeInfo(use = Id.NAME)\r\n    @JsonTypeIdResolver(ShapeTypeIdResolver.class)\r\n    static interface Shape {}\r\n\r\n    static class Rectangle implements Shape {\r\n        public int width;\r\n        public int height;\r\n    }\r\n\r\n    static class Triangle implements Shape {\r\n        public int base;\r\n        public int height;\r\n    }\r\n\r\n    static class ShapeTypeIdResolver implements TypeIdResolver {\r\n\r\n        @Override\r\n        public void init(JavaType baseType) {}\r\n\r\n        @Override\r\n        public String idFromValue(Object value) {\r\n            return idFromValueAndType(value, value.getClass());\r\n        }\r\n\r\n        @Override\r\n        public String idFromValueAndType(Object value, Class<?> suggestedType) {\r\n            if (Rectangle.class.isAssignableFrom(suggestedType)) {\r\n                return \"R\";\r\n            } else if (Triangle.class.isAssignableFrom(suggestedType)) {\r\n                return \"T\";\r\n            }\r\n            throw new IllegalArgumentException();\r\n        }\r\n\r\n        @Override\r\n        public String idFromBaseType() {\r\n            throw new UnsupportedOperationException();\r\n        }\r\n\r\n        @Override\r\n        public JavaType typeFromId(String id) {\r\n            return typeFromId(null, id);\r\n        }\r\n\r\n        @Override\r\n        public JavaType typeFromId(DatabindContext context, String id) {\r\n            TypeFactory typeFactory = (context != null) ? context.getTypeFactory() : TypeFactory.defaultInstance();\r\n            if (\"R\".equals(id)) {\r\n                return typeFactory.constructType(Rectangle.class);\r\n            } else if (\"T\".equals(id)) {\r\n                return typeFactory.constructType(Triangle.class);\r\n            }\r\n            throw new IllegalArgumentException();\r\n        }\r\n\r\n        @Override\r\n        public Id getMechanism() {\r\n            return Id.NAME;\r\n        }\r\n    }\r\n\r\n    public static void main(String[] args) throws Exception {\r\n        Rectangle rectangle = new Rectangle();\r\n        rectangle.width = 100;\r\n        rectangle.height = 200;\r\n\r\n        Example4 example = new Example4();\r\n        example.shape = rectangle;\r\n\r\n        ObjectMapper mapper = new ObjectMapper();\r\n\r\n        System.out.println(\"\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u7d50\u679c\uff1a\");\r\n        String json = mapper.writeValueAsString(example);\r\n        System.out.println(json);\r\n\r\n        System.out.println(\"\u30c7\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u7d50\u679c\u306b\u542b\u307e\u308c\u308b shape \u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u578b\uff1a\");\r\n        Example4 result = mapper.readValue(json, Example4.class);\r\n        System.out.println(result.shape.getClass().getName());\r\n    }\r\n}\r\n<\/pre>\n<p>\u3053\u308c\u3082 JSON \u3078\u306e\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u7d50\u679c\u306f\u578b\u89e3\u6c7a\u65b9\u6cd52\u3068\u540c\u3058\u3067\u3059\u3002<\/p>\n<p>@JsonTypeIdResolver  \u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3067\u6307\u5b9a\u3057\u305f TypeIdResolver \u306e\u5b9f\u88c5\u30af\u30e9\u30b9\u306e idFromValue() \u30e1\u30bd\u30c3\u30c9\u3084 idFromValueAndType() \u30e1\u30bd\u30c3\u30c9\u3067\u5b9f\u88c5\u30af\u30e9\u30b9\u304b\u3089 ID \u3078\u306e\u5909\u63db\u3092\u884c\u3044\u3001typeFromId() \u30e1\u30bd\u30c3\u30c9\u3067 ID \u304b\u3089\u5b9f\u88c5\u30af\u30e9\u30b9\u306e\u5909\u63db\u3092\u884c\u3044\u307e\u3059\u3002<\/p>\n<p>\u3053\u3053\u3067\u8a00\u3046 ID \u3068\u306f\u3001@JsonTypeInfo \u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u306e use \u5c5e\u6027\u306e\u5b9a\u7fa9\u306b\u3088\u3063\u3066\u6c7a\u5b9a\u3059\u308b\u60c5\u5831\u3067\u3001\u30b5\u30f3\u30d7\u30eb\u3067\u306f \"@type\" \u30ad\u30fc\u306b\u5bfe\u3059\u308b\u5024\u3068\u3057\u3066\u66f8\u304d\u51fa\u3055\u308c\u305f\u8b58\u5225\u540d\u306e\u3053\u3068\u3067\u3059\u3002<\/p>\n<p>\u5b9f\u884c\u7d50\u679c\u306f\u3053\u308c\u3067\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true\">\r\n\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u7d50\u679c\uff1a\r\n{\"shape\":{\"@type\":\"R\",\"width\":100,\"height\":200}}\r\n\u30c7\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u7d50\u679c\u306b\u542b\u307e\u308c\u308b shape \u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u578b\uff1a\r\ncom.techscore.blog.example.Example4$Rectangle\r\n<\/pre>\n<p>TypeIdResolver \u306e\u5b9f\u88c5\u30af\u30e9\u30b9\u3092\u4f5c\u308b\u3053\u3068\u3067\u578b\u89e3\u6c7a\u306b\u95a2\u3059\u308b\u30ed\u30b8\u30c3\u30af\u304c\u66f8\u3051\u308b\u306e\u3067\u3001\u8907\u96d1\u306a\u578b\u89e3\u6c7a\u306b\u3082\u6700\u9069\u3067\u3059\u3002\u305d\u306e\u5f15\u304d\u63db\u3048\u3068\u3057\u3066\u30b3\u30fc\u30c9\u91cf\u304c\u591a\u304f\u306a\u308a\u3001\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u306e\u4f5c\u6210\u91cf\u304c\u5897\u3048\u3066\u3057\u307e\u3044\u307e\u3059\u3002<\/p>\n<h2>\u3010\u578b\u89e3\u6c7a\u65b9\u6cd55\u3011 \u30df\u30c3\u30af\u30b9\u30a4\u30f3<\/h2>\n<p>\u3044\u308f\u3086\u308b\u30df\u30c3\u30af\u30b9\u30a4\u30f3\u3067\u3059\u3002<\/p>\n<p>\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u306f MixIn \u30af\u30e9\u30b9\u3092\u4f7f\u3063\u3066\u578b\u89e3\u6c7a\u65b9\u6cd52\u3068\u540c\u3058\u3053\u3068\u3092\u5b9f\u73fe\u3057\u3066\u3044\u307e\u3059\uff08\u30b5\u30f3\u30d7\u30eb\u3067\u306f ShapeMixIn \u30af\u30e9\u30b9\uff09\u3002<\/p>\n<p>\u578b\u89e3\u6c7a\u306b\u306f\u3001\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u81ea\u8eab\u3067\u306f\u306a\u304f MixIn \u30af\u30e9\u30b9\u306b\u4ed8\u3051\u3089\u308c\u305f\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u304c\u4f7f\u308f\u308c\u307e\u3059\u3002<\/p>\n<pre class=\"lang:java decode:true\">\r\npackage com.techscore.blog.example;\r\n\r\nimport com.fasterxml.jackson.annotation.JsonSubTypes;\r\nimport com.fasterxml.jackson.annotation.JsonSubTypes.Type;\r\nimport com.fasterxml.jackson.annotation.JsonTypeInfo;\r\nimport com.fasterxml.jackson.annotation.JsonTypeInfo.Id;\r\nimport com.fasterxml.jackson.databind.ObjectMapper;\r\n\r\npublic class Example5 {\r\n\r\n    public Shape shape;\r\n\r\n    static interface Shape {}\r\n\r\n    static class Rectangle implements Shape {\r\n        public int width;\r\n        public int height;\r\n    }\r\n\r\n    static class Triangle implements Shape {\r\n        public int base;\r\n        public int height;\r\n    }\r\n\r\n    @JsonTypeInfo(use = Id.NAME)\r\n    @JsonSubTypes({\r\n        @Type(name = \"R\", value = Rectangle.class),\r\n        @Type(name = \"T\", value = Triangle.class)\r\n    })\r\n    static abstract class ShapeMixIn {}\r\n\r\n    public static void main(String[] args) throws Exception {\r\n        Rectangle rectangle = new Rectangle();\r\n        rectangle.width = 100;\r\n        rectangle.height = 200;\r\n\r\n        Example5 example = new Example5();\r\n        example.shape = rectangle;\r\n\r\n        ObjectMapper mapper = new ObjectMapper();\r\n        mapper.addMixIn(Shape.class, ShapeMixIn.class);\r\n\r\n        System.out.println(\"\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u7d50\u679c\uff1a\");\r\n        String json = mapper.writeValueAsString(example);\r\n        System.out.println(json);\r\n\r\n        System.out.println(\"\u30c7\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u7d50\u679c\u306b\u542b\u307e\u308c\u308b shape \u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u578b\uff1a\");\r\n        Example5 result = mapper.readValue(json, Example5.class);\r\n        System.out.println(result.shape.getClass().getName());\r\n    }\r\n}\r\n<\/pre>\n<p>ObjectMapper \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b MixIn \u30af\u30e9\u30b9\u3092\u767b\u9332\u3059\u308b\u306e\u3092\u5fd8\u308c\u306a\u3044\u3088\u3046\u306b\u3057\u307e\u3057\u3087\u3046\u3002\u3053\u308c\u306b\u3088\u3063\u3066\u3001ShapeMixIn \u30af\u30e9\u30b9\u306b\u4ed8\u3051\u3089\u308c\u305f @JsonTypeInfo \u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3068 @JsonSubTypes \u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u304c\u3001\u307e\u308b\u3067 Shape \u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u306b\u4ed8\u3044\u3066\u3044\u308b\u304b\u306e\u3088\u3046\u306b\u52d5\u4f5c\u3057\u307e\u3059\u3002<\/p>\n<p>\u5b9f\u884c\u7d50\u679c\u306f\u3053\u308c\u3002<\/p>\n<pre class=\"lang:sh decode:true\">\r\n\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u7d50\u679c\uff1a\r\n{\"shape\":{\"@type\":\"R\",\"width\":100,\"height\":200}}\r\n\u30c7\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u7d50\u679c\u306b\u542b\u307e\u308c\u308b shape \u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u578b\uff1a\r\ncom.techscore.blog.example.Example5$Rectangle\r\n<\/pre>\n<p>MixIn \u306f\u3001\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u306e\u8a2d\u8a08\u3068\u3001JSON \u306e\u30a8\u30f3\u30c6\u30a3\u30c6\u30a3\u8a2d\u8a08\u3092\u5206\u96e2\u3067\u304d\u3001\u30af\u30e9\u30b9\u8a2d\u8a08\u304c\u3059\u3063\u304d\u308a\u3057\u307e\u3059\u3002<\/p>\n<p>\u30b5\u30f3\u30d7\u30eb\u306b\u306f\u542b\u3081\u3066\u3044\u307e\u305b\u3093\u304c\u3001\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u306e\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u3084\u30e1\u30bd\u30c3\u30c9\u306b\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3092\u4ed8\u3051\u308b\u3088\u3046\u306a\u30b1\u30fc\u30b9\u3082\u3001MixIn \u30af\u30e9\u30b9\u306e\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u3084\u30e1\u30bd\u30c3\u30c9\u306b\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3092\u4ed8\u3051\u308b\u3053\u3068\u3067\u4ee3\u66ff\u3067\u304d\u307e\u3059\uff08\u30b9\u30bf\u30c6\u30a3\u30c3\u30af\u30e1\u30bd\u30c3\u30c9\u3082\u30df\u30c3\u30af\u30b9\u30a4\u30f3\u53ef\u80fd\u3067\u3059\uff09\u3002<\/p>\n<p>\u3042\u3048\u3066\u96e3\u70b9\u3092\u8a00\u3048\u3070\u3001\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u306e\u62e1\u5f35\u3084\u5909\u66f4\u306b\u5bfe\u3057\u3001MixIn \u30af\u30e9\u30b9\u306e\u5909\u66f4\u3092\u8ffd\u968f\u3055\u305b\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u30b1\u30fc\u30b9\u304c\u3042\u308b\u3053\u3068\u3067\u3059\u3002\u3053\u3053\u304c\u3001\u6539\u4fee\u6642\u306e\u5f71\u97ff\u7bc4\u56f2\u3068\u3057\u3066\u6f0f\u308c\u306a\u3044\u3088\u3046\u4f55\u3089\u304b\u306e\u5de5\u592b\u304c\u5fc5\u8981\u3067\u3059\u3002<\/p>\n<h2>\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u306b\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u304c\u3064\u3051\u3089\u308c\u306a\u3044\u5834\u5408\u306f\u3069\u3046\u3059\u308c\u3070\u3044\u3044\u306e\uff1f<\/h2>\n<p>Java \u306e\u57fa\u672c API \u3084\u3001\u5916\u90e8\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u30af\u30e9\u30b9\u3092\u6c4e\u5316\u3057\u305f\u578b\u3068\u3057\u3066\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u306e\u30d7\u30ed\u30d1\u30c6\u30a3\uff08\u30d5\u30a3\u30fc\u30eb\u30c9\uff09\u306b\u5b9a\u7fa9\u3059\u308b\u30b1\u30fc\u30b9\u3067\u306f\u3001\u5c11\u3005\u5de5\u592b\u304c\u5fc5\u8981\u3067\u3059\u3002<\/p>\n<p>\u3053\u3053\u307e\u3067\u306e\u578b\u89e3\u6c7a\u65b9\u6cd5\u306e\u307b\u3068\u3093\u3069\u304c\u3001\u5bfe\u8c61\u3068\u306a\u308b\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u306b @JsonTypeInfo \u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3092\u4ed8\u3051\u3066\u3044\u307e\u3057\u305f\u3002\u3057\u304b\u3057 Java \u306e\u57fa\u672c API \u3084\u5916\u90e8\u30e9\u30a4\u30d6\u30e9\u30ea\u306b\u542b\u307e\u308c\u308b\u30af\u30e9\u30b9\u306b\u306f\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3092\u3064\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u305b\u3093\u3002\u30df\u30c3\u30af\u30b9\u30a4\u30f3\u306a\u3089\u3053\u306e\u70b9\u306f\u89e3\u6c7a\u3057\u307e\u3059\u304c\u3001\u30df\u30c3\u30af\u30b9\u30a4\u30f3\u304c\u4f7f\u3048\u306a\u3044\u30b1\u30fc\u30b9\u3082\u3042\u308a\u3048\u307e\u3059\u3002<\/p>\n<p>\u5b9f\u306f\u3001@JsonTypeInfo \u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u306f\u30d7\u30ed\u30d1\u30c6\u30a3\uff08\u30d5\u30a3\u30fc\u30eb\u30c9\uff09\u306b\u3082\u4ed8\u3051\u308b\u3053\u3068\u304c\u51fa\u6765\u307e\u3059\u3002\u3064\u307e\u308a\u3001\u5bfe\u8c61\u30d7\u30ed\u30d1\u30c6\u30a3\uff08\u30d5\u30a3\u30fc\u30eb\u30c9\uff09\u306b\u5b9a\u7fa9\u3092\u66f8\u3051\u3070\u3044\u3044\u308f\u3051\u3067\u3059\u3002<\/p>\n<p>\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3067\u306f\u3001field1 \u30d5\u30a3\u30fc\u30eb\u30c9\u306b\u5bfe\u3057 @JsonTypeInfo \u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3092\u8ffd\u52a0\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:java decode:true\">\r\npackage com.techscore.blog.example;\r\n\r\nimport com.fasterxml.jackson.annotation.JsonTypeInfo;\r\nimport com.fasterxml.jackson.annotation.JsonTypeInfo.As;\r\nimport com.fasterxml.jackson.annotation.JsonTypeInfo.Id;\r\nimport com.fasterxml.jackson.databind.ObjectMapper;\r\n\r\npublic class Example6 {\r\n\r\n    @JsonTypeInfo(use = Id.CLASS, include=As.EXTERNAL_PROPERTY)\r\n    public Object field1;\r\n\r\n    public Object field2;\r\n\r\n    public static void main(String[] args) throws Exception {\r\n        Example6 example = new Example6();\r\n        example.field1 = 100L;\r\n        example.field2 = 100L;\r\n\r\n        ObjectMapper mapper = new ObjectMapper();\r\n\r\n        System.out.println(\"\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u7d50\u679c\uff1a\");\r\n        String json = mapper.writeValueAsString(example);\r\n        System.out.println(json);\r\n\r\n        System.out.println(\"\u30c7\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u7d50\u679c\uff1a\");\r\n        Example6 result = mapper.readValue(json, Example6.class);\r\n        System.out.println(\"field1: \" + result.field1.getClass().getName());\r\n        System.out.println(\"field2: \" + result.field2.getClass().getName());\r\n    }\r\n}\r\n<\/pre>\n<p>\u540c\u3058 Object \u578b\u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u3067\u3042\u308b field1 \u3068 field2 \u306b\u5bfe\u3057\u3001main() \u30e1\u30bd\u30c3\u30c9\u5185\u3067\u3068\u3082\u306b Long \u578b\u306e 100 \u3092\u4ee3\u5165\u3057\u3001\u305d\u308c\u3092\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u3057\u3066\u304b\u3089\u30c7\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u7d50\u679c\u306f\u6b21\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002 <\/p>\n<pre class=\"lang:sh decode:true\">\r\n\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u7d50\u679c\uff1a\r\n{\"field1\":100,\"@class\":\"java.lang.Long\",\"field2\":100}\r\n\u30c7\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u7d50\u679c\uff1a\r\nfield1: java.lang.Long\r\nfield2: java.lang.Integer\r\n<\/pre>\n<p>field1 \u306f Long \u578b\u3068\u3057\u3066\u30c7\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u3055\u308c\u3001field2 \u306f Integer \u578b\u3068\u3057\u3066\u30c7\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u3053\u308c\u306f\u3001field1 \u306b\u5bfe\u3057\u3066 @JsonTypeInfo \u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3067 \"@class\" \u30ad\u30fc\u3067\u5b9f\u88c5\u30af\u30e9\u30b9\u540d\u3092\u66f8\u304d\u51fa\u3059\u3088\u3046\u306b\u3057\u305f\u304b\u3089\u3067\u3059\u3002<\/p>\n<p>\u3053\u3053\u3067\u809d\u3068\u306a\u308b\u306e\u306f\u3001@JsonTypeInfo \u306e include \u5c5e\u6027\u306b JsonTypeInfo.As.EXTERNAL_PROPERTY \u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u3067\u3059\u3002\u3053\u308c\u306b\u3088\u3063\u3066\u30ad\u30fc \u201d@type\u201d \u3084 \"@class\" \u304c\u3001\u5bfe\u8c61\u30d7\u30ed\u30d1\u30c6\u30a3\uff08\u30d5\u30a3\u30fc\u30eb\u30c9\uff09\u3068\u540c\u5217\u306e\u30e1\u30f3\u30d0\u30fc\u3068\u3057\u3066\u633f\u5165\u3055\u308c\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h2>\u6700\u5f8c\u306b<\/h2>\n<p>\u672c\u6587\u3067\u306f\u89e6\u308c\u307e\u305b\u3093\u3067\u3057\u305f\u304c\u3001JSON \u306b\u81ea\u52d5\u633f\u5165\u3055\u308c\u308b \"@type\" \u3084 \u201c@class\u201d \u3068\u3044\u3063\u305f\u30ad\u30fc\u306f\u3001@JsonTypeInfo \u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u306e property \u5c5e\u6027\u3067\u5909\u66f4\u3067\u304d\u307e\u3059\u3002\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u9762\u304b\u3089\u3082\u3001\u3053\u306e\u5c5e\u6027\u3092\u4f7f\u3063\u3066\u30ad\u30fc\u3092\u5909\u66f4\u3057\u3066\u304a\u3044\u305f\u65b9\u304c\u826f\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n<p>Jackson \u306b\u306f\u3001\u3053\u3053\u3067\u7d39\u4ecb\u3057\u306a\u304b\u3063\u305f\u578b\u89e3\u6c7a\u65b9\u6cd5\u3082\u3044\u304f\u3064\u304b\u3042\u308a\u307e\u3059\u3002\u57f7\u7b46\u5f53\u521d\u306f Serializer \u3084 Deserializer \u3092\u4f7f\u3063\u305f\u578b\u89e3\u6c7a\u306b\u3064\u3044\u3066\u3082\u6271\u3046\u3064\u3082\u308a\u3067\u3057\u305f\u304c\u3001\u8a18\u4e8b\u304c\u9577\u304f\u306a\u308a\u3059\u304e\u305f\u306e\u3067\u8ae6\u3081\u307e\u3057\u305f\u3002<\/p>\n<p>\u305d\u308c\u306b\u3057\u3066\u3082\u3001Java \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068 JSON \u306e\u30de\u30c3\u30d4\u30f3\u30b0\u3082O\/R \u30de\u30c3\u30d4\u30f3\u30b0\u540c\u69d8\u3067\u4e00\u7b4b\u7e04\u3067\u306f\u3044\u304b\u306a\u3044\u3082\u306e\u3067\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3002\u677e\u672c\u3067\u3059\u3002<\/p>\n<p>\u30dd\u30ea\u30e2\u30fc\u30d5\u30a3\u30ba\u30e0\u306f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u6307\u5411\u8a00\u8a9e\u306e\u9b45\u529b\u306e\u3072\u3068\u3064\u3067\u3059\u304c\u3001\u3053\u308c\u304c\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068 JSON \u306e\u30de\u30c3\u30d4\u30f3\u30b0\u3092\u8907\u96d1\u306b\u3059\u308b\u306a\u3042\u3068\u5ea6\u3005\u611f\u3058\u3066\u3044\u307e\u3059\u3002<br \/><a href=\"https:\/\/www.techscore.com\/blog\/2016\/06\/17\/java-jackson-polymorphic-deserialization\/\">\u7d9a\u304d\u3092\u8aad\u3080...<\/a><\/p>\n","protected":false},"author":14,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[18],"tags":[57],"_links":{"self":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/10592"}],"collection":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/comments?post=10592"}],"version-history":[{"count":47,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/10592\/revisions"}],"predecessor-version":[{"id":10731,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/10592\/revisions\/10731"}],"wp:attachment":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/media?parent=10592"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/categories?post=10592"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/tags?post=10592"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}