@@ -95,7 +95,8 @@ public class U<T> extends com.github.underscore.U<T> {
9595
9696 public enum Mode {
9797 REPLACE_SELF_CLOSING_WITH_NULL ,
98- REPLACE_SELF_CLOSING_WITH_EMPTY
98+ REPLACE_SELF_CLOSING_WITH_EMPTY ,
99+ REPLACE_EMPTY_VALUE_WITH_NULL
99100 }
100101
101102 public U (final Iterable <T > iterable ) {
@@ -2267,6 +2268,8 @@ public static String xmlToJson(String xml, Json.JsonStringBuilder.Step identStep
22672268 result = Json .toJson (replaceSelfClosingWithNull ((Map ) object ), identStep );
22682269 } else if (mode == Mode .REPLACE_SELF_CLOSING_WITH_EMPTY ) {
22692270 result = Json .toJson (replaceSelfClosingWithEmpty ((Map ) object ), identStep );
2271+ } else if (mode == Mode .REPLACE_EMPTY_VALUE_WITH_NULL ) {
2272+ result = Json .toJson (replaceEmptyValueWithNull ((Map ) object ), identStep );
22702273 } else {
22712274 result = Json .toJson ((Map ) object , identStep );
22722275 }
@@ -2416,6 +2419,36 @@ private static Object makeObjectSelfClose(Object value, String newValue) {
24162419 return result ;
24172420 }
24182421
2422+ @ SuppressWarnings ("unchecked" )
2423+ public static Map <String , Object > replaceEmptyValueWithNull (Map <String , Object > map ) {
2424+ if (map .isEmpty ()) {
2425+ return null ;
2426+ }
2427+ Map <String , Object > outMap = newLinkedHashMap ();
2428+ for (Map .Entry <String , Object > entry : map .entrySet ()) {
2429+ outMap .put (String .valueOf (entry .getKey ()),
2430+ makeObjectEmptyValue (entry .getValue ()));
2431+ }
2432+ return outMap ;
2433+ }
2434+
2435+ @ SuppressWarnings ("unchecked" )
2436+ private static Object makeObjectEmptyValue (Object value ) {
2437+ final Object result ;
2438+ if (value instanceof List ) {
2439+ List <Object > values = newArrayList ();
2440+ for (Object item : (List ) value ) {
2441+ values .add (item instanceof Map ? replaceEmptyValueWithNull ((Map ) item ) : item );
2442+ }
2443+ result = values ;
2444+ } else if (value instanceof Map ) {
2445+ result = replaceEmptyValueWithNull ((Map ) value );
2446+ } else {
2447+ result = value ;
2448+ }
2449+ return result ;
2450+ }
2451+
24192452 public static long gcd (long value1 , long value2 ) {
24202453 if (value1 == 0 ) {
24212454 return value2 ;
0 commit comments