0

I have a Java map Map<String, String> map = Collections.singletonMap("dummyKey", "dummyValue"). When I use ObjectMapper to serialize it, I get {"dummyKey":"dummyValue"}. Is there any way that I can insert JSON keys in the process of serialization so my JSON string looks like {"key":"dummyKey", "value":"dummyValue"}.

I'd like to be able to do this without having to create a separate class, if possible. I'm looking for something like the allow explicit property renaming feature.

I looked at the other mapper features provided, but can't find anything useful. I don't want to create a different class because I want to retain the ability to be able to use it as a map (look up by key) when I deserialize it later. I'm looking for something that'll only change the way serialization and deserialization happen.

  • Create new map. You can iterate over old one and foreach element add new key value to new map with values from old one. – z48o0 Dec 08 '22 at 10:21
  • You could write a custom serializer for the Map class, see e.g. https://stackoverflow.com/a/10835504/6465315. I had a similar problem. Like @z48o0 suggested i created an object with my desired structure and then serialized it. I assume there is not much usage to this use case because it is limited to a single Map.Entry... – RainerZufall Dec 08 '22 at 10:54
  • Thank you, z48o0 and RainerZufall. But I'm looking for something that I can use to process billions of records. Creating an object to map to and from just for serializing and de-serializing can't be the approach I use. – Polyphonic Mobius Dec 08 '22 at 19:23

0 Answers0