【问题标题】:Jackson serialization how to not show class and fields names杰克逊序列化如何不显示类和字段名称
【发布时间】:2017-01-12 09:47:51
【问题描述】:

有没有办法在序列化过程中使用 Jackson 来打印 1.类名(构造函数名) 2.字段名(意思是只打印字段值)

    public class Test
{


    private static final Logger LOG = Logger.getLogger(Test.class);

    /**
     * @param args
     * @throws IOException
     * @throws JsonMappingException
     * @throws JsonGenerationException
     */
    public void work(final ObjectMapper mapper) throws JsonGenerationException, JsonMappingException, IOException
    {
        RestrictedAndVat restrictedAndVat = new RestrictedAndVat();
        restrictedAndVat.setRate(BigDecimal.valueOf(20));
        restrictedAndVat.setRestricted(true);
        final CountryRestrictedAndVat countryRestrictedAndVat1 = new CountryRestrictedAndVat();
        countryRestrictedAndVat1.setCountryCode("UK");
        countryRestrictedAndVat1.setRestrictedAndVat(restrictedAndVat);

        final ProductCountryRestrictedAndVat productCountryRestrictedAndVat = new ProductCountryRestrictedAndVat();
        final List<CountryRestrictedAndVat> list = new ArrayList<CountryRestrictedAndVat>();
        list.add(countryRestrictedAndVat1);

        productCountryRestrictedAndVat.setCountryRestrictedAndVat(list);
        LOG.info("Json:" + serialiseJson(productCountryRestrictedAndVat, mapper));

    }

    private <DATA> String serialiseJson(final DATA pojoData, final ObjectMapper mapper)
            throws JsonGenerationException, JsonMappingException, IOException
    {

        final StringWriter jsonWriter = new StringWriter();
        mapper.writeValue(jsonWriter, pojoData);
        return jsonWriter.toString().trim();
    }

}

public class ProductCountryRestrictedAndVat
{


    @JsonProperty(value = "crv", required = false)
    private List<CountryRestrictedAndVat> countryRestrictedAndVat;

    /**
     * @return the countryRestrictedAndVat
     */
    public List<CountryRestrictedAndVat> getCountryRestrictedAndVat()
    {
        return countryRestrictedAndVat;
    }

    /**
     * @param countryRestrictedAndVat
     *           the countryRestrictedAndVat to set
     */
    public void setCountryRestrictedAndVat(final List<CountryRestrictedAndVat> countryRestrictedAndVat)
    {
        this.countryRestrictedAndVat = countryRestrictedAndVat;
    }




}


    public class RestrictedAndVat
    {


        @JsonProperty("vat")
        @JsonInclude(JsonInclude.Include.NON_NULL)
        private BigDecimal rate;

        @JsonProperty("restricted")
        private boolean restricted;

        /**
         * @return the rate
         */
        public BigDecimal getRate()
        {
            return rate;
        }

        /**
         * @param rate
         *           the rate to set
         */
        public void setRate(final BigDecimal rate)
        {
            this.rate = rate;
        }

        /**
         * @return the restricted
         */
        public boolean isRestricted()
        {
            return restricted;
        }

        /**
         * @param restricted
         *           the restricted to set
         */
        public void setRestricted(final boolean restricted)
        {
            this.restricted = restricted;
        }

    }

    public class CountryRestrictedAndVat
    {



        @JsonProperty("country")
        private String countryCode;


        @JsonProperty(value = "rv", required = false)
        private RestrictedAndVat restrictedAndVat;


        /**
         * @return the countryCode
         */
        public String getCountryCode()
        {
            return countryCode;
        }


        /**
         * @param countryCode
         *           the countryCode to set
         */
        public void setCountryCode(final String countryCode)
        {
            this.countryCode = countryCode;
        }


        /**
         * @return the restrictedAndVat
         */
        public RestrictedAndVat getRestrictedAndVat()
        {
            return restrictedAndVat;
        }


        /**
         * @param restrictedAndVat
         *           the restrictedAndVat to set
         */
        public void setRestrictedAndVat(final RestrictedAndVat restrictedAndVat)
        {
            this.restrictedAndVat = restrictedAndVat;
        }

    }

输出是: Json:{"crv":[{"country":"UK","rv":{"vat":20,"restricted":true}}]}

我希望它是: Json:[{"UK":{"vat":20,"restricted":true}}]

【问题讨论】:

  • 输出什么?您永远不会在发布的代码中序列化或打印任何内容。
  • 所以你是在序列化一个类,而不是类的实例?那没有意义。发布一个重现问题的完整示例。
  • 这不是问题。这是一个问题:有没有办法在序列化过程中使用 Jackson 来不打印 1.类名(构造函数名) 2.字段名(意味着只打印字段值)
  • 是的,这是默认行为。做显而易见的事情会让你得到那个结果。所以你没有做明显的事情。但由于你不会发布你在做什么,我们无法解释。
  • 我编辑了我的问题。请看

标签: java json jackson


【解决方案1】:

是的,可以使用自定义序列化程序。

注释您的 CountryRestrictedAndVat
@JsonSerialize(using = CountryRestrictedAndVatSerializer.class)

删除@JsonProperty 注释。

并编写序列化器:

public class CountryRestrictedAndVatSerializer extends JsonSerializer<CountryRestrictedAndVat> {
    @Override
    public void serialize(CountryRestrictedAndVat value,
                          JsonGenerator gen,
                          SerializerProvider serializers) throws IOException, JsonProcessingException {
        gen.writeStartObject();
        gen.writeObjectField(value.getCountryCode(), value.getRestrictedAndVat());
        gen.writeEndObject();
    }
}

【讨论】:

  • 谢谢!这行得通 。但实际上我需要另一个级别的 ProductCountryRestrictedAndVat 来保存 CountryRestrictedAndVat 列表(见上文我更改了问题代码),并且再次 - 不需要打印类名。我试图扩展你的方法但没有成功..
  • 好的,我发现:gen.writeStartArray(); for (final CountryRestrictedAndVat countryRestrictedAndVat : value.getCountryRestrictedAndVat()) { gen.writeStartObject(); gen.writeObjectField(countryRestrictedAndVat.getCountryCode(), countryRestrictedAndVat.getRestrictedAndVat()); gen.writeEndObject(); } gen.writeEndArray();
猜你喜欢
  • 1970-01-01
  • 2020-11-30
  • 1970-01-01
  • 2023-04-07
  • 2021-12-02
  • 2017-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多