日期:2014-05-16  浏览次数:20490 次

json对象和java对象呼唤工具
public class TestDataUtil {

@SuppressWarnings("rawtypes")
public static Object loadTestData(String postfix, Class testClass,
Class beanClass) throws Exception {

String patch = System.getProperty("user.dir") + "/resource/"
+ testClass.getName().replace(".", "/");
patch += "/"
+ testClass.getName().substring(
testClass.getName().lastIndexOf(".") + 1);
patch += "_" + postfix + ".json";
System.out.println("patch:" + patch);
File file = new File(patch);
BufferedReader reader = new BufferedReader(new FileReader(file));
StringBuffer stringBuffter = new StringBuffer();
String tempString = null;
while ((tempString = reader.readLine()) != null) {
stringBuffter.append(tempString);
}
reader.close();

JSONObject jsonObject = JSONObject.fromObject(stringBuffter.toString());
JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher());
JSONUtils.getMorpherRegistry().registerMorpher(new TimeMorpher());
JSONUtils.getMorpherRegistry().registerMorpher(new TimestampMorpher());
Object results = JSONObject.toBean(jsonObject, beanClass);

return results;
}

@SuppressWarnings("rawtypes")
public static List<Object> loadListTestData(String postfix,
Class testClass, Class beanClass) throws Exception {

String patch = System.getProperty("user.dir") + "/resource/"
+ testClass.getName().replace(".", "/");
patch += "/"
+ testClass.getName().substring(
testClass.getName().lastIndexOf(".") + 1);
patch += "_" + postfix + ".json";
File file = new File(patch);
BufferedReader reader = new BufferedReader(new FileReader(file));
StringBuffer stringBuffter = new StringBuffer();
String tempString = null;
while ((tempString = reader.readLine()) != null) {
stringBuffter.append(tempString);
}
reader.close();

JSONArray array = JSONArray.fromObject(stringBuffter.toString());
JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher());
JSONUtils.getMorpherRegistry().registerMorpher(new TimeMorpher());
JSONUtils.getMorpherRegistry().registerMorpher(new TimestampMorpher());
List<Object> list = new ArrayList<Object>();
for (Iterator iter = array.iterator(); iter.hasNext();) {
JSONObject jsonObject = (JSONObject) iter.next();
list.add(JSONObject.toBean(jsonObject, beanClass));
}
return list;
}

public static String toJsonString(Object o) throws Exception {
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
jsonConfig.registerJsonValueProcessor(java.sql.Date.class,
new DateJsonValueProcessor());
jsonConfig.registerJsonValueProcessor(java.sql.Time.class,
new DateJsonValueProcessor());
jsonConfig.registerJsonValueProcessor(java.sql.Timestamp.class,
new DateJsonValueProcessor());
if (o instanceof Collection || o instanceof Object[]) {
JSONArray jsonArray = JSONArray.fromObject(o, jsonConfig);
return jsonArray.toString();
} else {
JSONObject object = JSONObject.fromObject(o, jsonConfig);
return object.toString();
}
}

public static void saveTestData(String postfix, Class<?> testClass,
Object obj) throws Exception {

String patch = System.getProperty("user.dir") + "/resource/"
+ testClass.getName().replace(".", "/") + "_" + postfix
+ ".json";
File file = new File(patch);
if (file.exists()) {
System.out.print("file exists.");
} else {
System.out.print("no file.");
file.createNewFile();
}
BufferedWriter bw = new