@@ -96,7 +96,8 @@ public class U<T> extends com.github.underscore.U<T> {
9696 public enum Mode {
9797 REPLACE_SELF_CLOSING_WITH_NULL ,
9898 REPLACE_SELF_CLOSING_WITH_EMPTY ,
99- REPLACE_EMPTY_VALUE_WITH_NULL
99+ REPLACE_EMPTY_VALUE_WITH_NULL ,
100+ FORCE_ATTRIBUTE_USAGE
100101 }
101102
102103 public U (final Iterable <T > iterable ) {
@@ -2241,16 +2242,27 @@ public Object fromXml() {
22412242 return Xml .fromXml (getString ().get ());
22422243 }
22432244
2244- public static String jsonToXml (String json , Xml .XmlStringBuilder .Step identStep ) {
2245- Object result = Json .fromJson (json );
2246- if (result instanceof Map ) {
2247- return Xml .toXml ((Map ) result , identStep );
2245+ @ SuppressWarnings ("unchecked" )
2246+ public static String jsonToXml (String json , Xml .XmlStringBuilder .Step identStep , Mode mode ) {
2247+ Object object = Json .fromJson (json );
2248+ final String result ;
2249+ if (object instanceof Map ) {
2250+ if (mode == Mode .FORCE_ATTRIBUTE_USAGE ) {
2251+ result = Xml .toXml (forceAttributeUsage ((Map ) object ), identStep );
2252+ } else {
2253+ result = Xml .toXml ((Map ) object , identStep );
2254+ }
2255+ return result ;
22482256 }
2249- return Xml .toXml ((List ) result , identStep );
2257+ return Xml .toXml ((List ) object , identStep );
2258+ }
2259+
2260+ public static String jsonToXml (String json , Mode mode ) {
2261+ return jsonToXml (json , Xml .XmlStringBuilder .Step .TWO_SPACES , mode );
22502262 }
22512263
22522264 public static String jsonToXml (String json ) {
2253- return jsonToXml (json , Xml .XmlStringBuilder .Step .TWO_SPACES );
2265+ return jsonToXml (json , Xml .XmlStringBuilder .Step .TWO_SPACES , null );
22542266 }
22552267
22562268 @ SuppressWarnings ("unchecked" )
@@ -2443,6 +2455,35 @@ private static Object makeObjectEmptyValue(Object value) {
24432455 return result ;
24442456 }
24452457
2458+ @ SuppressWarnings ("unchecked" )
2459+ public static Map <String , Object > forceAttributeUsage (Map <String , Object > map ) {
2460+ Map <String , Object > outMap = newLinkedHashMap ();
2461+ for (Map .Entry <String , Object > entry : map .entrySet ()) {
2462+ outMap .put (!(entry .getValue () instanceof Map || entry .getValue () instanceof List
2463+ || String .valueOf (entry .getKey ()).startsWith ("-" ))
2464+ ? "-" + entry .getKey () : String .valueOf (entry .getKey ()),
2465+ makeAttributeUsage (entry .getValue ()));
2466+ }
2467+ return outMap ;
2468+ }
2469+
2470+ @ SuppressWarnings ("unchecked" )
2471+ private static Object makeAttributeUsage (Object value ) {
2472+ final Object result ;
2473+ if (value instanceof List ) {
2474+ List <Object > values = newArrayList ();
2475+ for (Object item : (List ) value ) {
2476+ values .add (item instanceof Map ? forceAttributeUsage ((Map ) item ) : item );
2477+ }
2478+ result = values ;
2479+ } else if (value instanceof Map ) {
2480+ result = forceAttributeUsage ((Map ) value );
2481+ } else {
2482+ result = value ;
2483+ }
2484+ return result ;
2485+ }
2486+
24462487 public static long gcd (long value1 , long value2 ) {
24472488 if (value1 == 0 ) {
24482489 return value2 ;
0 commit comments