{"action":"created","issue":{"url":"https://api.github.com/repos/FasterXML/jackson-databind/issues/266","labels_url":"https://api.github.com/repos/FasterXML/jackson-databind/issues/266/labels{/name}","comments_url":"https://api.github.com/repos/FasterXML/jackson-databind/issues/266/comments","events_url":"https://api.github.com/repos/FasterXML/jackson-databind/issues/266/events","html_url":"https://github.com/FasterXML/jackson-databind/issues/266","id":16886676,"number":266,"title":"Deserialize Json Array that contin 2 objects with the same ID - JsonMappingException: Already had POJO for id","user":{"login":"luisvt","id":1688987,"avatar_url":"https://avatars.githubusercontent.com/u/1688987?v=3","gravatar_id":"","url":"https://api.github.com/users/luisvt","html_url":"https://github.com/luisvt","followers_url":"https://api.github.com/users/luisvt/followers","following_url":"https://api.github.com/users/luisvt/following{/other_user}","gists_url":"https://api.github.com/users/luisvt/gists{/gist_id}","starred_url":"https://api.github.com/users/luisvt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/luisvt/subscriptions","organizations_url":"https://api.github.com/users/luisvt/orgs","repos_url":"https://api.github.com/users/luisvt/repos","events_url":"https://api.github.com/users/luisvt/events{/privacy}","received_events_url":"https://api.github.com/users/luisvt/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":16,"created_at":"2013-07-17T20:08:13Z","updated_at":"2015-01-22T21:18:17Z","closed_at":null,"body":"How can I deserialize an Array with two objects that contains the same SubObject with the same Id?\r\n\r\nFor example this is the JSON of the array\r\n```\r\n[\r\n    {\r\n        \"@id\": 98,\r\n        \"age\": 29,\r\n        \"name\": \"mkyong\",\r\n        \"relatedPackage\": {\"@id\":99, \"receivedOn\":1374012807237 }\r\n    },\r\n    {\r\n        \"@id\": 101,\r\n        \"age\": 25,\r\n        \"name\": \"luis\",\r\n        \"relatedPackage\": {\"@id\":99, \"receivedOn\":1374012807237 }\r\n    }\r\n]\r\n```\r\n\r\nAs you can see both objects has the same related package. I want that the deserializer only parse the first relatedPackage and use it for the secon relatedPackage.\r\n\r\nMy Pojos are the next:\r\n\r\nUser Class:\r\n```\r\npackage com.mkyong.core;\r\n \r\nimport com.fasterxml.jackson.annotation.JsonIdentityInfo;\r\nimport com.fasterxml.jackson.annotation.ObjectIdGenerators;\r\n\r\n@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class,property=\"@id\")\r\npublic class User {\r\n \r\n\tpublic int age;\r\n\tpublic String name;\r\n\tpublic RelatedPackage relatedPackage;\r\n \r\n\t//getter and setter methods\r\n \r\n\t@Override\r\n\tpublic String toString() {\r\n\t\treturn \"User [age=\" + age + \", name=\" + name + \", \" +\r\n\t\t\t\t\"messages=\" + relatedPackage + \"]\";\r\n\t}\r\n}\r\n```\r\n\r\nRelatedPackage Class:\r\n```\r\npackage com.mkyong.core;\r\n\r\nimport com.fasterxml.jackson.annotation.JsonIdentityInfo;\r\nimport com.fasterxml.jackson.annotation.ObjectIdGenerators;\r\n\r\n@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class,property=\"@id\")\r\npublic class RelatedPackage {\r\n\tpublic long receivedOn;\r\n\t\r\n\tpublic String toString() {\r\n\t\treturn \"{ receivedOn: \" + receivedOn + \" }\";\r\n\t}\r\n}\r\n```\r\n\r\nAnd finaly the main Object wich realize the deserializtion:\r\n```\r\npackage com.mkyong.core;\r\n \r\nimport java.io.File;\r\nimport java.io.IOException;\r\n\r\nimport com.fasterxml.jackson.core.JsonGenerationException;\r\nimport com.fasterxml.jackson.databind.JsonMappingException;\r\nimport com.fasterxml.jackson.databind.ObjectMapper;\r\n\r\n\r\npublic class JacksonExample {\r\n    public static void main(String[] args) {\r\n \r\n\tObjectMapper mapper = new ObjectMapper();\r\n \r\n\ttry {\r\n \r\n\t\t// read from file, convert it to user class\r\n\t\tFile file = new File(\"/home/lvargas/user.json\");\r\n\t\tUser[] userList = mapper.readValue(file, User[].class);\r\n \r\n\t\t// display to console\r\n\t\tSystem.out.println(userList);\r\n \r\n\t} catch (JsonGenerationException e) {\r\n \r\n\t\te.printStackTrace();\r\n \r\n\t} catch (JsonMappingException e) {\r\n \r\n\t\te.printStackTrace();\r\n \r\n\t} catch (IOException e) {\r\n \r\n\t\te.printStackTrace();\r\n \r\n\t}\r\n \r\n  }\r\n \r\n}\r\n```\r\n\r\nWhen both relatedPackage has the same Id it sends me the next error message\r\n\r\n```\r\ncom.fasterxml.jackson.databind.JsonMappingException: Already had POJO for id (java.lang.Integer) [99] (through reference chain: com.mkyong.core.User[\"relatedPackage\"]->com.mkyong.core.RelatedPackage[\"@id\"])\r\n\tat com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:232)\r\n\tat com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:197)\r\n\tat com.fasterxml.jackson.databind.deser.BeanDeserializerBase.wrapAndThrow(BeanDeserializerBase.java:1332)\r\n\tat com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:252)\r\n\tat com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:115)\r\n\tat com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:449)\r\n\tat com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:107)\r\n\tat com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:250)\r\n\tat com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:115)\r\n\tat com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:151)\r\n\tat com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:18)\r\n\tat com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)\r\n\tat com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1988)\r\n\tat com.mkyong.core.JacksonExample.main(JacksonExample.java:20)\r\nCaused by: java.lang.IllegalStateException: Already had POJO for id (java.lang.Integer) [99]\r\n\tat com.fasterxml.jackson.databind.deser.impl.ReadableObjectId.bindItem(ReadableObjectId.java:27)\r\n\tat com.fasterxml.jackson.databind.deser.impl.ObjectIdValueProperty.deserializeSetAndReturn(ObjectIdValueProperty.java:91)\r\n\tat com.fasterxml.jackson.databind.deser.impl.ObjectIdValueProperty.deserializeAndSet(ObjectIdValueProperty.java:80)\r\n\tat com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:250)\r\n\t... 10 more\r\n```\r\n\r\nI understand that it is trying to create a second object with the same objectId.\r\n\r\nHow can I solve that problem?\r\n"},"comment":{"url":"https://api.github.com/repos/FasterXML/jackson-databind/issues/comments/71100671","html_url":"https://github.com/FasterXML/jackson-databind/issues/266#issuecomment-71100671","issue_url":"https://api.github.com/repos/FasterXML/jackson-databind/issues/266","id":71100671,"user":{"login":"jfitzpatrick99","id":5713465,"avatar_url":"https://avatars.githubusercontent.com/u/5713465?v=3","gravatar_id":"","url":"https://api.github.com/users/jfitzpatrick99","html_url":"https://github.com/jfitzpatrick99","followers_url":"https://api.github.com/users/jfitzpatrick99/followers","following_url":"https://api.github.com/users/jfitzpatrick99/following{/other_user}","gists_url":"https://api.github.com/users/jfitzpatrick99/gists{/gist_id}","starred_url":"https://api.github.com/users/jfitzpatrick99/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jfitzpatrick99/subscriptions","organizations_url":"https://api.github.com/users/jfitzpatrick99/orgs","repos_url":"https://api.github.com/users/jfitzpatrick99/repos","events_url":"https://api.github.com/users/jfitzpatrick99/events{/privacy}","received_events_url":"https://api.github.com/users/jfitzpatrick99/received_events","type":"User","site_admin":false},"created_at":"2015-01-22T21:18:17Z","updated_at":"2015-01-22T21:18:17Z","body":"For my particular use case, the resulting deserialized object graph is denormalized so that I literally end up with multiple instances of the same object. This works well for me and might work well for others. If this approach were to be incorporated into Jackson, I would suggest adding a  \"DeserializationFeature\" such as \"DENORMALIZE_DUPLICATE_OBJECT_IDS\".\r\n\r\nObviously the default would be to have the feature disabled but if enabled, Jackson would simply ignore objects with same ID during deserialization and create multiple object instances representing the same object.\r\n\r\nFor implementation, it would just be necessary to change the implementation of the DefaultDeserializationContext.findObjectId according to whether the feature is enabled. Although I am not super familiar with the Jackson code base so there may be a better place to make this change."}}