java - How to serialize/deserialize "splashed" complex type? -


i'm connecting external xml api , i'm trying parse pojos using jackson xmlmapper class. part of xml looks this:

<invoice>     <some>element</some>     <some_other>element</some_other>     <currency>usd</currency>     <cost>10.42</cost>     <breakdown>         <item id="1">             <description>blah blah</description>             <cost>4.21</cost>         </item>     </breakdown> </invoice> 

and want parse currency , cost elements in single money object.

worse yet, inner items specify cost , "reuse" currency code. can parse them in intelligent way using jackson?

i want parse currency , cost elements in single money object.

given xml provided, can parse currency , cost elements in single money object creating value object invoice , utilizing @jsonunwrapped.

create value object invoice

if not want create object invoice, instead configure xmlmapper ignore unknown properties, , deserialize entire response money object. creating separate class invoice cleaner approach in opinion.

the purpose of creating invoice object encapsulate of elements of response. right may need currency , cost, later may want access breakdown example. object structured this:

public class invoice {     private final string some;     private final string some_other;     @jsonunwrapped     private final money money;     private final list<item> breakdown;      @jsoncreator     public invoice(@jsonproperty("some") string some,                    @jsonproperty("some_other") string some_other,                    @jsonproperty("money") money money,                    @jacksonxmlproperty(localname = "item") list<item> breakdown) {         this.some = some;         this.some_other = some_other;         this.money = money;         this.breakdown = breakdown;     }      public string getsome() {         return some;     }      public string getsome_other() {         return some_other;     }      public money getmoney() {         return money;     }      public list<item> getbreakdown() {         return breakdown;     } } 

notice money property annotated @jsonunwrapped. can on field, inside constructor, or on setter , "unwrap" money object , deserialize members on same level invoice's. structure class deserialize currency , cost single object:

public class money {     private final string currency;     private final double cost;      @jsoncreator     public money(@jsonproperty("currency") string currency,                  @jsonproperty("cost") double cost) {         this.currency = currency;         this.cost = cost;     }      public string getcurrency() {         return currency;     }      public double getcost() {         return cost;     } } 

the inner items specify cost , "reuse" currency code.

separating money , "item" models if possible

reusing money object 2 different models less ideal having abject represent each view. example, money object currency , cost, , item object id, description, , cost. if possible in project, create object "item":

public class item {     @jacksonxmlproperty(isattribute = true)     public final string id;     public final string description;     public final double cost;      @jsoncreator     public item(@jsonproperty("id") string id,                 @jsonproperty("description") string description,                 @jsonproperty("cost") double cost) {         this.id = id;         this.description = description;         this.cost = cost;     }      public string getid() {         return id;     }      public string getdescription() {         return description;     }      public double getcost() {         return cost;     } } 

reusing money "item" values

if not have liberty create new object , need reuse money, can configure xmlmapper ignore unknown properties , put of properties on money object.

configure xmlmapper ignore unknown properties

i recommend extending xmlmapper so:

public class customxmlmapper extends xmlmapper {     public customxmlmapper() {         configure(deserializationfeature.fail_on_unknown_properties, false);     } } 

add possible properties money

this populate properties when ever elements present. example, when "item" deserialized: id, description, , cost populated , currency null.

public class money {     @jacksonxmlproperty(isattribute = true)     public final string id;     public final string description;     private final string currency;     private final double cost;      @jsoncreator     public money(@jsonproperty("id") string id,                  @jsonproperty("description") string description,                  @jsonproperty("currency") string currency,                  @jsonproperty("cost") double cost) {         this.id = id;         this.description = description;         this.currency = currency;         this.cost = cost;     }      public string getid() {         return id;     }      public string getdescription() {         return description;     }      public string getcurrency() {         return currency;     }      public double getcost() {         return cost;     } } 

Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -