Skip to content

Commit 72501b7

Browse files
committed
Sync with underscore-java.
1 parent 5af137b commit 72501b7

File tree

6 files changed

+198
-50
lines changed

6 files changed

+198
-50
lines changed

pom-pack.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
<option>-keep public class com.github.underscore.Xml$XmlStringBuilder$Step { *; }</option>
117117
<option>-keep public class com.github.underscore.Xml$XmlStringBuilder { *; }</option>
118118
<option>-keep public class com.github.underscore.Xml$FromType { *; }</option>
119+
<option>-keep public class com.github.underscore.Xml$ArrayTrue { *; }</option>
119120
<option>-keepclassmembers class * { *** newArrayList(); *** newLinkedHashSet(); *** newHashSet(java.lang.Iterable); *** newLinkedHashMap(); }</option>
120121
<option>-dontnote com.github.underscore.*$ClassForName</option>
121122
<option>-dontnote com.github.underscore.*$OperationType</option>

pom-pack17.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
<option>-keep public class com.github.underscore.Xml$XmlStringBuilder$Step { *; }</option>
117117
<option>-keep public class com.github.underscore.Xml$XmlStringBuilder { *; }</option>
118118
<option>-keep public class com.github.underscore.Xml$FromType { *; }</option>
119+
<option>-keep public class com.github.underscore.Xml$ArrayTrue { *; }</option>
119120
<option>-keepclassmembers class * { *** newArrayList(); *** newLinkedHashSet(); *** newHashSet(java.lang.Iterable); *** newLinkedHashMap(); }</option>
120121
<option>-dontnote com.github.underscore.*$ClassForName</option>
121122
<option>-dontnote com.github.underscore.*$OperationType</option>

src/main/java/com/github/underscore/U.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"java:S4423",
6363
"java:S4830",
6464
"java:S5843",
65+
"java:S5996",
6566
"java:S5998"
6667
})
6768
public class U<T> extends Underscore<T> {
@@ -134,7 +135,9 @@ public enum Mode {
134135
REPLACE_NULL_WITH_EMPTY_VALUE,
135136
REPLACE_EMPTY_STRING_WITH_EMPTY_VALUE,
136137
REMOVE_FIRST_LEVEL_XML_TO_JSON,
137-
FORCE_ADD_ROOT_JSON_TO_XML
138+
FORCE_ADD_ROOT_JSON_TO_XML,
139+
FORCE_REMOVE_ARRAY_ATTRIBUTE_JSON_TO_XML,
140+
FORCE_REMOVE_ARRAY_BOOLEAN_NUMBER_ATTRIBUTES_JSON_TO_XML
138141
}
139142

140143
public U(final Iterable<T> iterable) {
@@ -2573,6 +2576,15 @@ public static String jsonToXml(
25732576
final Map<String, Object> map = U.newLinkedHashMap();
25742577
map.put(newRootName, object);
25752578
result = Xml.toXml(map, identStep);
2579+
} else if (mode == Mode.FORCE_REMOVE_ARRAY_ATTRIBUTE_JSON_TO_XML) {
2580+
result = Xml.toXml((Map) object, identStep, newRootName, Xml.ArrayTrue.SKIP);
2581+
} else if (mode == Mode.FORCE_REMOVE_ARRAY_BOOLEAN_NUMBER_ATTRIBUTES_JSON_TO_XML) {
2582+
result =
2583+
Xml.toXml(
2584+
replaceNumberAndBooleanWithString((Map) object),
2585+
identStep,
2586+
newRootName,
2587+
Xml.ArrayTrue.SKIP);
25762588
} else {
25772589
result = Xml.toXml((Map) object, identStep);
25782590
}
@@ -2937,6 +2949,36 @@ private static Object makeReplaceEmptyString(Object value) {
29372949
return result;
29382950
}
29392951

2952+
public static Map<String, Object> replaceNumberAndBooleanWithString(Map<String, Object> map) {
2953+
Map<String, Object> outMap = newLinkedHashMap();
2954+
for (Map.Entry<String, Object> entry : map.entrySet()) {
2955+
outMap.put(
2956+
entry.getKey(),
2957+
entry.getValue() instanceof Boolean || entry.getValue() instanceof Number
2958+
? String.valueOf(entry.getValue())
2959+
: makeReplaceNumberAndBoolean(entry.getValue()));
2960+
}
2961+
return outMap;
2962+
}
2963+
2964+
@SuppressWarnings("unchecked")
2965+
private static Object makeReplaceNumberAndBoolean(Object value) {
2966+
final Object result;
2967+
if (value instanceof List) {
2968+
List<Object> values = newArrayList();
2969+
for (Object item : (List) value) {
2970+
values.add(
2971+
item instanceof Map ? replaceNumberAndBooleanWithString((Map) item) : item);
2972+
}
2973+
result = values;
2974+
} else if (value instanceof Map) {
2975+
result = replaceNumberAndBooleanWithString((Map) value);
2976+
} else {
2977+
result = value;
2978+
}
2979+
return result;
2980+
}
2981+
29402982
public static Map<String, Object> replaceFirstLevel(Map<String, Object> map) {
29412983
return replaceFirstLevel(map, 0);
29422984
}

0 commit comments

Comments
 (0)