{"action":"closed","issue":{"url":"https://api.github.com/repos/FasterXML/jackson-docs/issues/5","repository_url":"https://api.github.com/repos/FasterXML/jackson-docs","labels_url":"https://api.github.com/repos/FasterXML/jackson-docs/issues/5/labels{/name}","comments_url":"https://api.github.com/repos/FasterXML/jackson-docs/issues/5/comments","events_url":"https://api.github.com/repos/FasterXML/jackson-docs/issues/5/events","html_url":"https://github.com/FasterXML/jackson-docs/issues/5","id":175013965,"number":5,"title":"custom serialization of map attributes","user":{"login":"amnonkhen","id":927145,"avatar_url":"https://avatars.githubusercontent.com/u/927145?v=3","gravatar_id":"","url":"https://api.github.com/users/amnonkhen","html_url":"https://github.com/amnonkhen","followers_url":"https://api.github.com/users/amnonkhen/followers","following_url":"https://api.github.com/users/amnonkhen/following{/other_user}","gists_url":"https://api.github.com/users/amnonkhen/gists{/gist_id}","starred_url":"https://api.github.com/users/amnonkhen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/amnonkhen/subscriptions","organizations_url":"https://api.github.com/users/amnonkhen/orgs","repos_url":"https://api.github.com/users/amnonkhen/repos","events_url":"https://api.github.com/users/amnonkhen/events{/privacy}","received_events_url":"https://api.github.com/users/amnonkhen/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-09-05T07:50:15Z","updated_at":"2016-09-05T17:32:49Z","closed_at":"2016-09-05T17:32:49Z","body":"I have an object with a map attribute with nested keys, e.g.\r\n- k1 ->\r\n  - nested11 -> value11\r\n  - nested12 -> value12\r\n- k2 ->\r\n  - nested21 -> value21\r\n  - nested22 -> value22\r\n\r\nI need it to be serialized to JSON in a flattened manner, i.e.:\r\n```\r\n{\r\n  \"k1.nested11\": \"value11\",\r\n  \"k1.nested12\": \"value12\",\r\n  \"k2.nested21\": \"value11\",\r\n  \"k2.nested22\": \"value22\",\r\n}\r\n```\r\n\r\nIs there anyway to achieve this without using custom serialization to the entire object?\r\nI thought of some combination of `@JsonAnyGetter` and `@JsonSerialize`, but it does not work.\r\n\r\nHere's what I tried in my custom serializer for the Map property:\r\n\r\n```\r\n    @Override\r\n    public void serialize(Map<String, Object> map, \r\n                           JsonGenerator jgen, \r\n                          SerializerProvider provider) \r\n                throws IOException, JsonProcessingException\r\n {\r\n        map.forEach((k, v) -> {\r\n            try {\r\n                jgen.writeObjectField(k, v);\r\n            } catch (IOException e) {\r\n                logger.warn(\"problem serializing field [\" + k + \"] with value [\" + v+\"]\", e);\r\n            }\r\n        });\r\n    }\r\n```\r\n\r\nI am getting the following error message:\r\n\r\n```\r\n2016-09-05 10:51:08,348 [main] WARN  MapJsonSerializer - problem writing field [swapLegs.1.nested1_k1] with value [nested1_v1]\r\ncom.fasterxml.jackson.core.JsonGenerationException: Can not write a field name, expecting a value\r\n\tat com.fasterxml.jackson.core.base.GeneratorBase._reportError(GeneratorBase.java:412)\r\n\tat com.fasterxml.jackson.core.json.WriterBasedJsonGenerator.writeFieldName(WriterBasedJsonGenerator.java:195)\r\n\tat com.fasterxml.jackson.core.JsonGenerator.writeObjectField(JsonGenerator.java:1018)\r\n```\r\n\r\nAm I using ` jgen.writeObjectField(k, v)` incorrectly?\r\nCan I call it a few times and add several properties?"}}
{"actor":{"display_login":"cowtowncoder"}}