easy-es icon indicating copy to clipboard operation
easy-es copied to clipboard

Map类型字段fastjson保存序列化问题

Open tomZF opened this issue 1 year ago • 1 comments

Caused by: ElasticsearchException[Elasticsearch exception [type=json_parse_exception, reason=Unexpected character ('n' (code 110)): was expecting double-quote to start field name .....

`@Data @IndexName("demo") public class EasyEsDemo {

@IndexId
private String id;

private String name;
private int age;

// 这个Map字段因为NameFilter过滤器,导致fastjson序列化后为{null:"value"}这种形式,insert报错
private Map<String, Object> data;

} ` EntityInfoHelper中创建的NameFilter是一个匿名类,看上去主要功能就是过滤排除字段,但是针对Map类型字段就出现了问题,导致序列化时候Map的key字段变成了个null. 是不是还有别的方式处理这种情况?

还是说EntityInfoHelper中创建的NameFilter可以留个口允许自定义实现?

目前的处理方式是自定义了一个MapSerializer,使用@JSONField(serializeUsing = MyMapSerializer.class)注解data字段 ` public static class MyMapSerializer extends MapSerializer { @Override protected String processKey(JSONSerializer jsonBeanDeser, Object object, String key, Object propertyValue) { // 删除了 jsonBeanDeser.nameFilters 的处理逻辑

        if (this.nameFilters != null) {
            for (NameFilter nameFilter : this.nameFilters) {
                key = nameFilter.process(object, key, propertyValue);
            }
        }
        return key;
    }
}

`

tomZF avatar Jun 18 '24 07:06 tomZF

@IndexField(nestedClass = yourClassName.class) 有这样一个嵌套内部类的注解也许是可以试试。

wayhelper avatar Jun 18 '24 10:06 wayhelper