杰克逊包装对象列表

发布于 2025-02-04 11:01:29 字数 1257 浏览 2 评论 0原文

我正在尝试创建一个Java库来管理Card.V1 JSON用于替代运行时Google附加组件功能。我已经开始工作了,但是正在遇到一些问题,让杰克逊以与Google API所需要的方式包装课程。

例如,他们的部分具有一个小部件列表,其中包含文本段落,图像或按钮列表。这是一个示例JSON:

"sections":[ {
 "widgets":[
    {
       "textParagraph":{
          "text":"Your random cat 2:"
       }
    },
    {
       "image":{
          "imageUrl":"https://cataas.com/cat"
       }
    },
    {
       "buttonList":{
          "buttons":[
             {
                "text":"Happy",
             },
             {
                "text":"Sad",
             }
          ]
       }
    }
 ]
} ]

我创建了一个text类,它看起来像这样:

@JsonTypeName("textParagraph")
@JsonTypeInfo(include= JsonTypeInfo.As.WRAPPER_OBJECT, use= JsonTypeInfo.Id.NAME)
public class TextParagraph extends BaseWidget
{
    String text;
}

如果我使用对象映射器单独写出此内容,我会得到预期的JSON:

"textParagraph" : {
  "text" : "Testing Text"
}

但是如果我有一个列出然后打印,它失去了“ textParagraph”包装。

{
  "widgets" : [ {
    "text" : "Testing Text"
  }, {
    "text" : "Testing Text 2"
  }, {
    "imageUrl" : "ggole.com/image.png",
    "altText" : "Image Text"
  } ]
}

我是否缺少包裹列表对象的特殊注释?我真的很想这样做,而不必使用任何自定义映射器或包装器类。

有人在这里有经验吗?

I am trying to create a Java library to manage the Card.v1 JSON for the Alternative Runtime Google Add-ons functions. I've gotten everything to work, but am running into some problems getting Jackson to wrap classes in the same way that the Google APIs require it.

As an example, they have a Section that has a list of Widgets, which are wrapped Text Paragraphs, Images, or Button Lists. Here is an example JSON:

"sections":[ {
 "widgets":[
    {
       "textParagraph":{
          "text":"Your random cat 2:"
       }
    },
    {
       "image":{
          "imageUrl":"https://cataas.com/cat"
       }
    },
    {
       "buttonList":{
          "buttons":[
             {
                "text":"Happy",
             },
             {
                "text":"Sad",
             }
          ]
       }
    }
 ]
} ]

I have created a Text class, which looks like this:

@JsonTypeName("textParagraph")
@JsonTypeInfo(include= JsonTypeInfo.As.WRAPPER_OBJECT, use= JsonTypeInfo.Id.NAME)
public class TextParagraph extends BaseWidget
{
    String text;
}

If I use the object mapper to write this out on its own, I get the expected JSON:

"textParagraph" : {
  "text" : "Testing Text"
}

But if I have a List and then print that, it loses the "textParagraph" wrapping.

{
  "widgets" : [ {
    "text" : "Testing Text"
  }, {
    "text" : "Testing Text 2"
  }, {
    "imageUrl" : "ggole.com/image.png",
    "altText" : "Image Text"
  } ]
}

Is there a special annotation I'm missing to wrap the list objects? I'd really like to do this without having to use any custom mappers or Wrapper classes.

Does anyone have experience here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走野 2025-02-11 11:01:29

好吧,我解决了。

在基类(BaseWidget)上,我需要添加@jsontypeinfo(use = jsontypeinfo.id.name,inclage inclage = jsontypeinfo.as.as.wrapper_object) notation。将其列出,列表与包装器序列化。

Well, I solved it.

On the base class (BaseWidget), I needed to add the @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.WRAPPER_OBJECT) annotation. With that in place, the list serializes with the wrappers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文