2017-10-10 3 views
2

J'essaye d'analyser Json dans un objet Java.
Certains champs nécessitent un comportement personnalisé, j'ai donc essayé d'utiliser @JsonCreator sur un constructeur. Eh bien, cela fonctionne, mais pour l'autre champ annoter avec @JsonProperty ne sont pas remplis.
N'a pas encore vérifié, mais je suppose que mon objet annoter avec @JsonUnwrapped ne sont pas remplis non plus.Jackson Deserialiser: utiliser @JsonCreator AND @JsonProperty dans le champ?

Dans ma recherche, j'ai vu un commentaire qui indique que c'est possible, mais je n'arrive pas à comprendre comment, si c'est effectivement possible.

Il y a environ 400 champs dans le json, et seulement 5 ou 6 qui nécessitent un comportement personnalisé. Donc, si je peux éviter de réécrire tout constructeur ... ce serait bien!

de ce que je Exemple essayé:

public class MyObjectA { 

    @JsonProperty("keyField1") 
    private String myField1; 

    @JsonUnwrapped 
    private MyObjectB; 

    private String[] myField2; 

    @JsonCreator 
    public MyObjectA(final Map<String, Object> properties){ 

    myField2 = ... //some Business logic 

    } 

} 

Junit:

ObjectMapper mapper = new ObjectMapper(); 
MyObjectA result = mapper.readValue(getJsonInputStream(JSON_FILE_PATH),MyObjectA.class); 
Assert.notNull(result.getMyField1(),"should be populated") 
Assert.notNull(result.getMyField2(),"should be populated") 
Assert.notNull(result.getMyObjectB(),"should be populated") 

Note: sans le constructeur, le champ autre sont bien peuplée

+0

s'il vous plaît partager votre code avec problème ... avec l'échantillon json et votre pojo – Optional

+0

post édité pour fournir un échantillon. –

Répondre

1

Ici, il est. Voir la différence entre l'utilisation @JsonConstructor commentée et non commentée. Je gère la propriété quelque chose comme la gestion personnalisée et en laissant le nom à appeler en utilisant setName. Hope qui aide

import com.fasterxml.jackson.annotation.JsonCreator; 
import com.fasterxml.jackson.annotation.JsonProperty; 
import com.fasterxml.jackson.databind.ObjectMapper; 
import java.util.Map; 

public class Jackson2 { 


    public static void main(String[] args) throws Exception { 

     final ObjectMapper mapper = new ObjectMapper(); 
     final String jsonInString = "{\"name\":\"hello world\",\"something\":\"from string\"}"; 
     System.out.println(jsonInString); 

     Foo newFoo = mapper.readValue(jsonInString, Foo.class); 
     System.out.println(newFoo.getName()); 
     System.out.println(newFoo.getSomething()); 
    } 
} 

class Foo { 

    @JsonProperty 
    private String name; 
    private String something; 

    public String getSomething() { 
     return something; 
    } 

    public void setSomething(String something) { 
     this.something = something; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 

    } 

    Foo() { 
    } 

    // @JsonCreator 
    public Foo(final Map<String, Object> properties) { 
      System.out.println("printing.."+properties); 
      something = "Something from constructor"; 
    } 
    @JsonCreator 
    public Foo(@JsonProperty("something") String something) { 
      System.out.println("printing.."+name); 
      this.something = "Something from constructor appended"+something; 
    } 



} 

Alors idée est que vous utilisez @JsonProperty dans l'argument constructeur de propriétés que vous souhaitez personnaliser. :)

+0

Oh, je vois! :) Fonctionne bien, merci. –