@@ -83,7 +83,8 @@ public class U<T> extends com.github.underscore.U<T> {
8383 }
8484
8585 public enum Mode {
86- REPLACE_SELF_CLOSING_WITH_NULL ;
86+ REPLACE_SELF_CLOSING_WITH_NULL ,
87+ REPLACE_SELF_CLOSING_WITH_EMPTY ;
8788 }
8889
8990 public U (final Iterable <T > iterable ) {
@@ -2256,12 +2257,19 @@ public static String jsonToXml(String json) {
22562257
22572258 @ SuppressWarnings ("unchecked" )
22582259 public static String xmlToJson (String xml , Json .JsonStringBuilder .Step identStep , Mode mode ) {
2259- Object result = Xml .fromXml (xml );
2260- if (result instanceof Map ) {
2261- return Json .toJson (mode == Mode .REPLACE_SELF_CLOSING_WITH_NULL ?
2262- replaceSelfClosingWithNull ((Map ) result ) : (Map ) result , identStep );
2260+ Object object = Xml .fromXml (xml );
2261+ final String result ;
2262+ if (object instanceof Map ) {
2263+ if (mode == Mode .REPLACE_SELF_CLOSING_WITH_NULL ) {
2264+ result = Json .toJson (replaceSelfClosingWithNull ((Map ) object ), identStep );
2265+ } else if (mode == Mode .REPLACE_SELF_CLOSING_WITH_EMPTY ) {
2266+ result = Json .toJson (replaceSelfClosingWithEmpty ((Map ) object ), identStep );
2267+ } else {
2268+ result = Json .toJson ((Map ) object , identStep );
2269+ }
2270+ return result ;
22632271 }
2264- return Json .toJson ((List ) result , identStep );
2272+ return Json .toJson ((List ) object , identStep );
22652273 }
22662274
22672275 public static String xmlToJson (String xml ) {
@@ -2361,21 +2369,50 @@ public static boolean isJsonNumber(final String string) {
23612369 return numberEncountered ;
23622370 }
23632371
2372+ @ SuppressWarnings ("unchecked" )
23642373 public static Map <String , Object > replaceSelfClosingWithNull (Map <String , Object > map ) {
2365- Map <String , Object > outMap = newLinkedHashMap ();
2374+ return (Map <String , Object >) replaceSelfClosingWithValue (map , null );
2375+ }
2376+
2377+ @ SuppressWarnings ("unchecked" )
2378+ public static Map <String , Object > replaceSelfClosingWithEmpty (Map <String , Object > map ) {
2379+ return (Map <String , Object >) replaceSelfClosingWithValue (map , "" );
2380+ }
2381+
2382+ @ SuppressWarnings ("unchecked" )
2383+ public static Object replaceSelfClosingWithValue (Map <String , Object > map , String value ) {
2384+ Object outMap = newLinkedHashMap ();
23662385 for (Map .Entry <String , Object > entry : map .entrySet ()) {
23672386 if ("-self-closing" .equals (entry .getKey ()) && "true" .equals (entry .getValue ())) {
23682387 if (map .size () == 1 ) {
2369- outMap = null ;
2388+ outMap = value ;
23702389 break ;
23712390 }
23722391 continue ;
23732392 }
2374- outMap .put (String .valueOf (entry .getKey ()), makeObjectSelfClose (entry .getValue ()));
2393+ ((Map <String , Object >) outMap ).put (String .valueOf (entry .getKey ()),
2394+ makeObjectSelfClose (entry .getValue (), value ));
23752395 }
23762396 return outMap ;
23772397 }
23782398
2399+ @ SuppressWarnings ("unchecked" )
2400+ private static Object makeObjectSelfClose (Object value , String newValue ) {
2401+ final Object result ;
2402+ if (value instanceof List ) {
2403+ List <Object > values = newArrayList ();
2404+ for (Object item : (List ) value ) {
2405+ values .add (item instanceof Map ? replaceSelfClosingWithValue ((Map ) item , newValue ) : item );
2406+ }
2407+ result = values ;
2408+ } else if (value instanceof Map ) {
2409+ result = replaceSelfClosingWithValue ((Map ) value , newValue );
2410+ } else {
2411+ result = value ;
2412+ }
2413+ return result ;
2414+ }
2415+
23792416 public static long gcd (long value1 , long value2 ) {
23802417 if (value1 == 0 ) {
23812418 return value2 ;
@@ -2391,23 +2428,6 @@ public static long findGcd(long ... array) {
23912428 return result ;
23922429 }
23932430
2394- @ SuppressWarnings ("unchecked" )
2395- private static Object makeObjectSelfClose (Object value ) {
2396- final Object result ;
2397- if (value instanceof List ) {
2398- List <Object > values = newArrayList ();
2399- for (Object item : (List ) value ) {
2400- values .add (item instanceof Map ? replaceSelfClosingWithNull ((Map ) item ) : item );
2401- }
2402- result = values ;
2403- } else if (value instanceof Map ) {
2404- result = replaceSelfClosingWithNull ((Map ) value );
2405- } else {
2406- result = value ;
2407- }
2408- return result ;
2409- }
2410-
24112431 public static Builder objectBuilder () {
24122432 return new U .Builder ();
24132433 }
0 commit comments