fastjson 指定系列化格式

 

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 枚举类
fastjson 指定系列化格式
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;
        }
    }
}
View Code

 

 

原文: https://www.cnblogs.com/rude3knife/p/13570423.html

 

上一篇:fastjson 解析 字符串 为对象


下一篇:fastjson到底做错了什么?为什么会被频繁爆出漏洞?