Всем привет. У меня есть пример формирования некой структуры в Java.
Подскажите, есть ли способ добиться аналогичного результат как-то более гибко (динамично)?
Исходные данные (JSON представление):
[
{
"type": "Title",
"data": {
"title": "title1",
"subTitle": "subTitle1"
}
},
{
"type": "Title",
"data": {
"title": "title2",
"subTitle": "subTitle2"
}
},
{
"type": "Title",
"data": {
"title": "title3",
"subTitle": "subTitle3"
}
},
{
"type": "TitleTwo",
"data": {
"title": "TitleTwo title",
"subTitle": "TitleTwo subTitle"
}
},
{
"type": "TitleTwo",
"data": {
"title": "TitleTwo title",
"subTitle": "TitleTwo subTitle"
}
},
{
"type": "New",
"data": {
"title": "new title1",
"subTitle": "new subTitle1"
}
},
{
"type": "New",
"data": {
"title": "new title2",
"subTitle": "new subTitle2"
}
},
{
"type": "NewTitle",
"data": {
"title": "NewTitle title1",
"subTitle": "NewTitle subTitle1"
}
},
{
"type": "NewTitleTwo",
"data": {
"title": "NewTitleTwo title",
"subTitle": "NewTitleTwo subTitle"
}
}
]
Ниже я описал класс, который формирует исходную структуру данных:
package com.example.demo;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
@lombok.Data
@lombok.NoArgsConstructor
@lombok.AllArgsConstructor
@JsonAutoDetect
class DataObj {
private String title;
private String subTitle;
}
@lombok.Data
@lombok.NoArgsConstructor
@lombok.AllArgsConstructor
@JsonAutoDetect
class Widget {
private String type;
private DataObj data;
}
public class Test {
public static void main(String[] args) throws IOException {
List<Widget> widgets = new ArrayList<>();
DataObj titleWidgetData = new DataObj("title1", "subTitle1");
DataObj titleWidgetData2 = new DataObj("title2", "subTitle2");
DataObj titleWidgetData3 = new DataObj("title3", "subTitle3");
Widget titleWidget = new Widget("Title", titleWidgetData);
Widget titleWidget2 = new Widget("Title", titleWidgetData2);
Widget titleWidget3 = new Widget("Title", titleWidgetData3);
widgets.add(titleWidget);
widgets.add(titleWidget2);
widgets.add(titleWidget3);
DataObj titleTwoWidgetData = new DataObj("TitleTwo title", "TitleTwo subTitle");
DataObj titleTwoWidgetData2 = new DataObj("TitleTwo title", "TitleTwo subTitle");
Widget titleTwoWidget = new Widget("TitleTwo", titleTwoWidgetData);
Widget titleTwoWidget2 = new Widget("TitleTwo", titleTwoWidgetData2);
widgets.add(titleTwoWidget);
widgets.add(titleTwoWidget2);
DataObj newWidgetData = new DataObj("new title1", "new subTitle1");
DataObj newWidgetData2 = new DataObj("new title2", "new subTitle2");
Widget newWidget = new Widget("New", newWidgetData);
Widget newWidget2 = new Widget("New", newWidgetData2);
widgets.add(newWidget);
widgets.add(newWidget2);
DataObj newTitleWidgetData = new DataObj("NewTitle title1", "NewTitle subTitle1");
Widget newTitleWidget = new Widget("NewTitle", newTitleWidgetData);
widgets.add(newTitleWidget);
DataObj newTitleTwoWidgetData = new DataObj("NewTitleTwo title", "NewTitleTwo subTitle");
Widget newTitleTwoWidget = new Widget("NewTitleTwo", newTitleTwoWidgetData);
widgets.add(newTitleTwoWidget);
System.out.println(widgets);
StringWriter writer = new StringWriter();
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(writer, widgets);
String result = writer.toString();
System.out.println(result);
}
}