Skip to content

Commit f024b52

Browse files
committed
Adds JSONArray toList method and JSONObject toMap method
1 parent 2657915 commit f024b52

File tree

2 files changed

+56
-13
lines changed

2 files changed

+56
-13
lines changed

JSONArray.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,9 @@ of this software and associated documentation files (the "Software"), to deal
2828
import java.io.StringWriter;
2929
import java.io.Writer;
3030
import java.lang.reflect.Array;
31-
import java.math.*;
32-
import java.util.ArrayList;
33-
import java.util.Collection;
34-
import java.util.Iterator;
35-
import java.util.Map;
31+
import java.math.BigDecimal;
32+
import java.math.BigInteger;
33+
import java.util.*;
3634

3735
/**
3836
* A JSONArray is an ordered sequence of values. Its external text form is a
@@ -1130,4 +1128,29 @@ public Writer write(Writer writer, int indentFactor, int indent)
11301128
throw new JSONException(e);
11311129
}
11321130
}
1131+
1132+
/**
1133+
* Returns a java.util.List containing all of the elements in this array.
1134+
* If an element in the array is a JSONArray or JSONObject it will also
1135+
* be converted.
1136+
* <p>
1137+
* Warning: This method assumes that the data structure is acyclical.
1138+
*
1139+
* @return a java.util.List containing the elements of this array
1140+
*/
1141+
public List<Object> toList() {
1142+
List<Object> results = new ArrayList<Object>(this.myArrayList.size());
1143+
for (Object element : this.myArrayList) {
1144+
if (element == null || JSONObject.NULL.equals(element)) {
1145+
results.add(null);
1146+
} else if (element instanceof JSONArray) {
1147+
results.add(((JSONArray) element).toList());
1148+
} else if (element instanceof JSONObject) {
1149+
results.add(((JSONObject) element).toMap());
1150+
} else {
1151+
results.add(element);
1152+
}
1153+
}
1154+
return results;
1155+
}
11331156
}

JSONObject.java

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,8 @@ of this software and associated documentation files (the "Software"), to deal
3232
import java.lang.reflect.Modifier;
3333
import java.math.BigDecimal;
3434
import java.math.BigInteger;
35-
import java.util.Collection;
36-
import java.util.Enumeration;
37-
import java.util.HashMap;
38-
import java.util.Iterator;
39-
import java.util.Locale;
40-
import java.util.Map;
35+
import java.util.*;
4136
import java.util.Map.Entry;
42-
import java.util.ResourceBundle;
43-
import java.util.Set;
4437

4538
/**
4639
* A JSONObject is an unordered collection of name/value pairs. Its external
@@ -1838,4 +1831,31 @@ public Writer write(Writer writer, int indentFactor, int indent)
18381831
throw new JSONException(exception);
18391832
}
18401833
}
1834+
1835+
/**
1836+
* Returns a java.util.Map containing all of the entrys in this object.
1837+
* If an entry in the object is a JSONArray or JSONObject it will also
1838+
* be converted.
1839+
* <p>
1840+
* Warning: This method assumes that the data structure is acyclical.
1841+
*
1842+
* @return a java.util.Map containing the entrys of this object
1843+
*/
1844+
public Map<String, Object> toMap() {
1845+
Map<String, Object> results = new HashMap<>();
1846+
for (Entry<String, Object> entry : this.map.entrySet()) {
1847+
Object value;
1848+
if (entry.getValue() == null || NULL.equals(entry.getValue())) {
1849+
value = null;
1850+
} else if (entry.getValue() instanceof JSONObject) {
1851+
value = ((JSONObject) entry.getValue()).toMap();
1852+
} else if (entry.getValue() instanceof JSONArray) {
1853+
value = ((JSONArray) entry.getValue()).toList();
1854+
} else {
1855+
value = entry.getValue();
1856+
}
1857+
results.put(entry.getKey(), value);
1858+
}
1859+
return results;
1860+
}
18411861
}

0 commit comments

Comments
 (0)