2017-01-12 12 views
-2

シリアル化プロセスでジャクソンを使用する方法はではない print 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":{ "バット":20は、 "制限付き":本当に}}}}

私はそれが欲しい: Json:[{"UK":{"vat":20、 "制限":true}}]

+0

何の出力?あなたが投稿したコードの中で何かをシリアライズや印刷することは決してありません。 –

+0

クラスのインスタンスではなく、クラスをシリアライズしていますか?それは意味をなさない。問題を再現した完全な例を投稿してください。 –

+0

これは問題ではありません。これは疑問です: シリアル化プロセスで、クラス名(コンストラクター名)2.the field name(フィールド値のみを出力することを意味します)を印刷しない方法はありますか? – yoav123456

答えて

0

はい、カスタムシリアライザを使用することは可能です。

@JsonSerialize(using = CountryRestrictedAndVatSerializer.class) 

であなたのCountryRestrictedAndVatクラスに注釈を付ける@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

ありがとう!それは働く。しかし、実際には、CountryRestrictedAndVatのリストを保持する別のレベルのProductCountryRestrictedAndVatが必要です(上記の質問コードを変更しました)。再度、クラス名を印刷する必要はありません。あなたのアプローチを拡張しようとしましたが、成功しませんでした.. – yoav123456

+0

found: \t \t gen.writeStartArray(); (最終CountryRestrictedAndVatのcountryRestrictedAndVat:value.getCountryRestrictedAndVat())のために \t \t \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

関連する問題