base.json.Json Maven / Gradle / Ivy
/**
* Creative commons Attribution-NonCommercial license.
*
* http://creativecommons.org/licenses/by-nc/2.5/au/deed.en_GB
*
* NO WARRANTY IS GIVEN OR IMPLIED, USE AT YOUR OWN RISK.
*/
package base.json;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import base.QueryResult;
public class Json {
private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");
private static final DateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyyMMddHHmmss");
public static String toDate(Date date) {
return DATE_FORMAT.format(date);
}
public static String toDateTime(Date date) {
return DATE_TIME_FORMAT.format(date);
}
public static Date toDateTime(String date) throws IOException {
try {
return DATE_TIME_FORMAT.parse(date);
} catch (ParseException e) {
throw new IOException("Invalid date format: "+date);
}
}
public static Date toDate(String date) throws IOException {
try {
return DATE_FORMAT.parse(date);
} catch (ParseException e) {
throw new IOException("Invalid date format: "+date);
}
}
public static String escape(String string) {
if(string == null) {
return null;
}
return string.replaceAll("\\\\", "\\\\\\\\").replaceAll("\"", "\\\\\"").replaceAll("\r", "\\\\r").replaceAll("\n", "\\\\n");
}
@SuppressWarnings("unchecked")
public static String toJson(Map attributes) {
StringBuffer string = new StringBuffer();
string.append("{");
for(String key : attributes.keySet()) {
if(string.length() > 1) {
string.append(", ");
}
if(attributes.get(key) == null) {
string.append("\""+key+"\":null");
continue;
}
if(attributes.get(key) instanceof Long) {
string.append("\""+key+"\":"+String.valueOf((Long)attributes.get(key))+"");
continue;
}
if(attributes.get(key) instanceof BigDecimal) {
string.append("\""+key+"\":"+String.valueOf((BigDecimal)attributes.get(key))+"");
continue;
}
if(attributes.get(key) instanceof QueryResult) {
string.append("\""+key+"\":"+((QueryResult)attributes.get(key)).toJson()+"");
continue;
}
if(attributes.get(key) instanceof Integer) {
string.append("\""+key+"\":"+String.valueOf((Integer)attributes.get(key))+"");
continue;
}
if(attributes.get(key) instanceof String) {
string.append("\""+key+"\":\""+escape((String)attributes.get(key))+"\"");
continue;
}
if(attributes.get(key) instanceof Date) {
string.append("\""+key+"\":\""+DATE_TIME_FORMAT.format((Date)attributes.get(key))+"\"");
continue;
}
if(attributes.get(key) instanceof Timestamp) {
string.append("\""+key+"\":\""+DATE_TIME_FORMAT.format((Timestamp)attributes.get(key))+"\"");
continue;
}
if(attributes.get(key) instanceof Map, ?>) {
string.append("\""+key+"\":"+toJson((Map)attributes.get(key))+"");
continue;
}
if(attributes.get(key) instanceof List>) {
string.append("\""+key+"\":"+toJson((List © 2015 - 2025 Weber Informatics LLC | Privacy Policy