JSON 构造器类,可以将Java对象映射为JSON对象
写道
/**
* JSON 构造器类,可以将Java对象映射为JSON对象
* @author yyl
* @datetime
* @version
*/
public class JsonBuilder {
//排除的字段
private Set<String> excludeFields = new HashSet<String>();
private static final Object EMPTY_OBJECT_ARRAY[] = new Object[;
public JsonBuilder() {
excludeFields.add("class");
excludeFields.add("declaringClass");
excludeFields.add("metaClass");
}
/**
* 转换成JSON时需要排除的字段
* @param excludes
*/
public void setExcludeFields(String... excludes) {
for (String exclude : excludes) {
excludeFields.add(exclude);
}
}
/**
* 将Java对象转化为JSON对象
* @param obj java对象
* @return js对象
*/
public String encode(Object object) {
return encodeBasic(object);
}
/**
* 将Java对象转化为JSON对象
* @param obj java对象
* @return js对象
*/
private String encodeBasic(Object object) {
// basic
if (object == null) {
return encodeNULL();
} else if (object instanceof String) {
return encodeString((String) object);
} else if (object instanceof Boolean) {
return encodeBoolean((Boolean) object);
} else if (object instanceof Number) {
return encodeNumber((Number) object);
} else if (object instanceof Map) {
return encodeMap((Map) object);
} else if (object instanceof Iterable) {
return encodeIterable((Iterable) object);
} else if (object instanceof Object[]) {// object.getClass().isArray()
return encodeArray((Object[]) object);
} else if (object instanceof java.util.Date) {
return encodeDate((java.util.Date) object);
} else {
Class clazz = object.getClass();
if (clazz.isInterface()) {
return encodeEmpty();
}
if (clazz.isEnum()) {
return encodeEnum((java.lang.Enum) object);
} else {
return encodeAdapter(object);
}
}
}
/**
* 返回一个NULL对象
* @return javascript null对象
*/
private String encodeNULL() {
return "null";
}
/**
* 将Java-String对象转化为JSON对象
* @param string 字符串对象
* @return javascript string对象
*/
private String encodeString(String string) {
StringBuilder sbr = new StringBuilder(string.length() * ;
sbr.append("'");
for (int i = sz = string.length(); i < sz; i++) {
char ch = string.charAt(i);
// handle unicode
if (ch > fff) {
sbr.append("\\u");
sbr.append(hex(ch));
} else if (ch > ff) {
sbr.append("\\u);
sbr.append(hex(ch));
} else if (ch > ) {
sbr.append("\\u);
sbr.append(hex(ch));
} else if (ch < {
switch (ch) {
case '\b':
sbr.append('\\');
sbr.append('b');
break;
case '\n':
sbr.append('\\');
sbr.append('n');
break;
case '\t':
sbr.append('\\');
sbr.append('t');
break;
case '\f':
sbr.append('\\');
sbr.append('f');
break;
case '\r':
sbr.append('\\');
sbr.append('r');
break;
default:
if (ch > f) {
sbr.append("\\u);
sbr.append(hex(ch));
} else {
sbr.append("\\u);
sbr.append(hex(ch));
}
break;
}
} else {
// line.
switch (ch) {
case '\'':
sbr.append('\\');
sbr.append('\'');
break;
case '"':
sbr.append('\\');
sbr.append('"');
break;
case '\\':
sbr.append('\\');
sbr.append('\\');
break;
default:
sbr.append(ch);
break;
}
}
}
sbr.append("'");
return sbr.toString();
}
private String hex(char ch) {
return Integer.toHexString(ch).toUpperCase(Locale.ENGLISH);
}
/**
* 将Java-Boolean对象转化为JSON对象
* @param obj 字符串对象
* @return javascript Boolean对象
*/
private String encodeBoolean(Boolean b) {
return b.toString();
}
/**
* 将Java-Number对象转化为JSON对象
* @param n 数字对象
* @return javascript Number对象 <