为什么Predicates.instanceOf返回false?

时间:2016-10-06 15:28:26

标签: java jackson guava instanceof predicates

我有一个

String s =
    {
      "code1" : {
        "price" : 100,
        "type" : null
      },
      "code2" : {
        "price" : 110,
        "type" : null
      }
    }

然后我这样做:

Object p = Mapper.readValue(s, Person.class);

因此它执行@JsonCreator中使用Person.class注释的方法:

@JsonCreator
static Person create(Map<String, Object> s) {
        s = Maps.filterValues(s, Predicates.instanceOf(Person.class));
        ...
    }

我的问题是s始终为空。我查了一下,价值分为pricetype。但是当我ps.get("code1").getClass()时,它会给我LinkedHashMap

我不明白发生了什么......你有什么线索吗?

这是我的班级Person(它是一个内部班级):

public static class Person{

        private int price;
        private String type;
        public Person(int price) {
            this.price = price;
        }
        public int getPrice() {
            return price;
        }
        public String getType() {
            return type;
        }
    }

谢谢!

1 个答案:

答案 0 :(得分:1)

问题是您将json String反序列化为Object,并且您将始终LinkedHashMap,因为java.lang.Object没有任何自定义字段。

尝试不同的方式:

  public  class Demo {
        public static void main(String[] args) throws IOException {
            String s = "{" +
                    "  \"code1\" : {" +
                    "    \"price\" : 100," +
                    "    \"type\" : null" +
                    "  }," +
                    "  \"code3\" : {" +
                    "    \"somethingElsse\" : false," +
                    "    \"otherType\" : 1" +
                    "  }," +
                    "  \"code2\" : {" +
                    "    \"price\" : 110," +
                    "    \"type\" : null" +
                    "  }" +
                    "}";


            ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            Map<String, Person> mapPerson = mapper.readValue(s, MapPerson.class);

            Map<String, Person> filteredMap = Maps.filterValues(mapPerson, new Predicate<Person>() {
                @Override
                public boolean apply(Person person) {
                    return person.isNotEmpty();
                }
            });

            System.out.println(filteredMap);


        }

        public static class MapPerson extends HashMap<String, Person> {}

        public static class Person{

            private int price;
            private String type;

            public Person() {
            }

            public boolean isNotEmpty() {
                return !(0 == price && null ==type);
            }

            @Override
            public String toString() {
                return "Person{" +
                        "price=" + price +
                        ", type='" + type + '\'' +
                        '}';
            }

            public int getPrice() {
                return price;
            }

            public void setPrice(int price) {
                this.price = price;
            }

            public String getType() {
                return type;
            }

            public void setType(String type) {
                this.type = type;
            }
        }
    }

使用configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)配置objec映射器时,它只会向地图添加一个空的Person实例,而不是抛出异常。 因此,您还应该定义一个方法,该方法将在Person的实例为空时回答,然后使用它过滤您的地图。

如果您使用java 8,则在过滤地图时可以减少代码:

Map<String, Person> filteredMap = Maps.filterValues(mapPerson, Person::isNotEmpty);
顺便说一下,即使你有一些额外的字段在你的密钥值JSON:

,它也会起作用
 {
      "code1" : {
        "price" : 100,
        "type" : null,
        "uselessExtraField": "Hi Stack"
      },
      "code2" : {
        "price" : 110,
        "type" : null,
        "anotherAccidentalField": "What?"
      }
    }

您将获得与该字段永远不存在相同的结果。