Skip to content

Commit d30ecad

Browse files
author
John J. Aylward
committed
Update README for best practices when using putAll on JSONArray
1 parent f35194b commit d30ecad

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,45 @@ Some notable exceptions that the JSON Parser in this library accepts are:
118118
* Unescaped literals like "tab" in string values `{ "key": "value with an unescaped tab" }`
119119
* Numbers out of range for `Double` or `Long` are parsed as strings
120120

121+
Recent pull requests added a new method `putAll` on the JSONArray. The `putAll` method
122+
works similarly as other `put` mehtods in that it does not call `JSONObject.wrap` for items
123+
added. This can lead to inconsistent object representation in JSONArray structures.
124+
125+
For example, code like this will create a mixed JSONArray, some items wrapped, others
126+
not:
127+
128+
```java
129+
SomeBean[] myArr = new SomeBean[]{ new SomeBean(1), new SomeBean(2) };
130+
// these will be wrapped
131+
JSONArray jArr = new JSONArray(myArr);
132+
// these will not be wrapped
133+
jArr.putAll(new SomeBean[]{ new SomeBean(3), new SomeBean(4) });
134+
```
135+
136+
For structure consistency, it would be recommended that the above code is changed
137+
to look like 1 of 2 ways.
138+
139+
Option 1:
140+
```Java
141+
SomeBean[] myArr = new SomeBean[]{ new SomeBean(1), new SomeBean(2) };
142+
JSONArray jArr = new JSONArray();
143+
// these will not be wrapped
144+
jArr.putAll(myArr);
145+
// these will not be wrapped
146+
jArr.putAll(new SomeBean[]{ new SomeBean(3), new SomeBean(4) });
147+
// our jArr is now consistent.
148+
```
149+
150+
Option 2:
151+
```Java
152+
SomeBean[] myArr = new SomeBean[]{ new SomeBean(1), new SomeBean(2) };
153+
// these will be wrapped
154+
JSONArray jArr = new JSONArray(myArr);
155+
// these will be wrapped
156+
jArr.putAll(new JSONArray(new SomeBean[]{ new SomeBean(3), new SomeBean(4) }));
157+
// our jArr is now consistent.
158+
```
159+
121160
**Unit Test Conventions**
122161

123162
Test filenames should consist of the name of the module being tested, with the suffix "Test".

0 commit comments

Comments
 (0)