Skip to content

Commit 342b31b

Browse files
committed
3.0.3
1 parent 84b6542 commit 342b31b

File tree

6 files changed

+21
-16
lines changed

6 files changed

+21
-16
lines changed

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ Java SE 7+/Android RFC 8259 compliant package
1010
\- “Json” means the Java representation of a JSON object.
1111

1212

13-
The class [JSON](#JSON) contains static methods for parsing/generating text in JSON format. Parser converts JSON to <a name="native"></a> **package-native** objects: **[Json object](#Json)** ( the Java representation of a JSON object )**, String, Number, Boolean, null, Object[ ]** - an array of the listed types. The JSON generator accept any Java object, all Java primitives and their arrays.
13+
The class [JSON](#JSON) contains static methods for parsing/generating text in JSON format. Parser converts JSON to <a id="native"></a> **package native** objects: **[Json](#Json_object)** ( the Java representation of a JSON object )**, String, Number, Boolean, null, Object[ ]** - an array of the listed types. The JSON generator accept any Java object, all Java primitives and their arrays.
1414

1515
Instances of Java classes can be converted to Json object (usually empty) using [Json.converter](#Converter).
1616

17-
The [JsonObject](#JsonObject) abstract class and [JsonConvertible](#JsonConvertible) interface use the JavaScript-like replacer/reviver tool to convert object instance fields to or from [Json object](#Json) members.
17+
The [JsonObject](#JsonObject) abstract class and [JsonConvertible](#JsonConvertible) interface use the JavaScript-like replacer/reviver tool to convert object instance fields to or from package native objects.
1818

1919

2020
#### package org.miktim.json
@@ -89,7 +89,7 @@ System.out.println(Arrays.deepToString(dbls));
8989
*/
9090
```
9191

92-
<a name="Json"></a>
92+
<a id="Json_object"></a>
9393
#### class Json extends HashMap &lt;String, Object\>
9494
This class is a Java representation of a JSON object.
9595
Json member types: **Json object, String, Number, Boolean, null, Object[ ]** array of listed types.
@@ -235,7 +235,7 @@ System.out.printf("%d %s %s\n\r", personId, firstName, homePhone);
235235
1234 John 123-4567
236236
*/
237237
```
238-
<a name="Converter"></a>
238+
<a id="Converter"></a>
239239
#### Json.converter
240240
Used to convert existing instances of Java objects to/from a Json object.
241241
Only the visible (context depended!) fields are converted. The converter ignores the final and transient fields.
@@ -278,12 +278,12 @@ Loads Json to target object. Returns target object.
278278
}
279279
```
280280

281-
<a name="JsonConvertible"></a>
281+
<a id="JsonConvertible"></a>
282282
#### interface JsonConvertible
283-
The JsonConvertible interface provdes JavaScript-like methods for conversion fields of a Java object to/from a package [native](#native) objects. Notes:
283+
The JsonConvertible interface provdes JavaScript-like methods for conversion fields of a Java object to/from a [package native](#native) objects. Notes:
284284
\- visibility of object fields as from the object constructor (including the privates);
285285
\- Java transient and final fields are ignored;
286-
\- it is strongly recommended to initialize the accessible fields and create a public default constructor;
286+
\- it is recommended to initialize the accessible fields and create a public default constructor;
287287
\- non-native, non Json convertible fields MUST be managed using replacer/reviver.
288288

289289
<p style="background-color: #B0C4DE;">
@@ -315,7 +315,7 @@ Applies on loading from Json object:
315315

316316
```java
317317
/*
318-
* Creating a JsonConvertible object from a HashMap
318+
* Creating a Json convertible HashMap
319319
*/
320320
public class NamesOfNumbers extends HashMap<Double, String>
321321
implements JsonConvertible {
@@ -392,12 +392,12 @@ one and five tenths
392392
```
393393

394394

395-
<a name="JsonObject"></a>
395+
<a id="JsonObject"></a>
396396
#### abstract class JsonObject implements JsonConvertible
397-
Java object extender. Unload/load fields of a Java object instance to/from a [native](#native) objects. Notes:
397+
Java object extender. Unload/load fields of a Java object instance to/from a [package native](#native) objects. Notes:
398398
\- visibility of object fields as from the object constructor (including the privates);
399399
\- Java transient and final fields are ignored;
400-
\- it is highly recommended to initialize accessible fields and create a public default constructor;
400+
\- it is recommended to initialize accessible fields and create a public default constructor;
401401
\- non-native, non Json convertible fields MUST be managed using replacer/reviver.
402402

403403
<p style="background-color: #B0C4DE;">

dist/json-3.0.3-rc0.jar

23.5 KB
Binary file not shown.

src/org/miktim/json/JSONGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ void generateObject(Object value, int level) throws IOException {
8686
return;
8787
}
8888
try {
89-
generateObject(Json.converter.toJson(value), level);
89+
generateObject(Json.converter.toJson(value), level - 1);
9090
} catch (Exception ex) {
9191
throw new ClassCastException(ex.getMessage());
9292
}

src/org/miktim/json/JsonConverter.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,10 @@ protected <T> T load(JsonConvertible thisObj, T targetObj, Object json) //{
8888
}
8989
// TODO: !? field value == null
9090
Class fieldCls = field.getType();
91-
if (!(JSON.isNativeClass(fieldCls) || value.getClass().isArray())) {
91+
if (!(JSON.isNativeClass(fieldCls)
92+
|| newValue.getClass().equals(fieldCls)
93+
|| fieldCls.isArray())) {
94+
// if (!(JSON.isNativeClass(fieldCls) || value.getClass().isArray())) {
9295
// if (!(JSON.isNativeType(value) || value.getClass().isArray())) {
9396
newValue = Json.converter.fromJson(value, newValue);
9497
}

test/json/Example4.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
import java.io.FileInputStream;
77
import java.io.IOException;
88
import java.text.ParseException;
9+
import java.util.ArrayList;
910
import java.util.Arrays;
1011
import java.util.HashSet;
11-
import java.util.List;
1212
import org.miktim.json.JSON;
1313
import org.miktim.json.Json;
1414
import org.miktim.json.JsonObject;
@@ -42,8 +42,9 @@ public Object replacer(String name, Object value) {
4242
public Object reviver(String name, Object value) {
4343
if (name.endsWith(":friends")) {
4444
// load friends Set, first create collection from array
45-
List<Integer> list = Arrays.asList(JSON.cast(Integer[].class, value));
46-
return new HashSet<>(list);
45+
@SuppressWarnings("unchecked")
46+
ArrayList<Integer> list= new ArrayList(Arrays.asList(JSON.cast(Integer[].class, value)));
47+
return new HashSet<Integer>(list);
4748
}
4849
return value;
4950
}

test/json/JsonObjectTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ public static void main(String[] args) throws Exception {
217217
a.fromJson(j);
218218
log(a);
219219
if (!j.toString().equals(a.toString())) {
220+
log(j);
220221
log("Failed! ");
221222
return;
222223
}

0 commit comments

Comments
 (0)