0

I am completely new in Json und I am trying to deserialize a complex Json with complex types (all these Elements are Classes from my programm: entete, pied-depage, corps (List<classe like garantie,..>), nomain, labels, lieu, garantie , Elt). I try to deserialize this JSON to a List from type class Tester.

[{
"entete":{
    "titre" :,  "Nummer" : , 
    … }
"pied-depage":{
    "utilisateur" : "martin"
    … }
"Corps":[{
 "reservation" : 0.00,
    
    "garantie" : [ ],
    "Utilisateurs" : [ ],

           …}
…}]
"nomain": $
"daterRange": {}
"labels":{}
"date1" : "1988-01-01",
"date2" : "1528-12-31"
}]
class Tester {
    private entete header =  entete.getHeader();
    private pied-depage footer = pied-depage.getFooter(); 
    private List<Elt> Corps;
    private Elt anlagengut;
    private String nomain;
    private LocalDateRange dateRange;
    
    public LocalDate getDate1() {
      return this.dateRange.getStart();
    }
    
    public LocalDate getDate2() {
      return this.dateRange.getEndInclusive();
    }
    
    // getter ...
    
    public static List<Tester> getCollection() throws IOException {  
      ObjectMapper mapper = new ObjectMapper();
      
      mapper.registerModules( new JavaTimeModule());
      mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false)
        .findAndRegisterModules();
      
      List<Tester>  tester = (List<Tester>) mapper.readValue(Paths.get("Tester.json").toFile(), List.class);
      return tester;
    }
}

I am trying after serialization of the same Object with Jackson to deserialize the produced JSON to have the identic container in another program. do you have any indication for me? I have already written these but I get an object with elements of Type LinkedHashMap.

João Dias
  • 16,277
  • 6
  • 33
  • 45
boris
  • 1
  • 1
  • As I understand, this isn't a real code: `private pied-depage footer = pied-depage.getFooter();`, right ? Could you please provide the working code. Anyway, to match JSON, that field declaration should be changed to something like `private Object piedDepage` – I.G. Nov 08 '21 at 07:57

1 Answers1

1
[{
“ entete“:{
    "titre" :,  "Nummer" : , 
    … }
“ pied-depage“:{
    "utilisateur" : "martin"
    … }
“ Corps“:[{
 "reservation" : 0.00,
    
    "garantie" : [ ],
    "Utilisateurs" : [ ],

           …}
…}]
“ nomain“: $
“ daterRange“: {}
“ labels“:{}
"date1" : "1988-01-01",
"date2" : "1528-12-31"
}]
``` 

Kindly see reference below:

When you see { } , You can either create a Class to map the properties, or you can use Map<String, String>

When you see [{}], You can use a List<YourCustomClass> or List<Map<String, String>>

Use @JsonProperty("YourWeirdPropertyNameHere") if you want to map your Class Properties towards JSON Properties which have different cases/names

When mapping properties,

entete“:{
    "titre" :,  "Nummer" : , 
    … }

Then a class representation will look like:

public class Entete {

 private String titre;     //String because the value is "Number" which is in ""

}

For

“ Corps“:[{
     "reservation" : 0.00,
        
        "garantie" : [ ],
        "Utilisateurs" : [ ],
    
               …}
    …}]

Then a class representation will look like:

public class Corp{
   
  private Double reservation;
  private List<String> garantie;

  @JsonProperty("Utilisateurs")
  private List<String> utilisateurs;

}

Then you can create the mapper class Test:

public class Test{

  public Entete entete;

  @JsonProperty("pied-depage")
  public PiedDepage piedDepage;

  @JsonProperty("Corps")
  public List<Corp> corps;
 
   
}

And to use ObjectMapper, you need to pass TypeReference:

List<Test> myTests = mapper.readValue(jsonInput, new TypeReference<List<Test>>(){});
JCompetence
  • 6,997
  • 3
  • 19
  • 26
  • thanks @susan i already tried with annotations but i can't manage to deserialize my Object with Variables of the same type as in the bean Tester and the LocalDate are Still String not LocalDate Type – boris Nov 08 '21 at 09:05
  • My suggestion to you @boris is to start small. Remove everything from your JSON, but leave entete and try that. If that works, add the next element to your JSON. You have a problem somewhere..... – JCompetence Nov 08 '21 at 09:06
  • Ok susan, I'll test it right away, I'm sorry, do you have an idea for LocalDate, even though I use JavaTimeModule I don't get any dates with the LocalDate type when I deserialize? – boris Nov 08 '21 at 09:30
  • @boris My recommendation is to take it 1 element at a time first :) Regarding the LocalDate, see https://stackoverflow.com/questions/28802544/java-8-localdate-jackson-format – JCompetence Nov 08 '21 at 09:40