Skip to content

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

@simon1514

Description

@simon1514

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions