Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The boolean getters starting with 'is' are ignored. #77

Open
simon1514 opened this issue Sep 10, 2021 · 1 comment
Open

The boolean getters starting with 'is' are ignored. #77

simon1514 opened this issue Sep 10, 2021 · 1 comment

Comments

@simon1514
Copy link

simon1514 commented Sep 10, 2021

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.

@code-uri
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants