D
D
dc65k2021-06-20 13:56:13
Java
dc65k, 2021-06-20 13:56:13

How to dynamically populate data in Java?

Hello. I have an example of the formation of a certain structure in Java.
Tell me, is there a way to achieve a similar result somehow more flexibly (dynamically)?
Initial data (JSON representation):

[
  {
    "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"
    }
  }
]

Below I have described the class that forms the initial data structure:
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);
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BorLaze, 2021-06-20
@BorLaze

What does "more flexible" mean?
If you want to add a new field to "data", for example, but do not want to change the java code, then you will have to use Map<String, String> instead of a specific DataObj.
Flexibility will be added, but specificity will be lost.
You have to pay for everything.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question