|
1 | 1 | package dev.dochia.cli.core.util; |
2 | 2 |
|
3 | | -import static com.fasterxml.jackson.core.JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN; |
4 | | -import static com.fasterxml.jackson.databind.SerializationFeature.FAIL_ON_EMPTY_BEANS; |
5 | | -import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT; |
6 | | -import static com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS; |
7 | | -import static com.fasterxml.jackson.databind.SerializationFeature.WRITE_ENUMS_USING_TO_STRING; |
8 | | - |
9 | 3 | import com.fasterxml.jackson.annotation.JsonInclude; |
10 | 4 | import com.fasterxml.jackson.core.JsonGenerator; |
11 | 5 | import com.fasterxml.jackson.databind.JsonNode; |
|
36 | 30 | import dev.dochia.cli.core.model.ann.ExcludeTestCaseStrategy; |
37 | 31 | import io.github.ludovicianul.prettylogger.PrettyLogger; |
38 | 32 | import io.github.ludovicianul.prettylogger.PrettyLoggerFactory; |
| 33 | +import net.minidev.json.JSONArray; |
| 34 | +import net.minidev.json.JSONValue; |
| 35 | +import net.minidev.json.parser.JSONParser; |
| 36 | +import org.apache.commons.lang3.StringUtils; |
| 37 | +import org.apache.commons.text.StringEscapeUtils; |
| 38 | + |
39 | 39 | import java.io.IOException; |
40 | 40 | import java.io.StringReader; |
41 | 41 | import java.io.StringWriter; |
|
50 | 50 | import java.util.regex.Matcher; |
51 | 51 | import java.util.regex.Pattern; |
52 | 52 | import java.util.stream.Collectors; |
53 | | -import net.minidev.json.JSONArray; |
54 | | -import net.minidev.json.JSONValue; |
55 | | -import net.minidev.json.parser.JSONParser; |
56 | | -import org.apache.commons.lang3.StringUtils; |
57 | | -import org.apache.commons.text.StringEscapeUtils; |
| 53 | + |
| 54 | +import static com.fasterxml.jackson.core.JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN; |
| 55 | +import static com.fasterxml.jackson.databind.SerializationFeature.FAIL_ON_EMPTY_BEANS; |
| 56 | +import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT; |
| 57 | +import static com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS; |
| 58 | +import static com.fasterxml.jackson.databind.SerializationFeature.WRITE_ENUMS_USING_TO_STRING; |
58 | 59 |
|
59 | 60 | /** |
60 | 61 | * Utility class for JSON objects interaction. |
@@ -249,12 +250,21 @@ public static boolean equalAsJson(String json1, String json2) { |
249 | 250 | if (!isValidJson(json1) || !isValidJson(json2)) { |
250 | 251 | return json1.equalsIgnoreCase(json2); |
251 | 252 | } |
| 253 | + |
252 | 254 | try { |
253 | | - return JsonPath.parse(json1).jsonString().contentEquals(JsonPath.parse(json2).jsonString()); |
254 | | - } catch (UnsupportedOperationException _) { |
255 | | - String json1Unescaped = StringEscapeUtils.unescapeJson(json1).replaceAll("(^[\"'])|([\"']$)", ""); |
256 | | - String json2Unescaped = StringEscapeUtils.unescapeJson(json2).replaceAll("(^[\"'])|([\"']$)", ""); |
257 | | - return JsonPath.parse(json1Unescaped).jsonString().contentEquals(JsonPath.parse(json2Unescaped).jsonString()); |
| 255 | + JsonNode n1 = SIMPLE_OBJECT_MAPPER.readTree(json1); |
| 256 | + JsonNode n2 = SIMPLE_OBJECT_MAPPER.readTree(json2); |
| 257 | + return n1.equals(n2); // object key order does NOT matter |
| 258 | + } catch (Exception _) { |
| 259 | + try { |
| 260 | + String j1 = StringEscapeUtils.unescapeJson(json1).replaceAll("(^[\"'])|([\"']$)", ""); |
| 261 | + String j2 = StringEscapeUtils.unescapeJson(json2).replaceAll("(^[\"'])|([\"']$)", ""); |
| 262 | + JsonNode n1 = SIMPLE_OBJECT_MAPPER.readTree(j1); |
| 263 | + JsonNode n2 = SIMPLE_OBJECT_MAPPER.readTree(j2); |
| 264 | + return n1.equals(n2); |
| 265 | + } catch (Exception _) { |
| 266 | + return json1.equalsIgnoreCase(json2); |
| 267 | + } |
258 | 268 | } |
259 | 269 | } |
260 | 270 |
|
|
0 commit comments