package jp.dip.arimodoki.common;
import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.net.URLDecoder;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;
@Scope("prototype")
@Component
public class JsonConvert implements JsonConvertIf {
Gson mygson = new Gson();
public Object Deserialize(String jsonData, Object parseObj) throws Exception {
if(jsonData == null || jsonData.equals("")) return null;
if(parseObj == null) return null;
InputStreamReader isr = new InputStreamReader( new ByteArrayInputStream(jsonData.getBytes()));
JsonReader jsr = new JsonReader( isr );
return (Object) mygson.fromJson( jsr, parseObj.getClass() );
}
public String Serialize(Object obj) throws Exception {
if(obj == null) return null;
return mygson.toJson(obj);
}
public String decode(String encStr) throws Exception {
if(encStr == null || encStr.equals("")) return null;
return this.decode(encStr,"UTF-8");
}
public String decode(String encStr, String charcode) throws Exception {
if(encStr == null || encStr.equals("")) return null;
if(charcode == null || charcode.equals("")) return null;
String decStr = "";
String decparam = URLDecoder.decode(encStr,charcode);
int len = decparam.length();
int last = decparam.lastIndexOf('=');
if(last == len-1) {
decStr = decparam.substring(0, len-1);
} else {
decStr = decparam;
}
return decStr;
}
}
|