欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java组装json

程序员文章站 2022-05-08 19:15:40
...

Java组装json,零侵入,自定义实现。

​

/**
 * @author zhangshiqiang on 2019/10/24.
 */
public interface IFieldAssembleJson {
    /**
     * 组装各种数据类型的json体
     *
     * @param sb
     * @param fieldName
     * @param value
     * @return
     */
    StringBuilder assemble(StringBuilder sb, String fieldName, Object value);
}
/**
 * @author zhangshiqiang on 2019/10/24.
 */
public class NumberField implements IFieldAssembleJson {


    @Override
    public StringBuilder assemble(StringBuilder sb, String fieldName, Object value) {
        return sb.append('"')
                .append(fieldName)
                .append('"')
                .append(':')
                .append(value)
                ;
    }
}
/**
 * @author zhangshiqiang on 2019/10/24.
 */
public class StringField implements IFieldAssembleJson {
    @Override
    public StringBuilder assemble(StringBuilder sb, String fieldName, Object value) {
        return sb.append('"')
                .append(fieldName)
                .append('"')
                .append(':')
                .append('"')
                .append(value)
                .append('"')
                ;
    }
}
/**
 * @author zhangshiqiang on 2019/10/24.
 */
public class FieldAssembleJsonUtil {


    private static Map<Class<?>, IFieldAssembleJson> map = new HashMap<>();


    static {
        // 公用一个
        NumberField numberField = new NumberField();
        map.put(int.class, numberField);
        map.put(Integer.class, numberField);
        map.put(short.class, numberField);
        map.put(Short.class, numberField);
        map.put(byte.class, numberField);
        map.put(Byte.class, numberField);
        map.put(long.class, numberField);
        map.put(Long.class, numberField);
        map.put(String.class, new StringField());
    }


    public static void process(StringBuilder sb,
                               String fieldName,
                               Object value) {
        Class<?> clazz = value.getClass();
        if (map.get(clazz) == null) {
            throw new RuntimeException("暂未支持此数据类型");
        }
        map.get(clazz).assemble(sb, fieldName, value);
    }
}
/**
 * @author zhangshiqiang on 2019/10/24.
 */
public class ClassUtil {


    /**
     * 暂时只支持基础数据类型与String数据类型 TODO
     *
     * @param t
     * @param <T>
     * @return
     */
    public static <T> String toJson(T t) {
        StringBuilder sb = new StringBuilder();
        sb.append('{');


        Field[] declaredFields = t.getClass().getDeclaredFields();


        int i = 0;


        for (Field declaredField : declaredFields) {
            declaredField.setAccessible(true);
            Object obj = null;
            try {
                obj = declaredField.get(t);
            } catch (IllegalAccessException e) {
                // ignore
            }


            if (obj == null || "null".equals(obj.toString())) {
                continue;
            }


            if (i++ > 0) {
                sb.append(',');
            }
            String fieldName = declaredField.getName();
            // 拼接 json
            FieldAssembleJsonUtil.process(sb, fieldName, obj);
        }


        sb.append('}');


        return sb.toString();
    }
}