{"action":"created","issue":{"url":"https://api.github.com/repos/FasterXML/jackson-databind/issues/1298","repository_url":"https://api.github.com/repos/FasterXML/jackson-databind","labels_url":"https://api.github.com/repos/FasterXML/jackson-databind/issues/1298/labels{/name}","comments_url":"https://api.github.com/repos/FasterXML/jackson-databind/issues/1298/comments","events_url":"https://api.github.com/repos/FasterXML/jackson-databind/issues/1298/events","html_url":"https://github.com/FasterXML/jackson-databind/issues/1298","id":165634076,"number":1298,"title":"JsonGenerationException: expected field name while writing ObjectId reference","user":{"login":"JeffQuandt","id":20460169,"avatar_url":"https://avatars0.githubusercontent.com/u/20460169?v=3","gravatar_id":"","url":"https://api.github.com/users/JeffQuandt","html_url":"https://github.com/JeffQuandt","followers_url":"https://api.github.com/users/JeffQuandt/followers","following_url":"https://api.github.com/users/JeffQuandt/following{/other_user}","gists_url":"https://api.github.com/users/JeffQuandt/gists{/gist_id}","starred_url":"https://api.github.com/users/JeffQuandt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JeffQuandt/subscriptions","organizations_url":"https://api.github.com/users/JeffQuandt/orgs","repos_url":"https://api.github.com/users/JeffQuandt/repos","events_url":"https://api.github.com/users/JeffQuandt/events{/privacy}","received_events_url":"https://api.github.com/users/JeffQuandt/received_events","type":"User","site_admin":false},"labels":[{"id":362288980,"url":"https://api.github.com/repos/FasterXML/jackson-databind/labels/2.9","name":"2.9","color":"0e8a16","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":23,"created_at":"2016-07-14T19:01:21Z","updated_at":"2017-06-19T19:56:29Z","closed_at":null,"body":"Issue submission requested by Tatu from jackson-user list question: https://groups.google.com/forum/#!topic/jackson-user/GOj0uGn_nTg\n\nI'm using Jackson 2.8.0 to serialize some JPA Entities over a REST API with Spring Data REST. I'm having a problem with serializing an Array of Entities that contain references to the same child object. All objects are using JsonIdentityInfo as below, where the key property is a Long @Id field.\n\n`@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = \"key\", scope = MyClass.class)`\n\nI believe the expected JSON should look like the below. The relevant object is supportEmailAddress.\n\n```\n\"companies\" : [ {\n  \"key\" : 1,\n  \"name\" : \"company1\",\n  \"supportEmailAddress\" : {\n    \"key\" : 1,\n    \"emailAddress\" : \"email@support.com\"\n  }\n  \"owner\" : {\n    \"key\" : 1,\n    \"name\" : \"owner 1\",\n    \"_links\" : {\n      \"company\" : {\n        \"href\" : \"http://localhost:8080/company/1\"\n      }\n    }\n  }\n}, {\n  \"key\" : 2,\n  \"name\" : \"company2\",\n  \"supportEmailAddress\" : {[1]}\n  \"owner\" : {\n    \"key\" : 2,\n    \"name\" : \"owner 2\",\n    \"_links\" : {\n      \"company\" : {\n        \"href\" : \"http://localhost:8080/company/2\"\n      }\n    }\n  }\n```\n\nThe first time the child object is referenced, it serializes correctly. The second time, it fails with the stack trace below.\n\n```\nCaused by: com.fasterxml.jackson.core.JsonGenerationException: Can not write a number, expecting field name\nat com.fasterxml.jackson.core.JsonGenerator._reportError(JsonGenerator.java:1676)\nat com.fasterxml.jackson.core.json.UTF8JsonGenerator._verifyValueWrite(UTF8JsonGenerator.java:925)\nat com.fasterxml.jackson.core.json.UTF8JsonGenerator.writeNumber(UTF8JsonGenerator.java:787)\nat com.fasterxml.jackson.databind.ser.std.NumberSerializers$LongSerializer.serialize(NumberSerializers.java:188)\nat com.fasterxml.jackson.databind.ser.impl.WritableObjectId.writeAsId(WritableObjectId.java:35)\nat com.fasterxml.jackson.databind.ser.std.BeanSerializerBase._serializeWithObjectId(BeanSerializerBase.java:584)\nat com.fasterxml.jackson.databind.ser.impl.UnwrappingBeanSerializer.serialize(UnwrappingBeanSerializer.java:114)\nat com.fasterxml.jackson.databind.ser.impl.UnwrappingBeanPropertyWriter.serializeAsField(UnwrappingBeanPropertyWriter.java:127)\nat com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:678)\nat com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:157)\nat com.fasterxml.jackson.databind.SerializerProvider.defaultSerializeValue(SerializerProvider.java:985)\nat org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module$PersistentEntityResourceSerializer.serialize(PersistentEntityJackson2Module.java:193)\nat org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module$PersistentEntityResourceSerializer.serialize(PersistentEntityJackson2Module.java:140)\nat com.fasterxml.jackson.databind.SerializerProvider.defaultSerializeValue(SerializerProvider.java:985)\nat org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module$NestedEntitySerializer.serialize(PersistentEntityJackson2Module.java:356)\nat com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:672)x\nat com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:678)\nat com.fasterxml.jackson.databind.ser.std.BeanSerializerBase._serializeWithObjectId(BeanSerializerBase.java:600)\nat com.fasterxml.jackson.databind.ser.impl.UnwrappingBeanSerializer.serialize(UnwrappingBeanSerializer.java:114)\nat com.fasterxml.jackson.databind.ser.impl.UnwrappingBeanPropertyWriter.serializeAsField(UnwrappingBeanPropertyWriter.java:127)\nat com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:678)\nat com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:157)\nat com.fasterxml.jackson.databind.SerializerProvider.defaultSerializeValue(SerializerProvider.java:985)\nat org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module$PersistentEntityResourceSerializer.serialize(PersistentEntityJackson2Module.java:193)\nat org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module$PersistentEntityResourceSerializer.serialize(PersistentEntityJackson2Module.java:140)\nat com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.java:119)\nat com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serialize(IndexedListSerializer.java:79)\nat com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serialize(IndexedListSerializer.java:18)\nat com.fasterxml.jackson.databind.ser.std.MapSerializer.serializeFields(MapSerializer.java:616)\nat com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:519)\nat com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:31)\nat org.springframework.hateoas.hal.Jackson2HalModule$HalResourcesSerializer.serialize(Jackson2HalModule.java:340)\nat org.springframework.hateoas.hal.Jackson2HalModule$HalResourcesSerializer.serialize(Jackson2HalModule.java:302)\nat com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:672)\nat com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:678)\nat com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:157)\nat com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:130)\nat com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1428)\nat com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:930)\nat org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:269)\n... 57 more\n```\n\nIf I remove the JsonIdentityInfo, I get the results back correctly. However, there are no Object references, and the full child object is serialized every time it is referenced. I'm concerned that this \"fix\" is not effective, because I originally added the JsonIdentityInfo annotations to get around some recursive relationships (ORM bi-directional stuff). If I have to remove them every time I might refer to the same instance in a result set, what is the point of the JsonIdentityInfo annotation in the first place? \n\nI'm sure I'm just applying something incorrectly, but I'm not able to find documentation on what I may be missing. Do I need a custom serializer to make use of this feature?\n\nAttaching minimal example project. Extract and execute gradlew bootRun. Navigate to `localhost:8080/company` to see the bug. Full Stack trace is not logged by default and would require debugging or custom exception handler to be applied. Error message is visible on console. Navigating to individual objects (e.g. /company/1) works correctly. \n\nTo validate /company works without the duplicate child object, edit rest-api/src/main/resources/data.sql and either set SUPPORT_EMAIL_ADDRESS null or to 3 for company 1 or company 3 and restart the application.\n\nEx: \n`2016-07-14T13:50:49,734 WARN  [mvc.support.DefaultHandlerExceptionResolver] [http-nio-8080-exec-2] Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: Can not write a number, expecting field name; nested exception is com.fasterxml.jackson.core.JsonGenerationException: Can not write a number, expecting field name`\n\n[databind-bug.zip](https://github.com/FasterXML/jackson-databind/files/364596/databind-bug.zip)\n"},"comment":{"url":"https://api.github.com/repos/FasterXML/jackson-databind/issues/comments/309555394","html_url":"https://github.com/FasterXML/jackson-databind/issues/1298#issuecomment-309555394","issue_url":"https://api.github.com/repos/FasterXML/jackson-databind/issues/1298","id":309555394,"user":{"login":"gledsonrabelo","id":765907,"avatar_url":"https://avatars0.githubusercontent.com/u/765907?v=3","gravatar_id":"","url":"https://api.github.com/users/gledsonrabelo","html_url":"https://github.com/gledsonrabelo","followers_url":"https://api.github.com/users/gledsonrabelo/followers","following_url":"https://api.github.com/users/gledsonrabelo/following{/other_user}","gists_url":"https://api.github.com/users/gledsonrabelo/gists{/gist_id}","starred_url":"https://api.github.com/users/gledsonrabelo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gledsonrabelo/subscriptions","organizations_url":"https://api.github.com/users/gledsonrabelo/orgs","repos_url":"https://api.github.com/users/gledsonrabelo/repos","events_url":"https://api.github.com/users/gledsonrabelo/events{/privacy}","received_events_url":"https://api.github.com/users/gledsonrabelo/received_events","type":"User","site_admin":false},"created_at":"2017-06-19T19:56:29Z","updated_at":"2017-06-19T19:56:29Z","body":"+1"}}
{"actor":{"display_login":"gledsonrabelo"}}