com.di.kit.Json Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kit Show documentation
Show all versions of kit Show documentation
json,xml,httpclient etc.
package com.di.kit;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* @author di
*/
public class Json {
SimpleDateFormat sdf;
boolean camelCaseToUnderscores = false;
static Json json;
public Json setDateFormat(String pattern) {
this.sdf = new SimpleDateFormat(pattern);
return this;
}
private SimpleDateFormat getDateFormat() {
if (sdf == null) {
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
return sdf;
}
public Json setCamelCaseToUnderscores(boolean camelCaseToUnderscores) {
this.camelCaseToUnderscores = camelCaseToUnderscores;
return this;
}
public static Json getJson() {
if (json == null) {
json = new Json();
}
return json;
}
private List split(String json) {
int off = 0;
int ang1 = 0;// {
int ang2 = 0;// }
int squa1 = 0;// [
int squa2 = 0;// ]
int quot = 0;// "
List is = new ArrayList<>();
List ss = new ArrayList<>();
while (off < json.length()) {
char c = json.charAt(off);
switch (c) {
case '"':
if (off == 0 || json.charAt(off - 1) != '\\') {
quot++;
}
break;
case '{':
if (quot % 2 == 0) {
ang1++;
}
break;
case '}':
if (quot % 2 == 0) {
ang2++;
}
break;
case '[':
if (quot % 2 == 0) {
squa1++;
}
break;
case ']':
if (quot % 2 == 0) {
squa2++;
}
break;
case ',':
if (quot % 2 == 0 && ang1 == ang2 && squa1 == squa2) {
is.add(off);
}
break;
default:
break;
}
off++;
}
if (is.size() > 1) {
ss.add(json.substring(0, is.get(0)));
for (int i = 0; i < is.size() - 1; i++) {
ss.add(json.substring(is.get(i) + 1, is.get(i + 1)));
}
ss.add(json.substring(is.get(is.size() - 1) + 1));
} else if (is.size() == 1) {
ss.add(json.substring(0, is.get(0)));
ss.add(json.substring(is.get(0) + 1));
} else {
ss.add(json);
}
return ss;
}
public static T fromJson(String json, Class c) {
return getJson().toObject(json, c);
}
public static String toJsonString(Object o) {
return getJson().toJson(o);
}
public String toJson(T o) {
if (o == null) {
return null;
} else if (o.getClass() == byte.class || o.getClass() == short.class || o.getClass() == int.class
|| o.getClass() == long.class || o.getClass() == double.class || o.getClass() == float.class
|| o.getClass() == java.lang.Byte.class || o.getClass() == java.lang.Short.class
|| o.getClass() == java.lang.Integer.class || o.getClass() == java.lang.Long.class
|| o.getClass() == java.lang.Double.class || o.getClass() == java.lang.Float.class
|| o.getClass() == boolean.class || o.getClass() == java.lang.Boolean.class
|| o.getClass() == java.lang.String.class || o.getClass() == java.lang.Character.class) {
return String.valueOf(o);
} else if (o.getClass() == Date.class || o.getClass() == java.sql.Date.class
|| o.getClass() == java.sql.Time.class) {
return getDateFormat().format(o);
} else if (o.getClass().isArray()) {
Str str = new Str().add("[");
Object[] os = (Object[]) o;
for (Object o0 : os) {
if (o0.getClass() == String.class || o0.getClass() == Date.class) {
str.add("\"").add(toJson(o0)).add("\",");
} else {
str.add(toJson(o0)).add(",");
}
}
return str.delLastChar().add("]").toString();
} else if (o.getClass() == java.util.List.class || o.getClass() == java.util.ArrayList.class) {
Str str = new Str().add("[");
List> os = (List>) o;
for (Object o0 : os) {
if (o0.getClass() == String.class || o0.getClass() == Date.class) {
str.add("\"").add(toJson(o0)).add("\",");
} else {
str.add(toJson(o0)).add(",");
}
}
return str.delLastChar().add("]").toString();
} else if ((o instanceof Object) && o.getClass() != Object.class && o.getClass() != Class.class) {
Str str = new Str().add("{");
try {
for (Field f : ClassUtil.getDeclaredFields(o.getClass())) {
f.setAccessible(true);
if (f.get(o) == null) {
continue;
}
String n0 = f.getName();
Object v = f.get(o);
if (f.getType() == String.class || f.getType() == Date.class) {
str.add("\"").add(n0).add("\":\"").add(toJson(v)).add("\",");
} else {
str.add("\"").add(n0).add("\":").add(toJson(v)).add(",");
}
}
str.deleteLastChar();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return str.add("}").toString();
}
return null;
}
@SuppressWarnings("unchecked")
public T toObject(String json, Class c) {
if (json == null && !c.isPrimitive()) {
return null;
} else if ((json == null || json.isEmpty()) && c.isPrimitive()) {
throw new IllegalArgumentException("json is null or empty that can't be transform to primitive Class.");
}
T o = null;
json = json.trim();
o = (T) toObjectVal(toObject(json), c);
return o;
}
public List toObjects(String json, Class c) {
if (json == null && !c.isPrimitive()) {
return null;
} else if ((json == null || json.isEmpty()) && c.isPrimitive()) {
throw new IllegalArgumentException("json is null or empty that can't be transform to primitive Class.");
}
List list=new ArrayList<>();
json = json.trim();
toObjectVal(toObject(json), List.class);
return list;
}
@SuppressWarnings("unchecked")
private Object toObjectVal(Object val, Class> c) {
try {
if (val == null) {
return null;
} else if (c == boolean.class || c == Boolean.class) {
return Boolean.valueOf(String.valueOf(val));
} else if (c == char.class || c == Character.class) {
return new Character(String.valueOf(val).charAt(0));
} else if (c == byte.class || c == Byte.class) {
return Byte.valueOf(String.valueOf(val));
} else if (c == short.class || c == Short.class) {
return Short.valueOf(String.valueOf(val));
} else if (c == int.class || c == Integer.class) {
return Integer.valueOf(String.valueOf(val));
} else if (c == long.class || c == Long.class) {
return Long.valueOf(String.valueOf(val));
} else if (c == float.class || c == Float.class) {
return Float.valueOf(String.valueOf(val));
} else if (c == double.class || c == Double.class) {
return Double.valueOf(String.valueOf(val));
} else if (c == Date.class) {
return getDateFormat().parse(String.valueOf(val));
} else if (c == java.sql.Date.class) {
return new java.sql.Date(getDateFormat().parse(String.valueOf(val)).getTime());
} else if (c == Time.class) {
return new Time(getDateFormat().parse(String.valueOf(val)).getTime());
} else if (c == String.class) {
return String.valueOf(val);
} else if (c.isArray() && val != null) {
List