feat: udpate JsonUtil

rename JsonUtil.parseString to JsonUtil.parseStringAsJsonObject, rename JsonUtil.parseObject to JsonUtil.parseObjectAsJsonObject; add JsonUtil.parseReaderAsJsonObject
This commit is contained in:
Frank.R.Wu 2021-11-17 14:40:21 +08:00
parent 2cfd82bb4b
commit c1654034ac
8 changed files with 28 additions and 24 deletions

View File

@ -40,7 +40,7 @@ public class ProgramPoint {
// functionMap.put(strings[1], "EXIT");
// }
if (string.contains("traceMark")) {
JsonObject jo = JsonUtil.parseString(string);
JsonObject jo = JsonUtil.parseStringAsJsonObject(string);
if (jo.get("traceMark") != null) {
if (jo.get("lval") != null) {
transaction.insert(

View File

@ -18,7 +18,7 @@ public class TracedFile {
// TODO ignore handle contractID/method....
// TODO ignore handle transaction end
while (sc.hasNextLine()) {
JsonObject jo = JsonUtil.parseString(sc.nextLine());
JsonObject jo = JsonUtil.parseStringAsJsonObject(sc.nextLine());
if (null != jo.get("traceMark")) {
if (null != jo.get("lval")) {
transaction.insert(jo.get("traceMark").getAsInt(), jo.get("lval").getAsInt());

View File

@ -54,7 +54,7 @@ public class PYEntry {
pyYjsRequest.setYjsParams(pyYjsParams);
response = HttpClient.post(ip, port, urlPath, JsonUtil.toJson(pyYjsRequest));
JsonObject pyResponse = JsonUtil.parseString(response);
JsonObject pyResponse = JsonUtil.parseStringAsJsonObject(response);
return pyResponse.getAsJsonObject("yjsResult").get("objectId").getAsString();
}
@ -72,7 +72,7 @@ public class PYEntry {
httpResponse = HttpClient.post(ip, port, urlPath, JsonUtil.toJson(pyYjsRequest));
JsonObject pyResponse = JsonUtil.parseString(httpResponse);
JsonObject pyResponse = JsonUtil.parseStringAsJsonObject(httpResponse);
return pyResponse.getAsJsonObject("yjsResult").get("objectId").getAsString();
}
@ -80,7 +80,7 @@ public class PYEntry {
String urlPath = "/api/availablePackages/";
String response;
response = HttpClient.get(ip, port, urlPath);
JsonObject pyResponse = JsonUtil.parseString(response);
JsonObject pyResponse = JsonUtil.parseStringAsJsonObject(response);
JsonObject availablePackages =
pyResponse.getAsJsonObject("yjsResult").getAsJsonObject("availablePackages");
JsonArray array = availablePackages.getAsJsonArray("packages");
@ -98,7 +98,7 @@ public class PYEntry {
String urlPath = "/api/availableModules/";
String response;
response = HttpClient.get(ip, port, urlPath);
JsonObject pyResponse = JsonUtil.parseString(response);
JsonObject pyResponse = JsonUtil.parseStringAsJsonObject(response);
JsonObject availablePackages =
pyResponse.getAsJsonObject("yjsResult").getAsJsonObject("availableModules");
JsonArray array = availablePackages.getAsJsonArray("modules");
@ -125,7 +125,7 @@ public class PYEntry {
// "yjsResult": {"packageTotalInfo": {"package_name": "yjsexample",
// "module_names": ["yjsexample.sample"]}}
JsonObject pyResponse = JsonUtil.parseString(httpResponse);
JsonObject pyResponse = JsonUtil.parseStringAsJsonObject(httpResponse);
JsonObject packageTotalInfo =
pyResponse.getAsJsonObject("yjsResult").getAsJsonObject("packageTotalInfo");
PackageInfo packageInfo = JsonUtil.fromJson(packageTotalInfo, PackageInfo.class);
@ -161,7 +161,7 @@ public class PYEntry {
// "sayHello"}, {"func_sign": "(self, name)", "func_name": "sayHello"}],
// "class_name": "A"}
ModuleInfo moduleInfo;
JsonObject pyResponse = JsonUtil.parseString(httpResponse);
JsonObject pyResponse = JsonUtil.parseStringAsJsonObject(httpResponse);
JsonObject moduleInfoObj =
pyResponse.getAsJsonObject("yjsResult").getAsJsonObject("moduleInfo");
moduleInfo = JsonUtil.fromJson(moduleInfoObj, ModuleInfo.class);
@ -230,7 +230,7 @@ public class PYEntry {
PYClass pyClass = new PYClass();
ClassCollection classInfo;
JsonObject pyResponse = JsonUtil.parseString(httpResponse);
JsonObject pyResponse = JsonUtil.parseStringAsJsonObject(httpResponse);
JsonObject classInfoObj =
pyResponse.getAsJsonObject("yjsResult").getAsJsonObject("classInfo");
classInfo = JsonUtil.fromJson(classInfoObj, ClassCollection.class);
@ -289,7 +289,7 @@ public class PYEntry {
pyYjsParams.setParams(PYMethodParams);
pyYjsRequest.setYjsParams(pyYjsParams);
httpResponse = HttpClient.post(ip, port, urlPath, JsonUtil.toJson(pyYjsRequest));
JsonObject pyResponse = JsonUtil.parseString(httpResponse);
JsonObject pyResponse = JsonUtil.parseStringAsJsonObject(httpResponse);
JsonObject ysjResult = pyResponse.getAsJsonObject("yjsResult");
return ysjResult.toString();
}
@ -308,7 +308,7 @@ public class PYEntry {
pyYjsParams.setParams(PYMethodParams);
pyYjsRequest.setYjsParams(pyYjsParams);
httpResponse = HttpClient.post(ip, port, urlPath, JsonUtil.toJson(pyYjsRequest));
JsonObject pyResponse = JsonUtil.parseString(httpResponse);
JsonObject pyResponse = JsonUtil.parseStringAsJsonObject(httpResponse);
JsonObject ysjResult = pyResponse.getAsJsonObject("yjsResult");
return ysjResult.toString();
}
@ -329,7 +329,7 @@ public class PYEntry {
pyYjsParams.setParams(PYMethodParams);
pyYjsRequest.setYjsParams(pyYjsParams);
httpResponse = HttpClient.post(ip, port, urlPath, JsonUtil.toJson(pyYjsRequest));
JsonObject pyResponse = JsonUtil.parseString(httpResponse);
JsonObject pyResponse = JsonUtil.parseStringAsJsonObject(httpResponse);
JsonObject ysjResult = pyResponse.getAsJsonObject("yjsResult");
return ysjResult.toString();
}
@ -352,7 +352,7 @@ public class PYEntry {
pyYjsRequest.setYjsParams(pyYjsParams);
System.out.println(JsonUtil.toJson(pyYjsRequest));
httpResponse = HttpClient.post(ip, port, urlPath, JsonUtil.toJson(pyYjsRequest));
JsonObject pyResponse = JsonUtil.parseString(httpResponse);
JsonObject pyResponse = JsonUtil.parseStringAsJsonObject(httpResponse);
JsonObject ysjResult = pyResponse.getAsJsonObject("yjsResult");
return ysjResult.toString();
}

View File

@ -37,11 +37,15 @@ public class JsonUtil {
return PRETTY_GSON.toJson(o);
}
public static JsonObject parseString(String json) {
public static JsonObject parseStringAsJsonObject(String json) {
return JsonParser.parseString(json).getAsJsonObject();
}
public static JsonObject parseObject(Object obj) {
public static JsonObject parseReaderAsJsonObject(Reader reader) {
return JsonParser.parseReader(reader).getAsJsonObject();
}
public static JsonObject parseObjectAsJsonObject(Object obj) {
return GSON.toJsonTree(obj).getAsJsonObject();
}
}

View File

@ -96,7 +96,7 @@ public class MultiIndexTimeDBUtil implements MultiIndexTimeDBUtilIntf {
try {
t = db.readFromDatabase(l.toString());
if (t != null && !t.isEmpty()) {
JsonObject jo = JsonUtil.parseString(t);
JsonObject jo = JsonUtil.parseStringAsJsonObject(t);
jo.addProperty("key", l.toString());
ret.add(jo);
}
@ -130,7 +130,7 @@ public class MultiIndexTimeDBUtil implements MultiIndexTimeDBUtilIntf {
String t = db.readFromDatabase(l.toString());
JsonObject jo;
if (t != null && !t.isEmpty()) {
jo = JsonUtil.parseString(t);
jo = JsonUtil.parseStringAsJsonObject(t);
} else {
jo = new JsonObject();
}

View File

@ -124,7 +124,7 @@ public class MultiIndexTimeRocksDBUtil implements MultiIndexTimeDBUtilIntf {
try {
t = new String(db.get(longToByte(l)));
if (!t.isEmpty()) {
JsonObject jo = JsonUtil.parseString(t);
JsonObject jo = JsonUtil.parseStringAsJsonObject(t);
jo.addProperty("key", l.toString());
ret.add(jo);
}
@ -158,7 +158,7 @@ public class MultiIndexTimeRocksDBUtil implements MultiIndexTimeDBUtilIntf {
String t = new String(db.get(longToByte(l)));
JsonObject jo;
if (!t.isEmpty()) {
jo = JsonUtil.parseString(t);
jo = JsonUtil.parseStringAsJsonObject(t);
} else {
jo = new JsonObject();
}
@ -246,7 +246,7 @@ public class MultiIndexTimeRocksDBUtil implements MultiIndexTimeDBUtilIntf {
RocksIterator iter = db.newIterator();
iter.seekToFirst();
for (; iter.isValid(); iter.next()) {
JsonObject jo = JsonUtil.parseString(new String(iter.value()));
JsonObject jo = JsonUtil.parseStringAsJsonObject(new String(iter.value()));
Long time = jo.get("date").getAsLong();
if (time < 0) {
continue;

View File

@ -134,7 +134,7 @@ public class TimeDBUtil {
try {
String t = db.readFromDatabase(l.toString());
if (null != t && !t.isEmpty()) {
JsonObject jo = JsonUtil.parseString(t);
JsonObject jo = JsonUtil.parseStringAsJsonObject(t);
jo.addProperty("key", l.toString());
if (jo.has("contractName")
&& jo.get("contractName").getAsString().equals(contractName)) {
@ -293,7 +293,7 @@ public class TimeDBUtil {
for (Long l : data) {
try {
String t = db.readFromDatabase(l.toString());
data2 = JsonUtil.parseString(t);
data2 = JsonUtil.parseStringAsJsonObject(t);
ret.add(data2);
} catch (Exception e) {
e.printStackTrace();
@ -323,7 +323,7 @@ public class TimeDBUtil {
try {
t = db.readFromDatabase(l.toString());
data2 = JsonUtil.parseString(t);
data2 = JsonUtil.parseStringAsJsonObject(t);
ret.add(data2);
} catch (Exception e) {
LOGGER.error("parse json error:" + t);

View File

@ -145,7 +145,7 @@ public class TimeRocksDBUtil {
try {
String t = new String(db.get(l.toString().getBytes(StandardCharsets.UTF_8)));
if (null != t && !t.isEmpty()) {
JsonObject jo = JsonUtil.parseString(t);
JsonObject jo = JsonUtil.parseStringAsJsonObject(t);
jo.addProperty("key", l.toString());
if (jo.has("contractName")
&& jo.get("contractName").getAsString().equals(contractName)) {