package com.daditech.common.util;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Date;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
/**
*
* 项目名称:drp <br/>
* 类名称:JsonUtil <br/>
* 类描述: <br/>
* 创建人:ki_tom <br/>
* 创建时间:2013-8-12 下午2:11:24 <br/>
* 修改人:ao.ouyang <br/>
* 修改时间:2014年10月1日11:25:24 <br/>
* @version 1.2
*
*/
public class JsonUtil {
public static final String SEP1 = ":";
public static final String SEP11 = "=";
public static final String SEP2 = ",";
public static boolean isRemoveZeroList;
public static String TimestampPattern = "yyyy-MM-dd HH:mm:ss";
public static String DatePattern = "yyyy-MM-dd HH:mm:ss";
public static boolean isCamelSpelling = true;
/**
*
* @Function string转化为map<String,String> <br/>
* @param mapStr <br/>
* @require <br/>
* @return Map<String,String> <br/>
* @Exception <br/>
* @since version 1.0 <br/>
* 创建人:ki_tom <br/>
* 创建时间:2013-8-12 下午2:11:39 <br/>
* 修改人:ao.ouyang <br/>
* 修改时间:2014年10月1日11:27:00 <br/>
*/
public static Map<String,String> strToMap(String mapStr) {
if (StringUtils.isEmpty(mapStr)) {
return null;
}
mapStr = mapStr.substring(1,mapStr.length()-1).replaceAll("'", "").replace(""", "");
Map<String,String> map = new HashMap<String,String>();
// 转换为数组
String[] text = mapStr.split("\" + SEP2);
for (String str : text) {
// 转换key与value的数组
String[] keyText = str.split(str.indexOf(SEP1)!=-1?SEP1:SEP11);
if (keyText.length <= 1) {
continue;
}
String key = keyText[0]; // key
String value = keyText[1]; // value
map.put(key.trim(), value.trim());
}
return map;
}
public static String toJson(Object obj)
{
if(null!=obj){
if(obj instanceof List){
return toJson((List)obj);
}else if(obj instanceof Object[]){
return toJson((Object[])obj);
}else{
toJson(getAttributes(obj));
}
}
return null;
}
public static String toJson(Map map)
{
String result = "";
map = removeListAttr(map);
for(Iterator it = map.keySet().iterator(); it.hasNext();)
{
String name = (String)it.next();
Object value = map.get(name);
if(value == null)
result = (new StringBuilder()).append(result).append(""").append(name).append("":null,").toString();
else
if(value instanceof Boolean)
{
Boolean b = (Boolean)value;
result = (new StringBuilder()).append(result).append(""").append(name).append("":").append(b.toString()).append(",").toString();
} else
if((value instanceof Integer) || (value instanceof Long) || (value instanceof Float) || (value instanceof Double) || (value instanceof Short) || (value instanceof BigInteger) || (value instanceof BigDecimal))
result = (new StringBuilder()).append(result).append(""").append(name).append("":").append(value.toString()).append(",").toString();
else
if(value instanceof String)
{
String v = (String)value;
v = v.replaceAll("\\", "\\\\");
v = v.replaceAll("
", "\\n");
v = v.replaceAll("
", "\\r");
v = v.replaceAll(""", "\\"");
v = v.replaceAll("'", "\\'");
result = (new StringBuilder()).append(result).append(""").append(name).append("":").append(""").append(v).append("",").toString();
} else
if(value instanceof Date || value instanceof java.util.Date)
{
SimpleDateFormat df = new SimpleDateFormat(DatePattern);
String s = df.format(value);
result = (new StringBuilder()).append(result).append(""").append(name).append("":").append(""").append(s).append("",").toString();
} else
if(value instanceof Timestamp)
{
SimpleDateFormat df = new SimpleDateFormat(TimestampPattern);
Timestamp v = (Timestamp)value;
String s = df.format(new java.util.Date(v.getTime()));
result = (new StringBuilder()).append(result).append(""").append(name).append("":").append(""").append(s).append("",").toString();
} else
if(value instanceof List)
{
String v = toJson((List)value);
result = (new StringBuilder()).append(result).append(""").append(name).append("":").append(v).append(",").toString();
} else
if(value instanceof Map)
{
Map attr = removeListAttr((Map)value);
result = (new StringBuilder()).append(result).append(""").append(name).append("":").append(toJson(attr)).append(",").toString();
} else
if(!value.getClass().getName().startsWith("java"))
{
Map attr = getAttributes(value);
attr = removeListAttr(attr);
result = (new StringBuilder()).append(result).append(""").append(name).append("":").append(toJson(attr)).append(",").toString();
} else
{
result = (new StringBuilder()).append(result).append(""").append(name).append("":").append(""").append(value.toString()).append("",").toString();
}
}
if(result.length() == 0)
return "{}";
else
return (new StringBuilder()).append("{").append(result.substring(0, result.length() - 1)).append("}").toString();
}
public static String toJson(Object aa[])
{
if(aa.length == 0)
return "[]";
String result = "";
Object arr$[] = aa;
int len$ = arr$.length;
for(int i$ = 0; i$ < len$; i$++)
{
Object obj = arr$[i$];
if(obj==null){
result = (new StringBuilder()).append(result).append("null").append(",").toString();
continue;
}
else if(obj instanceof Map)
{
Map map = (Map)obj;
map = removeListAttr(map);
result = (new StringBuilder()).append(result).append(toJson(map)).append(",").toString();
continue;
}
else if(obj instanceof List)
{
List arr = (List)obj;
result = (new StringBuilder()).append(result).append(toJson(arr)).append(",").toString();
continue;
}
else if (obj instanceof Object[]) {
Object[] arr = (Object[])obj;
result = (new StringBuilder()).append(result).append(toJson(arr)).append(",").toString();
continue;
}
else if(obj instanceof String)
{
result = (new StringBuilder()).append(result).append(""").append(String.valueOf(obj)).append("",").toString();
continue;
}
else {
Map attr = getAttributes(obj);
attr = removeListAttr(attr);
result = (new StringBuilder()).append(result).append(toJson(attr)).append(",").toString();
}
}
return (new StringBuilder()).append("[").append(result.substring(0, result.length() - 1)).append("]").toString();
}
public static String toJson(List ll)
{
return ll==null? "[]":toJson(ll.toArray());
}
public static Map getAttributes(Object obj)
{
Class c = obj.getClass();
try
{
Method method = c.getMethod("isProxy", new Class[0]);
Boolean result = (Boolean)method.invoke(obj, new Object[0]);
if(result.booleanValue())
c = c.getSuperclass();
}
catch(Exception e) { }
Map attr = new HashMap();
Field arr$[] = c.getFields();
int len$ = arr$.length;
for(int i$ = 0; i$ < len$; i$++)
{
Field f = arr$[i$];
try
{
Object value = f.get(obj);
attr.put(f.getName(), value);
}
catch(Exception e) { }
}
Method [] arr$2 = c.getDeclaredMethods();
len$ = arr$2.length;
for(int i$ = 0; i$ < len$; i$++)
{
Method m = arr$2[i$];
String mname = m.getName();
if(mname.equals("getClass"))
continue;
String pname;
if(mname.startsWith("get"))
{
pname = mname.substring(3);
if(pname.length() == 1)
pname = isCamelSpelling?
pname.toLowerCase()
:pname;
else
pname = isCamelSpelling?
(new StringBuilder())
.append(pname.substring(0, 1).toLowerCase())
.append(pname.substring(1))
.toString()
:pname;
try
{
Object value = m.invoke(obj, new Object[0]);
attr.put(pname, value);
}
catch(Exception e) { }
continue;
}
if(!mname.startsWith("is"))
continue;
pname = mname.substring(2);
if(pname.length() == 1)
pname = pname.toLowerCase();
else
pname = (new StringBuilder()).append(pname.substring(0, 1).toLowerCase()).append(pname.substring(1)).toString();
try
{
Object value = m.invoke(obj, new Object[0]);
attr.put(pname, value);
}
catch(Exception e) { }
}
return attr;
}
private static Map removeListAttr(Map map)
{
Map newMap = new HashMap();
Iterator i$ = map.entrySet().iterator();
do
{
if(!i$.hasNext())
break;
Object enObj = i$.next();
java.util.Map.Entry en = (java.util.Map.Entry)enObj;
if(isRemoveZeroList&&en.getValue() instanceof List &&((List)en.getValue()).size()==0)
continue;
else
newMap.put((String)en.getKey(), en.getValue());
} while(true);
return newMap;
}
}