| 1 | package com.github.dakusui.json; | |
| 2 | ||
| 3 | import com.github.dakusui.valid8j_cliche.core.All; | |
| 4 | import com.github.dakusui.valid8j_cliche.core.Transform; | |
| 5 | import com.google.gson.JsonArray; | |
| 6 | import com.google.gson.JsonElement; | |
| 7 | import com.google.gson.JsonObject; | |
| 8 | import com.google.gson.JsonPrimitive; | |
| 9 | ||
| 10 | import java.util.List; | |
| 11 | import java.util.Objects; | |
| 12 | import java.util.function.Predicate; | |
| 13 | ||
| 14 | import static com.github.dakusui.valid8j.Assertions.that; | |
| 15 | import static com.github.dakusui.valid8j_cliche.core.Cliche.statement; | |
| 16 | import static com.github.dakusui.valid8j_cliche.json.JsonObjectTo.keyList; | |
| 17 | import static com.github.dakusui.valid8j_pcond.forms.Predicates.*; | |
| 18 | import static com.github.dakusui.valid8j_pcond.forms.Printables.predicate; | |
| 19 | ||
| 20 | public class JsonSummarizer { | |
| 21 | public static JsonElement summaryObject(JsonObject root, List<Object> pathToParent, Object focus) { | |
| 22 | JsonElement previousElement = parentElement(focus, JsonUtils.asJsonElement(root, pathToParent.toArray())); | |
| 23 |
1
1. summaryObject : negated conditional → SURVIVED |
JsonElement ret = focus instanceof String ? new JsonObject() : new JsonArray(); |
| 24 |
3
1. summaryObject : negated conditional → KILLED 2. summaryObject : Replaced integer subtraction with addition → KILLED 3. summaryObject : changed conditional boundary → KILLED |
for (int i = pathToParent.size() - 1; i >= 0; i--) { |
| 25 | Object currentKey = pathToParent.get(i); | |
| 26 |
1
1. summaryObject : negated conditional → KILLED |
if (currentKey instanceof String) { |
| 27 | ret = new JsonObject(); | |
| 28 |
1
1. summaryObject : removed call to com/google/gson/JsonObject::add → KILLED |
((JsonObject) ret).add((String) currentKey, previousElement); |
| 29 |
1
1. summaryObject : negated conditional → KILLED |
} else if (currentKey instanceof Integer) { |
| 30 | ret = new JsonArray(); | |
| 31 |
1
1. summaryObject : removed call to com/google/gson/JsonArray::add → SURVIVED |
((JsonArray) ret).add(previousElement); |
| 32 | } else | |
| 33 | assert false; | |
| 34 | previousElement = ret; | |
| 35 | } | |
| 36 |
1
1. summaryObject : replaced return value with null for com/github/dakusui/json/JsonSummarizer::summaryObject → KILLED |
return ret; |
| 37 | } | |
| 38 | ||
| 39 | public static JsonElement collapseForObjectValue(JsonElement element) { | |
| 40 | JsonElement ret = collapseJsonElement(element); | |
| 41 |
2
1. collapseForObjectValue : negated conditional → KILLED 2. collapseForObjectValue : replaced return value with null for com/github/dakusui/json/JsonSummarizer::collapseForObjectValue → KILLED |
if (ret != null) return ret; |
| 42 |
1
1. collapseForObjectValue : replaced return value with null for com/github/dakusui/json/JsonSummarizer::collapseForObjectValue → SURVIVED |
return new JsonPrimitive("..."); |
| 43 | } | |
| 44 | ||
| 45 | private static JsonElement collapseJsonElement(JsonElement element) { | |
| 46 |
1
1. collapseJsonElement : negated conditional → KILLED |
if (element.isJsonArray()) { |
| 47 | JsonArray ret = new JsonArray(); | |
| 48 |
1
1. collapseJsonElement : negated conditional → KILLED |
if (!((JsonArray) element).isEmpty()) |
| 49 |
1
1. collapseJsonElement : removed call to com/google/gson/JsonArray::add → KILLED |
ret.add("..."); |
| 50 |
1
1. collapseJsonElement : replaced return value with null for com/github/dakusui/json/JsonSummarizer::collapseJsonElement → KILLED |
return ret; |
| 51 |
1
1. collapseJsonElement : negated conditional → KILLED |
} else if (element.isJsonObject()) { |
| 52 | JsonObject ret = new JsonObject(); | |
| 53 |
1
1. collapseJsonElement : negated conditional → KILLED |
if (!((JsonObject) element).keySet().isEmpty()) |
| 54 |
1
1. collapseJsonElement : removed call to com/google/gson/JsonObject::add → KILLED |
ret.add("...", new JsonPrimitive("...")); |
| 55 |
1
1. collapseJsonElement : replaced return value with null for com/github/dakusui/json/JsonSummarizer::collapseJsonElement → KILLED |
return ret; |
| 56 | } | |
| 57 | return null; | |
| 58 | } | |
| 59 | ||
| 60 | public static JsonElement collapseForArrayElement(JsonElement element) { | |
| 61 | JsonElement ret = collapseJsonElement(element); | |
| 62 |
2
1. collapseForArrayElement : replaced return value with null for com/github/dakusui/json/JsonSummarizer::collapseForArrayElement → SURVIVED 2. collapseForArrayElement : negated conditional → SURVIVED |
if (ret != null) return ret; |
| 63 |
1
1. collapseForArrayElement : replaced return value with null for com/github/dakusui/json/JsonSummarizer::collapseForArrayElement → SURVIVED |
return element; |
| 64 | } | |
| 65 | ||
| 66 | /** | |
| 67 | * <pre> | |
| 68 | * | |
| 69 | * </pre> | |
| 70 | * | |
| 71 | * @param object input JSON object. | |
| 72 | * @return A summarized JSON object. | |
| 73 | */ | |
| 74 | public static JsonObject focusedObject(JsonObject object) { | |
| 75 | JsonObject ret = new JsonObject(); | |
| 76 |
2
1. lambda$focusedObject$0 : removed call to com/google/gson/JsonObject::add → KILLED 2. focusedObject : removed call to java/util/Set::forEach → KILLED |
object.keySet().forEach(k -> ret.add(k, collapseForObjectValue(object.get(k)))); |
| 77 |
1
1. focusedObject : replaced return value with null for com/github/dakusui/json/JsonSummarizer::focusedObject → KILLED |
return ret; |
| 78 | } | |
| 79 | ||
| 80 | public static JsonElement parentElement(Object focus, JsonElement parent) { | |
| 81 |
2
1. lambda$parentElement$1 : replaced boolean return with true for com/github/dakusui/json/JsonSummarizer::lambda$parentElement$1 → SURVIVED 2. lambda$parentElement$1 : replaced boolean return with false for com/github/dakusui/json/JsonSummarizer::lambda$parentElement$1 → KILLED |
Predicate<Object> focusIsInstanceOfString = predicate("focusIsInstanceOfString", v -> focus instanceof String); |
| 82 |
2
1. lambda$parentElement$2 : replaced boolean return with true for com/github/dakusui/json/JsonSummarizer::lambda$parentElement$2 → SURVIVED 2. lambda$parentElement$2 : replaced boolean return with false for com/github/dakusui/json/JsonSummarizer::lambda$parentElement$2 → KILLED |
Predicate<Object> focusIsInstanceOfInteger = predicate("focusIsInstanceOfInteger", v -> focus instanceof Integer); |
| 83 | assert All.$( | |
| 84 | statement(focus, isNotNull()), | |
| 85 | statement(parent, allOf(isNotNull(), or( | |
| 86 | callp("isJsonObject").and(focusIsInstanceOfString), | |
| 87 | callp("isJsonArray").and(focusIsInstanceOfInteger))))); | |
| 88 |
1
1. parentElement : negated conditional → KILLED |
if (parent.isJsonObject()) |
| 89 |
1
1. parentElement : replaced return value with null for com/github/dakusui/json/JsonSummarizer::parentElement → KILLED |
return parentObject((String) focus, (JsonObject) parent); |
| 90 |
1
1. parentElement : negated conditional → KILLED |
if (parent.isJsonArray()) |
| 91 |
1
1. parentElement : replaced return value with null for com/github/dakusui/json/JsonSummarizer::parentElement → SURVIVED |
return parentArray((Integer) focus, (JsonArray) parent); |
| 92 | throw new RuntimeException(); | |
| 93 | } | |
| 94 | ||
| 95 | public static JsonObject parentObject(String focusedChildKey, JsonObject object) { | |
| 96 | assert All.$( | |
| 97 | statement(focusedChildKey, isNotNull()), | |
| 98 | statement(object, allOf( | |
| 99 | isNotNull(), | |
| 100 | Transform.$(keyList()).check(contains(focusedChildKey))))); | |
| 101 | JsonObject ret = new JsonObject(); | |
| 102 | object | |
| 103 | .keySet() | |
| 104 |
3
1. lambda$parentObject$3 : negated conditional → SURVIVED 2. parentObject : removed call to java/util/Set::forEach → KILLED 3. lambda$parentObject$3 : removed call to com/google/gson/JsonObject::add → KILLED |
.forEach(k -> ret.add(k, Objects.equals(k, focusedChildKey) ? |
| 105 | focusedElement(object.get(k)) : | |
| 106 | collapseForObjectValue(object.get(k)))); | |
| 107 |
1
1. parentObject : replaced return value with null for com/github/dakusui/json/JsonSummarizer::parentObject → KILLED |
return ret; |
| 108 | } | |
| 109 | ||
| 110 | public static JsonArray focusedArray(JsonArray array) { | |
| 111 | JsonArray ret = new JsonArray(); | |
| 112 |
2
1. lambda$focusedArray$4 : removed call to com/google/gson/JsonArray::add → SURVIVED 2. focusedArray : removed call to com/google/gson/JsonArray::forEach → SURVIVED |
array.forEach(each -> ret.add(collapseForArrayElement(each))); |
| 113 |
1
1. focusedArray : replaced return value with null for com/github/dakusui/json/JsonSummarizer::focusedArray → KILLED |
return ret; |
| 114 | } | |
| 115 | ||
| 116 | public static JsonArray parentArray(int focusedChildIndex, JsonArray array) { | |
| 117 | assert All.$( | |
| 118 | statement(array, isNotNull()), | |
| 119 | statement(focusedChildIndex, greaterThanOrEqualTo(0).and(lessThan(array.size())))); | |
| 120 | JsonArray ret = new JsonArray(); | |
| 121 | JsonElement focusedChild = array.get(focusedChildIndex); | |
| 122 |
3
1. lambda$parentArray$5 : removed call to com/google/gson/JsonArray::add → SURVIVED 2. parentArray : removed call to com/google/gson/JsonArray::forEach → SURVIVED 3. lambda$parentArray$5 : negated conditional → SURVIVED |
array.forEach(i -> ret.add(Objects.equals(i, focusedChild) ? |
| 123 | focusedElement(i) : | |
| 124 | collapseForArrayElement(i))); | |
| 125 |
1
1. parentArray : replaced return value with null for com/github/dakusui/json/JsonSummarizer::parentArray → SURVIVED |
return ret; |
| 126 | } | |
| 127 | ||
| 128 | public static JsonElement focusedElement(JsonElement element) { | |
| 129 | assert that(element, isNotNull()); | |
| 130 | JsonElement ret; | |
| 131 |
1
1. focusedElement : negated conditional → KILLED |
if (element.isJsonObject()) { |
| 132 | ret = focusedObject((JsonObject) element); | |
| 133 |
1
1. focusedElement : negated conditional → KILLED |
} else if (element.isJsonArray()) { |
| 134 | ret = focusedArray((JsonArray) element); | |
| 135 | } else { | |
| 136 | ret = element; | |
| 137 | } | |
| 138 |
1
1. focusedElement : replaced return value with null for com/github/dakusui/json/JsonSummarizer::focusedElement → KILLED |
return ret; |
| 139 | } | |
| 140 | ||
| 141 | static JsonPrimitive compactJsonPrimitive(JsonPrimitive primitive, int headLength, int tailLength) { | |
| 142 |
1
1. compactJsonPrimitive : negated conditional → SURVIVED |
if (primitive.isString()) { |
| 143 | String dots = "..."; | |
| 144 | String s = primitive.getAsString(); | |
| 145 |
4
1. compactJsonPrimitive : changed conditional boundary → SURVIVED 2. compactJsonPrimitive : negated conditional → KILLED 3. compactJsonPrimitive : Replaced integer addition with subtraction → KILLED 4. compactJsonPrimitive : Replaced integer addition with subtraction → KILLED |
if (headLength + dots.length() + tailLength >= s.length()) |
| 146 |
1
1. compactJsonPrimitive : replaced return value with null for com/github/dakusui/json/JsonSummarizer::compactJsonPrimitive → KILLED |
return primitive; |
| 147 |
2
1. compactJsonPrimitive : Replaced integer subtraction with addition → NO_COVERAGE 2. compactJsonPrimitive : replaced return value with null for com/github/dakusui/json/JsonSummarizer::compactJsonPrimitive → NO_COVERAGE |
return new JsonPrimitive(s.substring(0, headLength) + dots + s.substring(s.length() - tailLength)); |
| 148 | } | |
| 149 |
1
1. compactJsonPrimitive : replaced return value with null for com/github/dakusui/json/JsonSummarizer::compactJsonPrimitive → NO_COVERAGE |
return primitive; |
| 150 | } | |
| 151 | ||
| 152 | static JsonArray compactJsonArray(JsonArray array, int headLength, int tailLength) { | |
| 153 |
4
1. compactJsonArray : Replaced integer addition with subtraction → NO_COVERAGE 2. compactJsonArray : changed conditional boundary → NO_COVERAGE 3. compactJsonArray : Replaced integer addition with subtraction → NO_COVERAGE 4. compactJsonArray : negated conditional → NO_COVERAGE |
if (headLength + 1 + tailLength >= array.size()) |
| 154 |
1
1. compactJsonArray : replaced return value with null for com/github/dakusui/json/JsonSummarizer::compactJsonArray → NO_COVERAGE |
return array; |
| 155 | JsonArray ret = new JsonArray(); | |
| 156 |
2
1. compactJsonArray : negated conditional → NO_COVERAGE 2. compactJsonArray : changed conditional boundary → NO_COVERAGE |
for (int i = 0; i < headLength; i++) |
| 157 |
1
1. compactJsonArray : removed call to com/google/gson/JsonArray::add → NO_COVERAGE |
ret.add(array.get(i)); |
| 158 |
1
1. compactJsonArray : removed call to com/google/gson/JsonArray::add → NO_COVERAGE |
ret.add("..."); |
| 159 |
3
1. compactJsonArray : Replaced integer subtraction with addition → NO_COVERAGE 2. compactJsonArray : negated conditional → NO_COVERAGE 3. compactJsonArray : changed conditional boundary → NO_COVERAGE |
for (int i = array.size() - tailLength; i < array.size(); i++) |
| 160 |
1
1. compactJsonArray : removed call to com/google/gson/JsonArray::add → NO_COVERAGE |
ret.add(array.get(i)); |
| 161 |
1
1. compactJsonArray : replaced return value with null for com/github/dakusui/json/JsonSummarizer::compactJsonArray → NO_COVERAGE |
return ret; |
| 162 | } | |
| 163 | ||
| 164 | static JsonObject compactJsonObject(JsonObject object, int headLength, int tailLength) { | |
| 165 |
4
1. compactJsonObject : changed conditional boundary → SURVIVED 2. compactJsonObject : Replaced integer addition with subtraction → SURVIVED 3. compactJsonObject : Replaced integer addition with subtraction → KILLED 4. compactJsonObject : negated conditional → KILLED |
if (headLength + 1 + tailLength >= object.size()) |
| 166 |
1
1. compactJsonObject : replaced return value with null for com/github/dakusui/json/JsonSummarizer::compactJsonObject → KILLED |
return object; |
| 167 | JsonObject ret = new JsonObject(); | |
| 168 | List<String> keys = object.keySet().stream().sorted().toList(); | |
| 169 |
2
1. compactJsonObject : negated conditional → NO_COVERAGE 2. compactJsonObject : changed conditional boundary → NO_COVERAGE |
for (int i = 0; i < headLength; i++) |
| 170 |
1
1. compactJsonObject : removed call to com/google/gson/JsonObject::add → NO_COVERAGE |
ret.add(keys.get(i), object.get(keys.get(i))); |
| 171 |
1
1. compactJsonObject : removed call to com/google/gson/JsonObject::add → NO_COVERAGE |
ret.add(keys.get(headLength) + "...", new JsonPrimitive("...")); |
| 172 |
3
1. compactJsonObject : negated conditional → NO_COVERAGE 2. compactJsonObject : changed conditional boundary → NO_COVERAGE 3. compactJsonObject : Replaced integer subtraction with addition → NO_COVERAGE |
for (int i = keys.size() - tailLength; i < keys.size(); i++) |
| 173 |
1
1. compactJsonObject : removed call to com/google/gson/JsonObject::add → NO_COVERAGE |
ret.add(keys.get(i), object.get(keys.get(i))); |
| 174 |
1
1. compactJsonObject : replaced return value with null for com/github/dakusui/json/JsonSummarizer::compactJsonObject → NO_COVERAGE |
return ret; |
| 175 | } | |
| 176 | ||
| 177 | static JsonElement compactJsonElement(JsonElement element, int headLength, int tailLength) { | |
| 178 |
1
1. compactJsonElement : negated conditional → KILLED |
if (element.isJsonArray()) |
| 179 |
1
1. compactJsonElement : replaced return value with null for com/github/dakusui/json/JsonSummarizer::compactJsonElement → NO_COVERAGE |
return compactJsonArray((JsonArray) element, headLength, tailLength); |
| 180 |
1
1. compactJsonElement : negated conditional → KILLED |
if (element.isJsonObject()) |
| 181 |
1
1. compactJsonElement : replaced return value with null for com/github/dakusui/json/JsonSummarizer::compactJsonElement → KILLED |
return compactJsonObject((JsonObject) element, headLength, tailLength); |
| 182 |
1
1. compactJsonElement : negated conditional → SURVIVED |
if (element.isJsonPrimitive()) |
| 183 |
1
1. compactJsonElement : replaced return value with null for com/github/dakusui/json/JsonSummarizer::compactJsonElement → KILLED |
return compactJsonPrimitive((JsonPrimitive) element, headLength, tailLength); |
| 184 |
1
1. compactJsonElement : replaced return value with null for com/github/dakusui/json/JsonSummarizer::compactJsonElement → NO_COVERAGE |
return element; |
| 185 | } | |
| 186 | } | |
Mutations | ||
| 23 |
1.1 |
|
| 24 |
1.1 2.2 3.3 |
|
| 26 |
1.1 |
|
| 28 |
1.1 |
|
| 29 |
1.1 |
|
| 31 |
1.1 |
|
| 36 |
1.1 |
|
| 41 |
1.1 2.2 |
|
| 42 |
1.1 |
|
| 46 |
1.1 |
|
| 48 |
1.1 |
|
| 49 |
1.1 |
|
| 50 |
1.1 |
|
| 51 |
1.1 |
|
| 53 |
1.1 |
|
| 54 |
1.1 |
|
| 55 |
1.1 |
|
| 62 |
1.1 2.2 |
|
| 63 |
1.1 |
|
| 76 |
1.1 2.2 |
|
| 77 |
1.1 |
|
| 81 |
1.1 2.2 |
|
| 82 |
1.1 2.2 |
|
| 88 |
1.1 |
|
| 89 |
1.1 |
|
| 90 |
1.1 |
|
| 91 |
1.1 |
|
| 104 |
1.1 2.2 3.3 |
|
| 107 |
1.1 |
|
| 112 |
1.1 2.2 |
|
| 113 |
1.1 |
|
| 122 |
1.1 2.2 3.3 |
|
| 125 |
1.1 |
|
| 131 |
1.1 |
|
| 133 |
1.1 |
|
| 138 |
1.1 |
|
| 142 |
1.1 |
|
| 145 |
1.1 2.2 3.3 4.4 |
|
| 146 |
1.1 |
|
| 147 |
1.1 2.2 |
|
| 149 |
1.1 |
|
| 153 |
1.1 2.2 3.3 4.4 |
|
| 154 |
1.1 |
|
| 156 |
1.1 2.2 |
|
| 157 |
1.1 |
|
| 158 |
1.1 |
|
| 159 |
1.1 2.2 3.3 |
|
| 160 |
1.1 |
|
| 161 |
1.1 |
|
| 165 |
1.1 2.2 3.3 4.4 |
|
| 166 |
1.1 |
|
| 169 |
1.1 2.2 |
|
| 170 |
1.1 |
|
| 171 |
1.1 |
|
| 172 |
1.1 2.2 3.3 |
|
| 173 |
1.1 |
|
| 174 |
1.1 |
|
| 178 |
1.1 |
|
| 179 |
1.1 |
|
| 180 |
1.1 |
|
| 181 |
1.1 |
|
| 182 |
1.1 |
|
| 183 |
1.1 |
|
| 184 |
1.1 |