org.bdware.doip.cluster.util.JSONTool Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of doip-audit-tool Show documentation
Show all versions of doip-audit-tool Show documentation
doip audit tool developed by bdware
package org.bdware.doip.cluster.util;
import com.google.gson.*;
import wrp.jdk.nashorn.api.scripting.ScriptObjectMirror;
import wrp.jdk.nashorn.internal.objects.NativeArray;
import wrp.jdk.nashorn.internal.runtime.PropertyMap;
import wrp.jdk.nashorn.internal.scripts.JO;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class JSONTool {
public static JsonElement convertMirrorToJson(Object ret2) {
return convertMirrorToJsonInternal(ret2, new HashSet<>());
}
public static Object convertJsonElementToMirror(JsonElement jsonElement) {
if (jsonElement.isJsonPrimitive()) {
JsonPrimitive primitive = jsonElement.getAsJsonPrimitive();
if (primitive.isString())
return primitive.getAsString();
else if (primitive.isBoolean())
return primitive.getAsBoolean();
return primitive.getAsNumber();
} else if (jsonElement.isJsonObject()) {
JO jo = new JO(PropertyMap.newMap());
JsonObject jsonObject = jsonElement.getAsJsonObject();
for (String key : jsonObject.keySet()) {
jo.put(key, convertJsonElementToMirror(jsonObject.get(key)), false);
}
return jo;
} else if (jsonElement.isJsonArray()) {
NativeArray narray = new NativeArray();
JsonArray jsonArray = jsonElement.getAsJsonArray();
for (int i = 0; i < jsonArray.size(); i++)
NativeArray.push(narray, convertJsonElementToMirror(jsonArray.get(i)));
return narray;
}
return null;
}
public static Object convertJsonElementToMirror(Object input) {
if (input instanceof JsonElement) {
return convertJsonElementToMirror((JsonElement) input);
} else {
return input;
}
}
private static JsonElement convertMirrorToJsonInternal(Object obj, Set