简体   繁体   English

如何使用 Lombok + Gson 创建 JSON object 阵列?

[英]How do you create an JSON object array using Lombok + Gson?

Description : I am using Lombok's @Builder annotation to "build" a JSON payload, and then gson to convert it to a proper JSON output. Description : I am using Lombok's @Builder annotation to "build" a JSON payload, and then gson to convert it to a proper JSON output.

But how do I build an array via the builder() method?但是如何通过builder()方法构建一个数组呢?

Code:代码:

        RoleType RoleType = RoleType.getEnumByUserRole("Marketing");
        PropertyBean PropertyBean = ConfigFactory.create(PropertyBean.class);
        String defaultStore = "null";

        //this is the object that's suppose to be an array.
        Group group = Group.builder()
                .groupId(RoleType.getGroupId())
                .changedById(PropertyBean.sssUser())
                .storeCode(defaultStore)
                .primary(false).build();

        String lastName = "QA User";
        int numOfDays = 1;
        String defaultLocale = "en";
        User newUser = User.builder()
                .firstName(RoleType.getAcronym())
                .lastName(lastName)
                .startDay(getCurrentYearMonthDate())
                .endDay(addDaysToYearMonthDate(numOfDays))
                .password(PropertyBean.tempPass())
                .rightHand(true)
                .operatorId("123456")
                .userStores(userGroup) //<--- the userStoes object should be an array.
                .locale(defaultLocale).build();

        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String body = gson.toJson(newUser);

        System.out.println(body);


Output: Output:

{
  "firstName": "MKT_AUTO",
  "lastName": "QA User",
  "locale": "en",
  "rightHand": true,
  "startDay": "2020-02-03",
  "endDay": "2020-02-04",
  "operatorId": "123456",
  "password": "Temp-Auto01",
  "global": false,
  "userStores": {
    "storeCode": "null",
    "groupId": 24,
    "changedById": "000000081",
    "primary": false
  },
  "active": false,
  "lockedOutFlag": false
}

Desired Output所需 Output

{
  "firstName": "MKT_AUTO",
  "lastName": "QA User",
  "locale": "en",
  "rightHand": true,
  "startDay": "2020-02-03",
  "endDay": "2020-02-04",
  "operatorId": "81",
  "password": "Temp-Auto01",
  "global": false,
  "userStores": [{ //<---- Array
    "storeCode": "null",
    "groupId": 24,
    "changedById": "000000081",
    "primary": false
  }],
  "active": false,
  "lockedOutFlag": false
}

Omitting the usage of builder for list type is the way to go.省略对列表类型的builder的使用是要走的路。 And then just using the ArrayList<>() method worked.然后只使用ArrayList<>()方法就可以了。

Example :例子 :

        List<Group> userStores = new ArrayList<>();
        userStores.add(new Group("216", 1, "81", true));

        String lastName = "QA User";
        int numOfDays = 1;
        String defaultLocale = "en";
        User newUser = User.builder()
                .firstName(RoleType.getAcronym())
                .lastName(lastName)
                .startDay(getCurrentYearMonthDate())
                .endDay(addDaysToYearMonthDate(numOfDays))
                .password(PropertyBean.tempPass())
                .rightHand(true)
                .operatorId("123456")
                .userStores(userStores)
                .locale(defaultLocale).build();

Output :输出 :

{
  "firstName": "MKT_AUTO",
  "lastName": "QA User",
  "locale": "en",
  "rightHand": true,
  "startDay": "2020-02-03",
  "endDay": "2020-02-04",
  "operatorId": "123456",
  "password": "Temp-Auto01",
  "global": false,
  "userStores": [
    {
      "storeCode": "216",
      "groupId": 1,
      "changedById": "81",
      "primary": true
    }
  ],
  "active": false,
  "lockedOutFlag": false
}

I know this is an old question and probably doesn't make a difference any more, however Collections work fine with Lombok's builder syntax.我知道这是一个老问题,可能不再有什么不同,但是 Collections 与 Lombok 的构建器语法配合得很好。

import lombok.Builder;

@Builder
public class TestBuilder {

  private final List<String> testStrings;

}

With that you are able to call TestBuilder.builder().testStrings(List.of("test", "test2")).build();这样你就可以调用TestBuilder.builder().testStrings(List.of("test", "test2")).build(); without any issue.没有任何问题。

You can also add the @Singular annotation to also implement .add functions for adding singular object to the array.您还可以添加@Singular注释来实现.add函数,以将奇异 object 添加到数组中。

A good example can be found on Baeldung .Baeldung上可以找到一个很好的例子。

Since the OP didn't show his lombok'd sources, all I can suppose is that the User class wasn't expecting a collection for userStores, since a single group was sent in.由于 OP 没有显示他的 lombok 来源,我只能假设用户 class 并不期望 userStores 的集合,因为发送了一个组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM