Search code examples
javajsonto-json

Converted json (from ArrayList) has more elements


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


Solution

  • 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