0

I have a json object which looks something like this

"price": {
        "sale": {
          "value": 39.99,
          "label": "Now"
        },
        "regular": {
          "value": 69.5,
          "label": "Orig."
        },
        "shippingfee": 0,
        "pricetype": "Markdown",
        "pricetypeid": 7,
        "onsale": true
}

"sale" and "regular" are keywords (unique) are 2 of the many price-types available, and the labels "Orig" and "Now" are keywords (unique) are 2 of the many labels available.

I am not sure the best data structure to store this price json into the POJO.

can someone please guide me through this ?

user641887
  • 1,506
  • 3
  • 32
  • 50

1 Answers1

1

I guess your problem is to convert the sale and regular attributes into an uniform representation which probably could use an Enumeration for the unique values. With default mechanism of JSON serilization/deserialization, this could be difficult. I am not sure about the JSON parsing library you are using, in Jackson there is a possibility to register custom serializers and deserializers for fields (using annotations). In this case, whcy tou would do is to register a custom serializer/deserializer and handle the fields of the JSON in the way you want it. You could refer this post

Added the below in response to the comment:

A probable dtructure for the POJO could be as below:

publi class Price{

  protected List<PricePointDetails> pricePointDetails;
  protected float shippingFee;
  protected PriceTypes priceType;
  protected priceTypeId;
  protected boolean onSale;

}//class closing

public class PricePointDetails{

 protected PriceTypes priceType;
 protected float value;
 protected LabelTypes labelType;

}//class closing


public enumeration PriceTypes{

 sale,regular,markdown;

}//enumeration closing

public enumeration LabelTypes{

 Orig,Now;

}//enumeration closing

What I have added, is just one way of structuring the data, it could be done in otherways also.

Community
  • 1
  • 1
Ironluca
  • 3,402
  • 4
  • 25
  • 32
  • hi, thanks for your response. I am using jackson core and jackson mapper along with spring data. Yes, I was planing to use an enum for sale" and "regular" and "Orig" and "Now". I am still stuck into what data structure would I use to create the price object and I was not even aware that with enum's JSON serialization and deserialization would be a problem. Thanks for calling that out. For now I would appreciate if you could help me with the data structure for the price object – user641887 Sep 23 '16 at 07:29
  • Thanks ! By other ways u mean without the Enum or something else ? – user641887 Sep 23 '16 at 08:34
  • Yes, that would be a possibility – Ironluca Sep 23 '16 at 08:46