59
59
import java .util .Map ;
60
60
import java .util .TreeMap ;
61
61
62
+ import static org .kordamp .json .util .JSONUtils .isBlank ;
63
+
62
64
/**
63
65
* Utility class for transforming JSON to XML an back.<br>
64
66
* When transforming JSONObject and JSONArray instances to XML, this class will
@@ -560,9 +562,7 @@ public void setForcedArrayElements(Collection<String> forcedArrayElements) {
560
562
* Creates a JSON value from a XML string.
561
563
*
562
564
* @param xml A well-formed xml document in a String
563
- *
564
565
* @return a JSONNull, JSONObject or JSONArray
565
- *
566
566
* @throws JSONException if the conversion from XML to JSON can't be made for
567
567
* I/O or format reasons.
568
568
*/
@@ -600,9 +600,7 @@ public JSON read(String xml) {
600
600
* Creates a JSON value from a File.
601
601
*
602
602
* @param file
603
- *
604
603
* @return a JSONNull, JSONObject or JSONArray
605
- *
606
604
* @throws JSONException if the conversion from XML to JSON can't be made for
607
605
* I/O or format reasons.
608
606
*/
@@ -627,9 +625,7 @@ public JSON readFromFile(File file) {
627
625
* Creates a JSON value from a File.
628
626
*
629
627
* @param path
630
- *
631
628
* @return a JSONNull, JSONObject or JSONArray
632
- *
633
629
* @throws JSONException if the conversion from XML to JSON can't be made for
634
630
* I/O or format reasons.
635
631
*/
@@ -643,9 +639,7 @@ public JSON readFromFile(String path) {
643
639
* Creates a JSON value from an input stream.
644
640
*
645
641
* @param stream
646
- *
647
642
* @return a JSONNull, JSONObject or JSONArray
648
- *
649
643
* @throws JSONException if the conversion from XML to JSON can't be made for
650
644
* I/O or format reasons.
651
645
*/
@@ -767,9 +761,7 @@ public void setKeepArrayName(boolean keepName) {
767
761
* Writes a JSON value into a XML string with UTF-8 encoding.<br>
768
762
*
769
763
* @param json The JSON value to transform
770
- *
771
764
* @return a String representation of a well-formed xml document.
772
- *
773
765
* @throws JSONException if the conversion from JSON to XML can't be made for
774
766
* I/O reasons.
775
767
*/
@@ -783,9 +775,7 @@ public String write(JSON json) {
783
775
*
784
776
* @param json The JSON value to transform
785
777
* @param encoding The xml encoding to use
786
- *
787
778
* @return a String representation of a well-formed xml document.
788
- *
789
779
* @throws JSONException if the conversion from JSON to XML can't be made for
790
780
* I/O reasons or the encoding is not supported.
791
781
*/
@@ -1193,7 +1183,9 @@ private Element processJSONObject(JSONObject jsonObject, Element root,
1193
1183
1194
1184
Object [] names = jsonObject .names ().toArray ();
1195
1185
List <String > unprocessed = new ArrayList <>();
1196
- if (isSortPropertyNames ()) { Arrays .sort (names ); }
1186
+ if (isSortPropertyNames ()) {
1187
+ Arrays .sort (names );
1188
+ }
1197
1189
for (Object o : names ) {
1198
1190
String name = (String ) o ;
1199
1191
Object value = jsonObject .get (name );
@@ -1275,7 +1267,6 @@ private Element processJSONObject(JSONObject jsonObject, Element root,
1275
1267
* Only perform auto expansion if all children are objects.
1276
1268
*
1277
1269
* @param array The array to check
1278
- *
1279
1270
* @return True if all children are objects, false otherwise.
1280
1271
*/
1281
1272
private boolean canAutoExpand (JSONArray array ) {
@@ -1557,7 +1548,11 @@ private void setValue(JSONObject jsonObject, Element element, String defaultType
1557
1548
params = StringUtils .split (paramsAttribute .getValue (), "," );
1558
1549
setOrAccumulate (jsonObject , key , new JSONFunction (params , text ));
1559
1550
} else {
1560
- if (isArray (element , false )) {
1551
+ Attribute typeAttr = element .getAttribute (addJsonPrefix ("type" ));
1552
+ if (typeAttr != null && isBlank (element .getValue ()) &&
1553
+ element .getChildCount () == 0 && element .getChildElements ().size () == 0 ) {
1554
+ setOrAccumulate (jsonObject , key , "" );
1555
+ } else if (isArray (element , false )) {
1561
1556
setOrAccumulate (jsonObject , key , processArrayElement (element , defaultType ));
1562
1557
} else if (isObject (element , false )) {
1563
1558
setOrAccumulate (jsonObject , key , simplifyValue (jsonObject ,
0 commit comments