diff --git a/build.gradle b/build.gradle index 645891b..fe3e73a 100644 --- a/build.gradle +++ b/build.gradle @@ -36,7 +36,7 @@ repositories { } dependencies { - api 'com.google.code.gson:gson:2.8.6' + api 'com.google.code.gson:gson:2.8.7' api 'log4j:log4j:1.2.17' - testImplementation 'junit:junit:4.12' + testImplementation 'junit:junit:4.13.2' } diff --git a/src/main/java/org/bdware/mockjava/JsonUtil.java b/src/main/java/org/bdware/mockjava/JsonUtil.java index 724d4a8..4f4edcb 100644 --- a/src/main/java/org/bdware/mockjava/JsonUtil.java +++ b/src/main/java/org/bdware/mockjava/JsonUtil.java @@ -8,13 +8,14 @@ import java.util.Set; public class JsonUtil { private static Set recorded = null; + public static JsonElement toJson(jdk.nashorn.api.scripting.ScriptObjectMirror res) { recorded = new HashSet<>(); JsonElement jsonElement = copyInternal(res); recorded.clear(); - ; return jsonElement; } + private static JsonElement copyInternal(Object obj) { if (recorded.contains(obj)) return JsonNull.INSTANCE; if (obj == null) return JsonNull.INSTANCE; diff --git a/src/main/java/org/bdware/mockjava/JsonVisitor.java b/src/main/java/org/bdware/mockjava/JsonVisitor.java index e98ad29..652b8b6 100644 --- a/src/main/java/org/bdware/mockjava/JsonVisitor.java +++ b/src/main/java/org/bdware/mockjava/JsonVisitor.java @@ -6,8 +6,16 @@ import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; public abstract class JsonVisitor { + JsonElement result; + + public JsonElement get() { + return result; + } + public JsonVisitor visit(JsonElement ele) { - if (ele == null) return null; + if (null == ele) { + return null; + } if (ele.isJsonArray()) { return visitJsonArray((JsonArray) ele); } else if (ele.isJsonObject()) { @@ -18,7 +26,20 @@ public abstract class JsonVisitor { } } - public abstract JsonVisitor visitObject(JsonObject asJsonObject); + public JsonVisitor visitObject(JsonObject jsonObject) { + JsonObject jo = new JsonObject(); + for (String key : jsonObject.keySet()) { + JsonElement origin = jsonObject.get(key); + visit(origin); + JsonElement je = get(); + if (origin.isJsonArray()) { + key += "|" + Math.min(((JsonArray) origin).size(), 20); + } + jo.add(key, je); + } + result = jo; + return this; + } public abstract JsonVisitor visitJsonArray(JsonArray ele); diff --git a/src/main/java/org/bdware/mockjava/MockSchemaParser.java b/src/main/java/org/bdware/mockjava/MockSchemaParser.java index 29e148d..82ae1c7 100644 --- a/src/main/java/org/bdware/mockjava/MockSchemaParser.java +++ b/src/main/java/org/bdware/mockjava/MockSchemaParser.java @@ -1,37 +1,14 @@ package org.bdware.mockjava; import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; public class MockSchemaParser extends JsonVisitor { public static MockSchemaParser instance = new MockSchemaParser(); - JsonElement result; - - public JsonElement get() { - return result; - } - - @Override - public JsonVisitor visitObject(JsonObject jsonObject) { - JsonObject jo = new JsonObject(); - for (String key : jsonObject.keySet()) { - JsonElement origin = jsonObject.get(key); - visit(origin); - JsonElement je = get(); - if (origin.isJsonArray()) { - key += "|" + Math.min(((JsonArray) origin).size(), 20); - } - jo.add(key, je); - } - result = jo; - return this; - } @Override public JsonVisitor visitJsonArray(JsonArray array) { - JsonArray ret = new JsonArray(); + JsonArray ret = new JsonArray(); if (array.size() > 0) { visit(array.get(0)); ret.add(result); @@ -46,8 +23,9 @@ public class MockSchemaParser extends JsonVisitor { result = new JsonPrimitive("@boolean"); } else if (primitive.isNumber()) { result = new JsonPrimitive("@integer"); + } else { + result = new JsonPrimitive("@string"); } - result = new JsonPrimitive("@string"); return this; } } \ No newline at end of file diff --git a/src/main/java/org/bdware/mockjava/MockUtil.java b/src/main/java/org/bdware/mockjava/MockUtil.java index 210166c..68c52a6 100644 --- a/src/main/java/org/bdware/mockjava/MockUtil.java +++ b/src/main/java/org/bdware/mockjava/MockUtil.java @@ -19,8 +19,8 @@ public class MockUtil { MOCK_JS_ENGINE = new ScriptEngineManager().getEngineByName("JavaScript"); // System.out.println(MOCK_JS_ENGINE); try (InputStream mockJs = - MockUtil.class.getClassLoader().getResourceAsStream(MOCK_JS_PATH); - InputStreamReader reader = new InputStreamReader(mockJs); ) { + MockUtil.class.getClassLoader().getResourceAsStream(MOCK_JS_PATH); + InputStreamReader reader = new InputStreamReader(mockJs)) { MOCK_JS_ENGINE.eval(reader); } catch (ScriptException | IOException e) { // log.error("执行MockJs错误", e); @@ -31,7 +31,7 @@ public class MockUtil { private static boolean isJson(String str) { try { - JsonObject jsonStr = JsonParser.parseString(str).getAsJsonObject(); + JsonParser.parseString(str).getAsJsonObject(); return true; } catch (Exception e) { return false; @@ -39,7 +39,7 @@ public class MockUtil { } public static Object mock(String template) { - Object result = (ScriptObjectMirror) ScriptObjectMirror.wrap(null, Context.getGlobal()); + Object result = ScriptObjectMirror.wrap(null, Context.getGlobal()); if (template.length() > 0) { try { //