weiboclient4j.utils.JsonUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of weiboclient4j Show documentation
Show all versions of weiboclient4j Show documentation
Java library for Sina Weibo
The newest version!
package weiboclient4j.utils;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.codehaus.jackson.type.TypeReference;
import org.scribe.model.Response;
import weiboclient4j.WeiboClientException;
import weiboclient4j.WeiboError;
import static weiboclient4j.utils.StringUtils.isNotBlank;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import java.util.logging.Logger;
/**
* @author Hover Ruan
*/
public final class JsonUtils {
private static Logger log = Logger.getLogger("weiboclient2/json");
private static ObjectMapper mapper = new ObjectMapper();
static {
mapper.setPropertyNamingStrategy(new SinaJsonNamingStrategy());
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
format.setTimeZone(TimeZone.getTimeZone("GMT"));
mapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
mapper.setDateFormat(format);
}
private JsonUtils() {
}
public static T readValue(String content, Class valueType) throws IOException {
return mapper.readValue(content, valueType);
}
public static T readValue(String content, TypeReference valueTypeRef) throws IOException {
return mapper.readValue(content, valueTypeRef);
}
public static T parseJsonObject(Response response, Class clazz) throws WeiboClientException {
if (response.isSuccessful()) {
return parseJsonObject(response.getCode(), response.getBody(), clazz);
} else {
throw createException(response);
}
}
public static List parseJsonObject(Response response, TypeReference> type)
throws WeiboClientException {
if (response.isSuccessful()) {
return parseJsonObject(response.getCode(), response.getBody(), type);
} else {
throw createException(response);
}
}
public static List parseJsonObject(String content, TypeReference> type) throws WeiboClientException {
try {
return readValue(content, type);
} catch (IOException e) {
handleWeiboError(0, content, e);
return null;
}
}
private static List parseJsonObject(int code, String body, TypeReference> type)
throws WeiboClientException {
try {
return readValue(body, type);
} catch (IOException e) {
handleWeiboError(code, body, e);
return null;
}
}
public static T parseJsonObject(String content, Class clazz) throws WeiboClientException {
try {
return readValue(content, clazz);
} catch (IOException e) {
handleWeiboError(0, content, e);
return null;
}
}
private static T parseJsonObject(int code, String content, Class clazz) throws WeiboClientException {
try {
return readValue(content, clazz);
} catch (IOException e) {
handleWeiboError(code, content, e);
return null;
}
}
private static WeiboClientException createException(Response response) {
String body = response.getBody();
int code = response.getCode();
if (isNotBlank(body)) {
try {
WeiboError error = readValue(body, WeiboError.class);
return new WeiboClientException(code, body, error);
} catch (IOException e) {
return new WeiboClientException(code, body, e);
}
} else {
return new WeiboClientException(code);
}
}
private static void handleWeiboError(int code, String content, IOException e) throws WeiboClientException {
try {
WeiboError error = readValue(content, WeiboError.class);
throw new WeiboClientException(code, content, error);
} catch (IOException ioe) {
throw new WeiboClientException(code, content, e);
}
}
public static String writeObjectAsString(Object obj) throws IOException {
return mapper.writeValueAsString(obj);
}
}