json,这玩意比较火爆,地球人都知道。网上json的解析包也非常多,然而大部分时候,我们只是需要在服务端输出json格式到客户端,甚至我经常做的是在调试日志里面输出变量的值,所以就自己写了个非常精简的json格式输出包。
package com.kievnsoft.json; import java.io.IOException; import java.lang.reflect.Array; import java.lang.reflect.Method; import java.util.Calendar; import java.util.Collection; import java.util.Date; import java.util.Map; public class JsonObject { private static final char[] numbers = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; private Calendar calendar = Calendar.getInstance(); private StringBuilder sb = new StringBuilder(); private boolean dateToText; private char[] ints = new char[16]; public JsonObject(Object value) throws Exception { this(value, false); } public JsonObject(Object value, boolean dateToText) throws Exception { super(); this.dateToText = dateToText; makeJson(value); } @Override public String toString() { return sb.toString(); } public static String toJson(Object value) throws Exception { return toJson(value, false); } public static String toJson(Object value, boolean dateToText) throws Exception { JsonObject jo = new JsonObject(value); return jo.toString(); } private void makeJson(Object value) throws Exception { if(value == null) { sb.append("null"); return; } if(value instanceof String) { jsonString(value.toString()); } else if(value instanceof Number) { sb.append(value.toString()); } else if(value instanceof Date) { if(!dateToText) sb.append(((Date)value).getTime()); else { sb.append('"'); calendar.setTime((Date)value); formatInt(calendar.get(Calendar.YEAR), 4); sb.append(ints, 0, 4).append('-'); formatInt(calendar.get(Calendar.MONTH), 2); sb.append(ints, 0, 2).append('-'); formatInt(calendar.get(Calendar.DAY_OF_MONTH), 2); sb.append(ints, 0, 2).append(' '); formatInt(calendar.get(Calendar.HOUR_OF_DAY), 2); sb.append(ints, 0, 2).append(':'); formatInt(calendar.get(Calendar.MINUTE), 2); sb.append(ints, 0, 2).append(':'); formatInt(calendar.get(Calendar.SECOND), 2); sb.append(ints, 0, 2); sb.append('"'); } } else if(value instanceof Collection) { sb.append('['); for(Object o : (Collection)value) { makeJson(o); sb.append(','); } sb.setCharAt(sb.length() - 1, ']'); } else if(value instanceof Map) { sb.append('{'); for(Map.Entry v : ((Map)value).entrySet()) { makeJson(v.getKey()); sb.append(':'); makeJson(v.getValue()); sb.append(','); } sb.setCharAt(sb.length() - 1, '}'); } else if(value.getClass().isArray()) { sb.append('['); int n = Array.getLength(value); for(int i = 0; i < n; ++i) { makeJson(Array.get(value, i)); sb.append(','); } sb.setCharAt(sb.length() - 1, ']'); } else if(value instanceof Boolean) { sb.append(value); } else if(value instanceof Character) { jsonString(String.valueOf(value)); } else if(value instanceof Enum) { jsonString(((Enum)value).toString()); } else { sb.append('{'); Class cls = value.getClass(); Method[] ms = cls.getMethods(); for(Method v : ms) { String msn = v.getName(); if(!msn.startsWith("get") || msn.charAt(3) == '_' || msn.equals("getClass") || v.getParameterTypes().length > 0) continue; sb.append(Character.toLowerCase(msn.charAt(3))); sb.append(msn, 4, msn.length()).append(':'); makeJson(v.invoke(value)); sb.append(','); } sb.setCharAt(sb.length() - 1, '}'); } } private void jsonString(String str) throws IOException { char c; sb.append('"'); for(int i = 0; i < str.length(); ++i) { c = str.charAt(i); switch (c) { case '"': sb.append("///""); break; case '/'': sb.append("//'"); break; case '//': sb.append("////"); break; case '/': sb.append("///"); break; case '/b': sb.append("//b"); break; case '/f': sb.append("//f"); break; case '/n': sb.append("//n"); break; case '/r': sb.append("//r"); break; case '/t': sb.append("//t"); break; default: sb.append(c); break; } } sb.append('"'); } private void formatInt(int value, int length) { for(int i = length - 1; i >= 0; --i) { if(value > 0) { ints[i] = numbers[value % 10]; value = value / 10; } else ints[i] = '0'; } } }