0

I post what I have done, as I don't get the result.. Here I have a method which returns an ArrayList:

public ArrayList<Label> getLabels() 
         throws ClassNotFoundException, SQLException{

    ArrayList<Label> labels = new ArrayList<>();
    sq = "SELECT * from LABELS";

    try {       
          Class.forName(typeDB);
          c = DriverManager.getConnection(path);            
          stm = c.prepareStatement(sq);  
          ResultSet rs = stm.executeQuery();

          while(rs.next()) {
             Label label = new Label(rs.getString("type"), rs.getString("description"),rs.getString("product")+"-"+rs.getString("version"), rs.getString("cutter"));
             labels.add(label); 
           }

        } catch (SQLException e) {
            System.out.println(e.getMessage());
        } finally {
            if (stm != null)
                stm.close();
            if (c != null)
        c.close();
        }

        System.out.println("Label "+ labels.size());
        return labels;
    }

then I want covert this ArrayList to JSON format. So I execute labelsToJSON(action.getLabels()); where:

public void labelsToJSON(ArrayList<Label> list){

      ObjectMapper mapper = new ObjectMapper();
      try{
           mapper.writeValue(new File("C:\\temp\\labels.json"), list);
          }catch(JsonGenerationException e){
               e.printStackTrace();
          }catch(JsonMappingException e){
               e.printStackTrace();
          }catch (IOException e){
               e.printStackTrace();
          }

     }
}

The Label class is defined:

public class Label {
    private String barcode;     
    private String labelCode;
    private String productCode; 
    private String type;   
    //and many others..

    public Label(){

    }

    //This is the costructor I use above in the method   
    public Label(String type, String description, String productCode, String cutter) {
        this.type = type;
        this.description = description;
        this.productCode = productCode;
        this.cutter = cutter;    
    }

    //and then some other constructors (I post 2 for example)
    public Label(String type, String description, String product, String version, String cutter) {
        this.type = type;
        this.description = description;
        this.product = product;
        this.version = version;
        this.cutter = cutter;    
    }

    public Label(String barcode, String product, String version, String dateProduction, String order , int quantity, String packetNumber, String type, String description, String cutter) {
        this.barcode = barcode;
        this.product = product;
        this.version = version;
        this.dateProduction = dateProduction;
        this.order = order;
        this.packetNumber = packetNumber;
        this.quantity = quantity;
        this.type = type;
        this.description = description;
        this.cutter = cutter;
    }

   //setters, getters etc

So, I create an object from the constructor with parameters String type, String description, String productCode, String cutter. However the labels.json contains these data

[{ 
    "barcode":null,
    "labelCode":null,
    "productCode":"111123123-1123",        //<- 
    "type":"Container",                    //<-
    "description":"this is a description", //<- all these I was expected.
    "cutter":"1031",                       //<-
    "date":null,
    "time":null,
    "dateProduction":null,
    "order":null,
    "product":null,
    "version":null,
    "packetNumber":null,
    "quantity":0
  }, //and so on

I don't understand why the json file has so many attributes?? My objects supposed to have only 4 --> String type, String description, String productCode, String cutter

yaylitzis
  • 5,354
  • 17
  • 62
  • 107

2 Answers2

2

ObjectMapper will by default serialise all field values on a class, regardless of whether they are null or not so you get everything from your Label class.

To only serialise non null values that you can configure the ObjectMapper see the JavaDoc for setSerializationInclusion and Include

mapper.setSerializationInclusion(Include.NON_NULL);

EDIT: As Maraboc pointed out you have the problem with quantity still being serialised when using the Include.NON_NULL. For finer grain control of which fields are serialised you can use @JsonIgnore annotation to prevent the other fields in your class from being serialised.

Or you can add @JsonIgnoreProperties({"quantity"}) to your class

Maraboc
  • 10,550
  • 3
  • 37
  • 48
George Lee
  • 826
  • 6
  • 11
  • The problem is in `"quantity"` !! – Maraboc Nov 12 '15 at 10:40
  • But ther is an other problem witch is if one of your wanted fields has null as value it will not be serialized so you must check if the JSON contain the key to avoid `NullPointerException` – Maraboc Nov 12 '15 at 11:00
  • If you use, JsonIgnoreProperties, parser will ignore those properties during serialization and de-serialization irrespective of the property is null or populated with some value. Here if quantity property value is 10 then this will not come in json output. – kswaughs Nov 12 '15 at 11:33
0

You can define your Label class with JsonSerialize annotation and change the type of quantity from primitive int to Integer Object. If type is int, the default value zero will be assigned to the variable.

@JsonSerialize(
include=JsonSerialize.Inclusion.NON_NULL)
public class Label {

    // ...other properties

    private Integer quantity;

    public Integer getQuantity() {
       return quantity;
    }

    public void setQuantity(Integer quantity) {
       this.quantity = quantity;
    }
}   
kswaughs
  • 2,967
  • 1
  • 17
  • 21