public static void main(String[] args) { List<User> list = new ArrayList<>(); list.add(new User(1,"11","11")); list.add(new User(2,"22","22")); list.add(new User(3,"33","33")); String buildData = buildData(list); System.out.println(buildData); // [{"id":1,"user_age":"11","user_name":"11"},{"id":2,"user_age":"22","user_name":"22"},{"id":3,"user_age":"33","user_name":"33"}] String buildData2 = buildData2(list); System.out.println(buildData2); // [{"id":1,"userAge":"11","userName":"11"},{"id":2,"userAge":"22","userName":"22"},{"id":3,"userAge":"33","userName":"33"}] String buildData3 = buildData3(list); System.out.println(buildData3); // [{"id":1,"user-age":"11","user-name":"11"},{"id":2,"user-age":"22","user-name":"22"},{"id":3,"user-age":"33","user-name":"33"}] } public static String buildData(Object bean) { try { SerializeConfig CONFIG = new SerializeConfig(); CONFIG.propertyNamingStrategy = PropertyNamingStrategy.SnakeCase; return JSON.toJSONString(bean, CONFIG); } catch (Exception e) { return null; } } public static String buildData2(Object bean) { try { SerializeConfig CONFIG = new SerializeConfig(); CONFIG.propertyNamingStrategy = PropertyNamingStrategy.CamelCase; return JSON.toJSONString(bean, CONFIG); } catch (Exception e) { return null; } } public static String buildData3(Object bean) { try { SerializeConfig CONFIG = new SerializeConfig(); CONFIG.propertyNamingStrategy = PropertyNamingStrategy.KebabCase; return JSON.toJSONString(bean, CONFIG); } catch (Exception e) { return null; } }
最好先实例化,否则运行过程会频繁创建,详情参考别人的(https://www.cnblogs.com/rude3knife/p/13570423.html)
private static final SerializeConfig CONFIG = new SerializeConfig(); static { CONFIG.propertyNamingStrategy = PropertyNamingStrategy.SnakeCase; }
PropertyNamingStrategy 枚举类
package com.alibaba.fastjson; /** * @since 1.2.15 */ public enum PropertyNamingStrategy { CamelCase, // PascalCase, // SnakeCase, // KebabCase; public String translate(String propertyName) { switch (this) { case SnakeCase: { StringBuilder buf = new StringBuilder(); for (int i = 0; i < propertyName.length(); ++i) { char ch = propertyName.charAt(i); if (ch >= 'A' && ch <= 'Z') { char ch_ucase = (char) (ch + 32); if (i > 0) { buf.append('_'); } buf.append(ch_ucase); } else { buf.append(ch); } } return buf.toString(); } case KebabCase: { StringBuilder buf = new StringBuilder(); for (int i = 0; i < propertyName.length(); ++i) { char ch = propertyName.charAt(i); if (ch >= 'A' && ch <= 'Z') { char ch_ucase = (char) (ch + 32); if (i > 0) { buf.append('-'); } buf.append(ch_ucase); } else { buf.append(ch); } } return buf.toString(); } case PascalCase: { char ch = propertyName.charAt(0); if (ch >= 'a' && ch <= 'z') { char[] chars = propertyName.toCharArray(); chars[0] -= 32; return new String(chars); } return propertyName; } case CamelCase: { char ch = propertyName.charAt(0); if (ch >= 'A' && ch <= 'Z') { char[] chars = propertyName.toCharArray(); chars[0] += 32; return new String(chars); } return propertyName; } default: return propertyName; } } }
fastjson SerializeConfig 做了什么
SerializeConfig介绍:
SerializeConfig的主要功能是配置并记录每种Java类型对应的序列化类(ObjectSerializer接口的实现类),
比如Boolean.class使用BooleanCodec(看命名就知道该类将序列化和反序列化实现写到一起了)作为序列化实现类,
float[].class使用FloatArraySerializer作为序列化实现类。这些序列化实现类,有的是FastJSON中默认实现的(比如Java基本类),
有的是通过ASM框架生成的(比如用户自定义类),有的甚至是用户自定义的序列化类(比如Date类型框架默认实现是转为毫秒,应用需要转为秒)。
当然,这就涉及到是使用ASM生成序列化类还是使用JavaBean的序列化类类序列化的问题,这里判断根据就是是否Android环境(环境变量"java.vm.name"为"dalvik"
或"lemur"就是Android环境),但判断不仅这里一处,后续还有更具体的判断。
理论上来说,每个SerializeConfig实例若序列化相同的类,都会找到之前生成的该类的代理类,来进行序列化。们的服务在每次接口被调用时,都实例化一个ParseConfig对象来配置Fastjson反序列的设置,而未禁用ASM代理的情况下,由于每次调用ParseConfig都是一个新的实例,因此永远也检查不到已经创建的代理类,所以Fastjson便不断的创建新的代理类,并加载到metaspace中,最终导致metaspace不断扩张,将机器的内存耗尽。