1

enter image description hereI have a specific json come from prestashop webservoice and i don't manage do deserialize correctly with playframework 2.8

    {
    "orders": [

        {
            "id": 44,
            "id_address_delivery": "95",
            "id_address_invoice": "95",
            "id_cart": "121",
            "id_currency": "2",
            "id_lang": "2",
            "id_customer": "61",
            "id_carrier": "19",
            "current_state": "5",
            "module": "stripe_official",
            "invoice_number": "0",
            "invoice_date": "0000-00-00 00:00:00",
            "delivery_number": "41",
            "delivery_date": "2020-01-19 23:16:05",
            "valid": "1",
            "date_add": "2020-01-19 18:38:10",
            "date_upd": "2020-01-19 23:16:06",
            "shipping_number": "",
            "id_shop_group": "1",
            "id_shop": "1",
            "secure_key": "23e5bfbd764ab60bd23aa1c7d70e8e4e",
            "payment": "Paiement par Stripe",
            "recyclable": "0",
            "gift": "0",
            "gift_message": "",
            "mobile_theme": "0",
            "total_discounts": "0.000000",
            "total_discounts_tax_incl": "0.000000",
            "total_discounts_tax_excl": "0.000000",
            "total_paid": "28.000000",
            "total_paid_tax_incl": "28.000000",
            "total_paid_tax_excl": "28.000000",
            "total_paid_real": "28.000000",
            "total_products": "28.000000",
            "total_products_wt": "28.000000",
            "total_shipping": "0.000000",
            "total_shipping_tax_incl": "0.000000",
            "total_shipping_tax_excl": "0.000000",
            "carrier_tax_rate": "0.000",
            "total_wrapping": "0.000000",
            "total_wrapping_tax_incl": "0.000000",
            "total_wrapping_tax_excl": "0.000000",
            "round_mode": "2",
            "round_type": "2",
            "conversion_rate": "1.000000",
            "reference": "JRRCSMHKI",
            "associations": {
                "order_rows": [
                    {
                        "id": "133",
                        "product_id": "45",
                        "product_attribute_id": "0",
                        "product_quantity": "1",
                        "product_name": "Kebab - Sandwich ou Menu: Sandwich seul;  - Sauce: Biggy burger;  - Crudités: Salade;  Maïs",
                        "product_reference": "",
                        "product_ean13": "",
                        "product_isbn": "",
                        "product_upc": "",
                        "product_price": "5.500000",
                        "id_customization": "0",
                        "unit_price_tax_incl": "5.500000",
                        "unit_price_tax_excl": "5.500000"
                    }



                ]
            }
        }
    ]
}

The problem is with associations object i really don"t understant because at the end i only want a order list with in each order a product list... I managed to get order list this way but then i could not manage I started with this but product could not be deserialized

List<Order> orders= Json.mapper().readValue(json.get("orders").traverse(), new TypeReference<List<Order>>(){});

and my class is like that

    package models;

    import java.util.Date;
    import java.util.List;

    import com.fasterxml.jackson.annotation.JsonFormat;
    import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
    import com.fasterxml.jackson.annotation.JsonProperty;
    import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

    import utils.ProductDeserializer;

    @JsonIgnoreProperties(ignoreUnknown = true)
    public class Order {

    private Long id;

    private State state;

    private String payment;

    @JsonProperty(value="delivery_date")
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy HH:mm:ss")
    private Date deliveryDate;

    @JsonProperty(value="total_paid_tax_incl")
    private float totalPaid;


    @JsonDeserialize(using=ProductDeserializer.class)
    private List<Product> products;

    public float getTotalPaid() {
        return totalPaid;
    }

    public void setTotalPaid(float totalPaid) {
        this.totalPaid = totalPaid;
    }



    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public State getState() {
        return state;
    }

    public void setState(State state) {
        this.state = state;
    }

    public String getPayment() {
        return payment;
    }

    public void setPayment(String payment) {
        this.payment = payment;
    }

    public Date getDeliveryDate() {
        return deliveryDate;
    }

    public void setDeliveryDate(Date deliveryDate) {
        this.deliveryDate = deliveryDate;
    }


    public List<Product> getProducts() {
        return products;
    }

    public void setProducts(List<Product> products) {
        this.products = products;
    }



    }

