fastjson fastson如何禁止序列化字符串,因为对字符串序列化会产生转义字符

aiqt4smr  于 2021-11-27  发布在  Java
关注(0)|答案(4)|浏览(542)
  • No description provided.*
ltqd579y

ltqd579y1#

如果想禁止序列化某一类型的value,可以使用PropertyFilter将其过滤掉。

public static void filterByPropertyFilter()
    {
        PropertyFilter filter = new PropertyFilter()
        {
            @Override
            public boolean apply( Object o, String key, Object value )
            {
                if (! (value instanceof String) )
                {
                    return true;    // 过滤出不是String类型的value
                }
                return false;
            }
        };

        SerializeWriter out = new SerializeWriter(  );
        JSONSerializer serializer = new JSONSerializer( out );
        serializer.getPropertyFilters().add( filter );

        Car car = new Car( "Audi", 5, 38.8 );
        serializer.write( car );
        System.out.println(out.toString());  // {"doors":5,"price":38.8}
    }
oyxsuwqo

oyxsuwqo2#

试试在属性上加这个注解@JSONField(jsonDirect = true)

9udxz4iz

9udxz4iz3#

现在的问题是整个通过JSON.toJSONString()转换为字符串后FastJsonHttpMessageConverter会继续把序列化后的json字符串序列化,两次序列化导致出现转义字符

相关问题