Skip to content

Commit 2d01c2b

Browse files
json sanitizer refactor to reduce decoding encoding redundancy
1 parent ca4c590 commit 2d01c2b

File tree

2 files changed

+82
-40
lines changed

2 files changed

+82
-40
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
11
package com.blackduck.integration.detectable.util;
22

3-
import com.google.gson.Gson;
4-
import com.google.gson.GsonBuilder;
53
import com.google.gson.JsonObject;
64
import com.google.gson.JsonParser;
75
import com.google.gson.JsonSyntaxException;
86

97
public class JsonSanitizer {
10-
private static final Gson GSON = new GsonBuilder()
11-
.disableHtmlEscaping()
12-
.serializeNulls()
13-
.create();
14-
15-
public static String sanitize(String json) throws JsonSyntaxException {
16-
JsonObject jsonObject = JsonParser.parseString(json).getAsJsonObject();
17-
return GSON.toJson(jsonObject);
8+
public static JsonObject sanitize(String json) throws JsonSyntaxException {
9+
return JsonParser.parseString(json).getAsJsonObject();
1810
}
1911
}
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,105 @@
11
package com.blackduck.integration.detectable.util;
22

3+
import com.google.gson.JsonObject;
34
import com.google.gson.JsonSyntaxException;
4-
import static org.junit.jupiter.api.Assertions.*;
55
import org.junit.jupiter.api.Test;
6+
import static org.junit.jupiter.api.Assertions.*;
67

7-
public class JsonSanitizerTest {
8-
8+
class JsonSanitizerTest {
99
@Test
10-
void testValidJsonNoDuplicates() {
11-
String input = "{\"name\": \"John\", \"age\": 30}";
12-
String expected = "{\"name\":\"John\",\"age\":30}";
13-
String actual = JsonSanitizer.sanitize(input);
14-
assertEquals(expected, actual);
10+
void testValidJson() {
11+
String json = "{ " +
12+
"\"name\": \"my-project\", " +
13+
"\"version\": \"1.0.0\", " +
14+
"\"private\": true, " +
15+
"\"scripts\": { " +
16+
"\"start\": \"node index.js\", " +
17+
"\"test\": \"jest\"" +
18+
"}, " +
19+
"\"dependencies\": { " +
20+
"\"express\": \"^4.17.1\", " +
21+
"\"lodash\": \"^4.17.21\"" +
22+
"}, " +
23+
"\"devDependencies\": { " +
24+
"\"jest\": \"^27.0.0\", " +
25+
"\"eslint\": \"^7.32.0\"" +
26+
"}" +
27+
"}";
28+
29+
JsonObject sanitized = JsonSanitizer.sanitize(json);
30+
31+
assertEquals("my-project", sanitized.get("name").getAsString());
32+
assertEquals("1.0.0", sanitized.get("version").getAsString());
33+
assertTrue(sanitized.get("private").getAsBoolean());
34+
assertEquals("node index.js", sanitized.getAsJsonObject("scripts").get("start").getAsString());
35+
assertEquals("^4.17.1", sanitized.getAsJsonObject("dependencies").get("express").getAsString());
36+
assertEquals("^27.0.0", sanitized.getAsJsonObject("devDependencies").get("jest").getAsString());
1537
}
1638

1739
@Test
18-
void testValidJsonWithDuplicates() {
19-
String input = "{\"name\": \"John\", \"name\": \"Doe\", \"age\": 30}";
20-
String expected = "{\"name\":\"Doe\",\"age\":30}";
21-
String actual = JsonSanitizer.sanitize(input);
22-
assertEquals(expected, actual);
40+
void testDuplicateKeysInDependencies() {
41+
String json = "{ " +
42+
"\"dependencies\": { " +
43+
"\"express\": \"^4.17.1\", " +
44+
"\"express\": \"^5.0.0\"" +
45+
"}" +
46+
"}";
47+
48+
JsonObject sanitized = JsonSanitizer.sanitize(json);
49+
50+
assertEquals("^5.0.0", sanitized.getAsJsonObject("dependencies").get("express").getAsString()); // Last value is kept
2351
}
2452

2553
@Test
26-
void testNestedJsonWithDuplicates() {
27-
String input = "{\"person\": {\"name\": \"John\", \"name\": \"Doe\"}, \"age\": 30}";
28-
String expected = "{\"person\":{\"name\":\"Doe\"},\"age\":30}";
29-
String actual = JsonSanitizer.sanitize(input);
30-
assertEquals(expected, actual);
54+
void testNestedScriptsAndDependencies() {
55+
String json = "{ " +
56+
"\"scripts\": { " +
57+
"\"build\": \"tsc\", " +
58+
"\"start\": \"node dist/index.js\"" +
59+
"}, " +
60+
"\"dependencies\": { " +
61+
"\"typescript\": \"^4.0.0\", " +
62+
"\"node-fetch\": \"^2.6.1\"" +
63+
"}" +
64+
"}";
65+
66+
JsonObject sanitized = JsonSanitizer.sanitize(json);
67+
68+
assertEquals("tsc", sanitized.getAsJsonObject("scripts").get("build").getAsString());
69+
assertEquals("^4.0.0", sanitized.getAsJsonObject("dependencies").get("typescript").getAsString());
3170
}
3271

3372
@Test
3473
void testInvalidJson() {
35-
String invalidJson = "{\"name\":\"example\",\"version\":}";
36-
assertThrows(JsonSyntaxException.class, () -> {
37-
JsonSanitizer.sanitize(invalidJson);
38-
});
74+
String json = "{ " +
75+
"\"name\": \"my-project\", " +
76+
"\"version\": \"1.0.0\"";
77+
78+
assertThrows(JsonSyntaxException.class, () -> JsonSanitizer.sanitize(json));
3979
}
4080

4181
@Test
42-
void testEmptyJson() {
43-
String emptyJson = "{}";
44-
String sanitizedJson = JsonSanitizer.sanitize(emptyJson);
45-
assertEquals(emptyJson, sanitizedJson);
82+
void testEmptyPackageJson() {
83+
String json = "{}";
84+
85+
JsonObject sanitized = JsonSanitizer.sanitize(json);
86+
assertTrue(sanitized.entrySet().isEmpty());
4687
}
4788

4889
@Test
4990
void testJsonWithNullValues() {
50-
String input = "{\"name\": null, \"age\": 30}";
51-
String expected = "{\"name\":null,\"age\":30}";
52-
String actual = JsonSanitizer.sanitize(input);
53-
assertEquals(expected, actual);
91+
String json = "{ " +
92+
"\"name\": \"my-project\", " +
93+
"\"version\": null, " +
94+
"\"dependencies\": { " +
95+
"\"express\": \"^4.17.1\", " +
96+
"\"lodash\": null " +
97+
"} " +
98+
"}";
99+
100+
JsonObject sanitized = JsonSanitizer.sanitize(json);
101+
102+
assertTrue(sanitized.get("version").isJsonNull());
103+
assertTrue(sanitized.getAsJsonObject("dependencies").get("lodash").isJsonNull());
54104
}
55105
}

0 commit comments

Comments
 (0)