杰克逊@JsonRawValue的地图价值 [英] Jackson @JsonRawValue for Map's value

查看:1553
本文介绍了杰克逊@JsonRawValue的地图价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下Java bean类,使用Jackson将其转换为JSON。

I have the following Java bean class with gets converted to JSON using Jackson.

  public class Thing {
    public String name;

    @JsonRawValue
    public Map content = new HashMap();
  }

content 是一个map的值是来自其他来源的原始JSON。例如:

content is a map who's values will be raw JSON from another source. For example:

String jsonFromElsewhere = "{ \"foo\": \"bar\" }";

Thing t = new Thing();
t.name = "test";
t.content.put("1", jsonFromElsewhere);

所需的生成JSON是:

The desired generated JSON is:

{"name":"test","content":{"1":{ "foo": "bar" }}}

然而,使用 @JsonRawValue 会导致:

{"name":"test","content":{1={ "foo": "bar" }}}

我需要的是一种指定 @JsonRawValue 的方法,仅用于Map的值。杰克逊有可能吗?

What I need is a way to specify @JsonRawValue for only for the Map's value. Is this possible with Jackson?

推荐答案

没有。你可以轻松地创建自定义 JsonSerializer 来做到这一点。

No. You could easily create custom JsonSerializer to do that though.

另外,也许只使用一次性POJO:

Also, maybe rather just use one-off POJO:

public class RawHolder {
   @JsonProperty("1")
   public String raw;
}

public class Thing {
   public String name;
   public RawHolder content;
}

这篇关于杰克逊@JsonRawValue的地图价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