Из ответа Михаила просто напишите json, используя Jackson ObjectMapper, и вы получите свой формат json:
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(YamlTestApplication.class, args);
Demo demo = (Demo) context.getBean("demo");
System.out.println("name: " + demo);
ObjectMapper mapper = new ObjectMapper();
try {
String test = mapper.writeValueAsString(demo);
System.out.println("json: "+test);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
вывод:
name: Demo [a=[A [type=A, prop1=1, prop2=2, proop3=3], A [type=B, prop1=1, prop2=2, proop3=3]], b=[B [type=A, prop1=1, prop2=2, proop3=3], B [type=B, prop1=1, prop2=2, proop3=3]]]
json: {"A":[{"type":"A","prop1":1,"prop2":2,"proop3":3},{"type":"B","prop1":1,"prop2":2,"proop3":3}],"B":[{"type":"A","prop1":1,"prop2":2,"proop3":3},{"type":"B","prop1":1,"prop2":2,"proop3":3}]}