Deserializer is not called, i try with Associations object with into OrderRaws then product but it is useless class, i want something simple and efficient. Any idea ?

    package utils;

    import java.io.IOException;
    import java.util.List;

    import com.fasterxml.jackson.core.JsonParser;
    import com.fasterxml.jackson.core.type.TypeReference;
    import com.fasterxml.jackson.databind.DeserializationContext;
    import com.fasterxml.jackson.databind.JsonNode;
    import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

    import models.Product;
    import play.libs.Json;

    public class ProductDeserializer extends StdDeserializer<List<Product>>{


     /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public ProductDeserializer() { 
            this(null); 
        } 

        public ProductDeserializer(Class<?> vc) { 
            super(vc); 
        }
    @Override
    public List<Product> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        JsonNode node = p.getCodec().readTree(p);       
        return Json.mapper().readValue(node.get("associations").get("order_rows").traverse(), new TypeReference<List<Product>>(){});
    }

}

enter image description here

enter image description here

cyril
  • 872
  • 6
  • 29
  • Please update your question with code of `ProductDeserializer` – Smile Jan 23 '20 at 05:23
  • made, but even with this code ,serializer never called.... because of response construction..... i just want Order with list of product – cyril Jan 23 '20 at 11:10

1 Answers1

1

In the json, there is no attribute by the name products. You should add @JsonProperty("associations") to products attribute in Order, like:

@JsonDeserialize(using = ProductDeserializer.class)
@JsonProperty("associations")
private List<Product> products;

And then update ProductDeserializer to traverse order_rows instead of associations.

return Json.mapper().readValue(node.get("order_rows").traverse(), new TypeReference<List<Product>>(){});

Edit: Adding my entire code. I am using only Jackson and not Jackson with play framework.

Main class:

import java.util.List;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class SoMain {

    private static final String jsonStr = "Your json";

    public static void main(String[] args) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            JsonNode parentNode = objectMapper.readTree(jsonStr);
            List<Order> orders = objectMapper.convertValue(parentNode.get("orders"), new TypeReference<List<Order>>() {});
            System.out.println(orders);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

    }
}

Product:

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Product {

    private int id;

    @JsonProperty("product_name")
    private String productName;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

}

ProductDeserializer :

import java.io.IOException;
import java.util.List;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

public class ProductDeserializer extends StdDeserializer<List<Product>> {

    /**
    * 
    */
    private static final long serialVersionUID = 1L;

    public ProductDeserializer() {
        this(null);
    }

    public ProductDeserializer(Class<?> vc) {
        super(vc);
    }

    @Override
    public List<Product> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        JsonNode node = p.getCodec().readTree(p);
        return new ObjectMapper().convertValue(node.get("order_rows"), new TypeReference<List<Product>>() {});
    }

}

Order:

import java.util.Date;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Order {

    private Long id;

    private String payment;

    @JsonProperty(value = "delivery_date")
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy HH:mm:ss")
    private Date deliveryDate;

    @JsonProperty(value = "total_paid_tax_incl")
    private float totalPaid;

    @JsonDeserialize(using = ProductDeserializer.class)
    @JsonProperty("associations")
    private List<Product> products;

    public float getTotalPaid() {
        return totalPaid;
    }

    public void setTotalPaid(float totalPaid) {
        this.totalPaid = totalPaid;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getPayment() {
        return payment;
    }

    public void setPayment(String payment) {
        this.payment = payment;
    }

    public Date getDeliveryDate() {
        return deliveryDate;
    }

    public void setDeliveryDate(Date deliveryDate) {
        this.deliveryDate = deliveryDate;
    }

    public List<Product> getProducts() {
        return products;
    }

    public void setProducts(List<Product> products) {
        this.products = products;
    }

}
Smile
  • 3,832
  • 3
  • 25
  • 39
  • tried it but in Deserializer node is null and in front i have this error [JsonMappingException: (was java.lang.NullPointerException) (through reference chain: java.util.ArrayList[0]->models.Order["associations"])] – cyril Jan 24 '20 at 09:59
  • Are you trying with the json in the exact format you mentioned in your question? – Smile Jan 24 '20 at 10:07
  • Your error log suggests that associations attribute is empty/null in json – Smile Jan 24 '20 at 10:44
  • it crashes here : JsonNode node = p.getCodec().readTree(p); then the p content is – cyril Jan 24 '20 at 10:54
  • Possible to print your json and share? – Smile Jan 24 '20 at 10:58
  • yes i added in picture .... in fact i retrieve my data in p, but i don't know how to process it i will update my question. and i noticed i sent in question already the printed json – cyril Jan 24 '20 at 11:28
  • Your IDE debugger screenshot shows value of associations attribute of json. Which variable it is showing the value of? – Smile Jan 24 '20 at 11:47
  • JsonParser NodeCursor – cyril Jan 24 '20 at 12:12
  • I have added my complete code to my answer which works fine for the updated json you have shared. May be you can take reference from this code. I have used jackson without play framework for deserializing json. – Smile Jan 25 '20 at 10:10