Skip to content

Instantly share code, notes, and snippets.

@lucasnata
Created June 15, 2022 18:06
Show Gist options
  • Save lucasnata/97bb8f77f9e9c9493245d464b0b341d6 to your computer and use it in GitHub Desktop.
Save lucasnata/97bb8f77f9e9c9493245d464b0b341d6 to your computer and use it in GitHub Desktop.
HashMap to List JSON output
public class HashMapSerializer extends JsonSerializer<Map<Long, Object>> {
ObjectMapper mapper = new ObjectMapper();
@Override
public void serialize(Map<Long, Object> userMap, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeString(mapper.writeValueAsString(userMap.values()));
}
}
...
@JsonSerialize(using = HashSerializer.class)
private final Map<Long, Product> products;
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment