简体   繁体   English

Jackson 序列化如何不显示类和字段名称

[英]Jackson serialization how to not show class and fields names

Is there a way using Jackson , in a serialization process , to not print 1.the class name (the constructor name) 2.the field name (meaning to print only the field value)有没有办法在序列化过程中使用 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;
        }

    }

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

I want it to be : Json:[{"UK":{"vat":20,"restricted":true}}]我希望它是: Json:[{"UK":{"vat":20,"restricted":true}}]

Yes, it's possible, using a custom serializer.是的,使用自定义序列化程序是可能的。

Annotate your CountryRestrictedAndVat class with注释CountryRestrictedAndVat

@JsonSerialize(using = CountryRestrictedAndVatSerializer.class)

Remove the @JsonProperty annotations.删除@JsonProperty注释。

And write the serializer:并编写序列化程序:

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();
    }
}

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

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