json-view icon indicating copy to clipboard operation
json-view copied to clipboard

The boolean getters starting with 'is' are ignored.

Open simon1514 opened this issue 3 years ago • 1 comments

As shown below, the boolean getters starting with 'is' are ignored, but should be included in the json. This is in 1.0.1 and 1.1.0.

package com.pros.travel.commons.dropwizard.cassandra.dao;

import static com.monitorjbl.json.Match.match;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.monitorjbl.json.JsonView;
import com.monitorjbl.json.JsonViewModule;

public class JsonViewDemo {

  public static void main(String[] args) throws JsonProcessingException {
    // Prints 
    //   {"name":"Antartica","code":"AQ","active":true,"computedLabel":"asdf"}
    printWithJackson();
    // Prints 
    //   {"name":"Antartica","computedLabel":"asdf"}
    // and misses the 'active' property
    printWithJsonView();
  }

  private static void printWithJackson() throws JsonProcessingException {
    Pojo1 pojo1 = new Pojo1();
    ObjectMapper objectMapper = new ObjectMapper();
    System.out.println(objectMapper
        .writeValueAsString(pojo1));
  }

  private static void printWithJsonView() throws JsonProcessingException {
    Pojo1 pojo1 = new Pojo1();
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(new JsonViewModule());
    System.out.println(objectMapper
        .writeValueAsString(
            JsonView.with(pojo1).onClass(pojo1.getClass(), match().exclude("code"))));
  }
}


class Pojo1 {

  private String name = "Antartica";
  private String code = "AQ";
  private boolean active = true;

  public String getComputedLabel() {
    return "asdf";
  }

  public String getCode() {
    return code;
  }

  public void setCode(String code) {
    this.code = code;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public boolean isActive() {
    return active;
  }

  public void setActive(boolean active) {
    this.active = active;
  }
}

I am suspecting that line from JsonViewSerializer

        getDeclaredMethods(cls).stream()
            .filter(m -> m.getName().startsWith("get") && !m.getReturnType().equals(Void.class) && m.getParameters().length == 0)

I cant tweak JsonView with @JsonAutoDetect since that jackson annotation would change our existing jackson serialization.

simon1514 avatar Sep 10 '21 13:09 simon1514

@monitorjbl any update on this issue? facing same issue with boolean fields.

code-uri avatar Jul 14 '22 10:07 code-uri