-1

I have this simple java object

public class Order {
    static public class Product {
        public String name;
        public Integer quantity;
        public Float price;
    }
    public String clientName;
    public String clientPhone;
    ArrayList<Product> products = new ArrayList<Product>();
    public Float total;
}

I want to serialize it to JSON using Jackson. I do like this:

    String _json;
    ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
    _json = ow.writeValueAsString(order);

but it won't serialie the array list in my order, only the other members.

How do I serialize an object that contains an ArrayList? If it's not possible is there another container class that I can use which is easy to serialize?

Guerlando OCs
  • 1,886
  • 9
  • 61
  • 150

1 Answers1

2

Jackson depends on public accessors to determine fields to serialize.

Your list is the only field that isn't publicly exposed, so that's why it's hidden in the output

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245