Skip to content

Commit

Permalink
Merge pull request #79 from gcatanese/process-array-list
Browse files Browse the repository at this point in the history
JSON Array processing
  • Loading branch information
gcatanese authored Mar 18, 2024
2 parents 6aebfc3 + 10b69a2 commit 6cb7f87
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

Expand Down Expand Up @@ -117,13 +118,18 @@ private String traverseMap(LinkedHashMap<String, Object> linkedHashMap, String r
value = ((String)value).replace("\\\"", "\"");

ret = ret + JSON_ESCAPE_DOUBLE_QUOTE + key + JSON_ESCAPE_DOUBLE_QUOTE + ": " +
JSON_ESCAPE_DOUBLE_QUOTE + value + JSON_ESCAPE_DOUBLE_QUOTE;
JSON_ESCAPE_DOUBLE_QUOTE + formatString((String) value) + JSON_ESCAPE_DOUBLE_QUOTE;
} else if (value instanceof Boolean) {
ret = ret + JSON_ESCAPE_DOUBLE_QUOTE + key + JSON_ESCAPE_DOUBLE_QUOTE + ": " +
value;
} else if (value instanceof Integer) {
ret = ret + JSON_ESCAPE_DOUBLE_QUOTE + key + JSON_ESCAPE_DOUBLE_QUOTE + ": " +
value;
} else if (value instanceof LinkedHashMap) {
String in = ret + JSON_ESCAPE_DOUBLE_QUOTE + key + JSON_ESCAPE_DOUBLE_QUOTE + ": ";
ret = traverseMap(((LinkedHashMap<String, Object>) value), in);
} else if (value instanceof ArrayList<?>) {
ret = ret + JSON_ESCAPE_DOUBLE_QUOTE + key + JSON_ESCAPE_DOUBLE_QUOTE + ": " + getJsonArray((ArrayList<Object>) value);
} else {
LOGGER.warn("Value type unrecognised: " + value.getClass());
}
Expand All @@ -140,6 +146,51 @@ private String traverseMap(LinkedHashMap<String, Object> linkedHashMap, String r
return ret;
}

String formatString(String str) {
String ret = "";

if(str.startsWith("{")) {
// isJson (escape double quotes in json: "live" to \\\"live\\\")
ret = str.replace("\"", "\\\\\\\"");
} else {
ret = str;
}

return ret;
}

String getJsonArray(ArrayList<Object> list) {
String ret = "";

for(Object element: list) {
if(element instanceof String) {
ret = ret + getStringArrayElement((String) element) + ", ";
} else if(element instanceof LinkedHashMap) {
ret = traverseMap((LinkedHashMap<String, Object>) element, ret) + ", ";
}
}

if(ret.endsWith(", ")) {
ret = ret.substring(0, ret.length() - 2);
}

return "[" + ret + "]";
}

String getStringArrayElement(String element) {
String ret = "";

if(element.startsWith("{")) {
// isJson (escape all double quotes)
ret = ret + element.replace("\"", JSON_ESCAPE_DOUBLE_QUOTE);
} else {
// string element (add escaped double quotes)
ret = ret + JSON_ESCAPE_DOUBLE_QUOTE + element + JSON_ESCAPE_DOUBLE_QUOTE;
}

return ret;
}

String getPostmanType(CodegenProperty codegenProperty) {
if(codegenProperty.isNumeric) {
return "number";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.openapitools.codegen.CodegenParameter;
import org.openapitools.codegen.CodegenProperty;

import java.util.ArrayList;
import java.util.LinkedHashMap;

import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -151,4 +152,121 @@ public void convertNestedLinkedHashMapToJson() {

}

@Test
public void convertNestedArrayListToJson() {

final String EXPECTED =
"{\\n " +
"\\\"id\\\": 1,\\n \\\"city\\\": \\\"Amsterdam\\\",\\n " +
"\\\"tags\\\": [\\\"ams\\\", \\\"adam\\\"]" +
"\\n}";

LinkedHashMap<String, Object> city = new LinkedHashMap<>();
city.put("id", 1);
city.put("city", "Amsterdam");
ArrayList<String> tags = new ArrayList<>();
tags.add("ams");
tags.add("adam");
city.put("tags", tags);

assertEquals(EXPECTED, new ExampleJsonHelper().convertToJson(city));

}

@Test
public void convertNestedEmptyArrayListToJson() {

final String EXPECTED =
"{\\n " +
"\\\"id\\\": 1,\\n \\\"city\\\": \\\"Amsterdam\\\",\\n " +
"\\\"tags\\\": []" +
"\\n}";

LinkedHashMap<String, Object> city = new LinkedHashMap<>();
city.put("id", 1);
city.put("city", "Amsterdam");
ArrayList<String> tags = new ArrayList<>();
city.put("tags", tags);

assertEquals(EXPECTED, new ExampleJsonHelper().convertToJson(city));

}

@Test
public void convertNestedArrayListIncludingDoubleQuotesToJson() {

final String EXPECTED =
"{\\n " +
"\\\"id\\\": 1,\\n \\\"city\\\": \\\"Amsterdam\\\",\\n " +
"\\\"tags\\\": [{\\\"live\\\": \\\"false\\\", \\\"demo\\\": \\\"yes\\\"}]" +
"\\n}";

LinkedHashMap<String, Object> city = new LinkedHashMap<>();
city.put("id", 1);
city.put("city", "Amsterdam");
ArrayList<String> tags = new ArrayList<>();
tags.add("{\"live\": \"false\", \"demo\": \"yes\"}");
city.put("tags", tags);

assertEquals(EXPECTED, new ExampleJsonHelper().convertToJson(city));

}

@Test
public void convertNestedArrayObjectListToJson() {

final String EXPECTED =
"{\\n " +
"\\\"id\\\": 1,\\n " +
"\\\"lineItems\\\": [" +
"{\\n \\\"quantity\\\": 10,\\n \\\"description\\\": \\\"item1\\\"\\n}, " +
"{\\n \\\"quantity\\\": 100,\\n \\\"description\\\": \\\"item2\\\"\\n}, " +
"{\\n \\\"quantity\\\": 20,\\n \\\"description\\\": \\\"item3\\\"\\n}" +
"]" +
"\\n}";

LinkedHashMap<String, Object> order = new LinkedHashMap<>();
order.put("id", 1);
ArrayList<LinkedHashMap> lineItems = new ArrayList<>();
lineItems.add(new LinkedHashMap<String, Object>() {{
put("quantity", 10);
put("description", "item1");
}});
lineItems.add(new LinkedHashMap<String, Object>() {{
put("quantity", 100);
put("description", "item2");
}});
lineItems.add(new LinkedHashMap<String, Object>() {{
put("quantity", 20);
put("description", "item3");
}});

order.put("lineItems", lineItems);

assertEquals(EXPECTED, new ExampleJsonHelper().convertToJson(order));

}

class LineItem {
Integer quantity;
String description;

LineItem(Integer quantity, String description) {
this.quantity = quantity;
this.description = description;
}
}



@Test
public void formatString() {
final String EXPECTED = "{\\\\\\\"live\\\\\\\": \\\\\\\"false\\\\\\\", \\\\\\\"demo\\\\\\\": \\\\\\\"yes\\\\\\\"}";

String json = "{\"live\": \"false\", \"demo\": \"yes\"}";

assertEquals(EXPECTED, new ExampleJsonHelper().formatString(json));
}


}

0 comments on commit 6cb7f87

Please sign in to comment.