2017-01-12 34 views
-2

是否有使用傑克遜的方式,在序列化過程中,要打印 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」:[{ 「國家」: 「英國」, 「RV」:{ 「VAT」:20, 「限制」:真正的}}]}

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

+0

什麼樣的輸出?你永遠不會在你發佈的代碼中序列化或打印任何東西。 –

+0

所以你是序列化一個類,而不是類的一個實例?這沒有意義。發佈一個完整的例子來重現問題。 –

+0

這不是問題。這是一個問題: 有沒有一種方法在序列化過程中使用Jackson不打印1.類名稱(構造函數名稱)2.字段名稱(意味着僅打印字段值) – yoav123456

回答

0

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

標註您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(); 
    } 
} 
+0

謝謝!這行得通 。但實際上,我需要另一個級別的ProductCountryRestrictedAndVat,它包含CountryRestrictedAndVat的列表(請參閱上面我更改問題代碼),並且再次 - 無需打印類名稱。我試圖擴展您的方法,但沒有成功。 – yoav123456

+0

ok我發現: \t \t gen.writeStartArray(); \t \t爲(最終CountryRestrictedAndVat countryRestrictedAndVat:value.getCountryRestrictedAndVat()) \t \t { \t \t \t gen.writeStartObject(); \t \t \t gen.writeObjectField(countryRestrictedAndVat.getCountryCode(),countryRestrictedAndVat.getRestrictedAndVat()); \t \t \t gen.writeEndObject(); \t \t} \t \t gen.writeEndArray(); – yoav123456