Skip to content

Commit f92b187

Browse files
committed
* New custom ClassFactory classes are easier to write:
* See [examples](user-guide.md#classfactory-and-customwriter-examples) * `ByteBuffer` and `CharBuffer` now natively supported. * `CompactMap` supported added via `CompactMapFactory` and `CompactMapWriter`. * `Sealable*` tests moved from `java-util` to `json-io` (and so have the `Sealable*` classes).
1 parent d4bc385 commit f92b187

35 files changed

+2122
-607
lines changed

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ ___
3131
To include in your project:
3232
##### Gradle
3333
```groovy
34-
implementation 'com.cedarsoftware:json-io:4.32.0'
34+
implementation 'com.cedarsoftware:json-io:4.33.0'
3535
```
3636

3737
##### Maven
3838
```xml
3939
<dependency>
4040
<groupId>com.cedarsoftware</groupId>
4141
<artifactId>json-io</artifactId>
42-
<version>4.32.0</version>
42+
<version>4.33.0</version>
4343
</dependency>
4444
```
4545
___
@@ -57,23 +57,23 @@ ___
5757
>- [ ] **Java Package**: com.cedarsoftware.io
5858
>- [ ] **Java**: JDK17+ (Class file 61 format, includes module-info.class - multi-release JAR)
5959
>- [ ] **API**
60-
> - Static methods on [JsonIo](https://www.javadoc.io/doc/com.cedarsoftware/json-io/latest/com/cedarsoftware/io/JsonIo.html): [toJson()](https://www.javadoc.io/static/com.cedarsoftware/json-io/4.32.0/com/cedarsoftware/io/JsonIo.html#toJson(java.lang.Object,com.cedarsoftware.io.WriteOptions)), [toObjects()](https://www.javadoc.io/static/com.cedarsoftware/json-io/4.32.0/com/cedarsoftware/io/JsonIo.html#toObjects(java.lang.String,com.cedarsoftware.io.ReadOptions,java.lang.Class)), [formatJson()](https://www.javadoc.io/static/com.cedarsoftware/json-io/4.32.0/com/cedarsoftware/io/JsonIo.html#formatJson(java.lang.String,com.cedarsoftware.io.ReadOptions,com.cedarsoftware.io.WriteOptions)), [deepCopy()](https://www.javadoc.io/static/com.cedarsoftware/json-io/4.32.0/com/cedarsoftware/io/JsonIo.html#deepCopy(java.lang.Object,com.cedarsoftware.io.ReadOptions,com.cedarsoftware.io.WriteOptions))
60+
> - Static methods on [JsonIo](https://www.javadoc.io/doc/com.cedarsoftware/json-io/latest/com/cedarsoftware/io/JsonIo.html): [toJson()](https://www.javadoc.io/static/com.cedarsoftware/json-io/4.33.0/com/cedarsoftware/io/JsonIo.html#toJson(java.lang.Object,com.cedarsoftware.io.WriteOptions)), [toObjects()](https://www.javadoc.io/static/com.cedarsoftware/json-io/4.33.0/com/cedarsoftware/io/JsonIo.html#toObjects(java.lang.String,com.cedarsoftware.io.ReadOptions,java.lang.Class)), [formatJson()](https://www.javadoc.io/static/com.cedarsoftware/json-io/4.33.0/com/cedarsoftware/io/JsonIo.html#formatJson(java.lang.String,com.cedarsoftware.io.ReadOptions,com.cedarsoftware.io.WriteOptions)), [deepCopy()](https://www.javadoc.io/static/com.cedarsoftware/json-io/4.33.0/com/cedarsoftware/io/JsonIo.html#deepCopy(java.lang.Object,com.cedarsoftware.io.ReadOptions,com.cedarsoftware.io.WriteOptions))
6161
> - Use [ReadOptionsBuilder](/user-guide-readOptions.md) and [WriteOptionsBuilder](/user-guide-writeOptions.md) to configure `JsonIo`
62-
> - Use [JsonReader.ClassFactory](https://www.javadoc.io/static/com.cedarsoftware/json-io/4.32.0/com/cedarsoftware/io/JsonReader.ClassFactory.html) for difficult classes (hard to instantiate & fill)
63-
> - Use [JsonWriter.JsonClassWriter](https://www.javadoc.io/static/com.cedarsoftware/json-io/4.32.0/com/cedarsoftware/io/JsonWriter.JsonClassWriter.html) to customize the output JSON for a particular class
62+
> - Use [JsonReader.ClassFactory](https://www.javadoc.io/static/com.cedarsoftware/json-io/4.33.0/com/cedarsoftware/io/JsonReader.ClassFactory.html) for difficult classes (hard to instantiate & fill)
63+
> - Use [JsonWriter.JsonClassWriter](https://www.javadoc.io/static/com.cedarsoftware/json-io/4.33.0/com/cedarsoftware/io/JsonWriter.JsonClassWriter.html) to customize the output JSON for a particular class
6464
>- [ ] Updates will be 5.1.0, 5.2.0, ...
65-
>### 4.32.0 (current)
66-
>- [ ] **Version**: [4.32.0](https://www.javadoc.io/doc/com.cedarsoftware/json-io/4.32.0/index.html)
65+
>### 4.33.0 (current)
66+
>- [ ] **Version**: [4.33.0](https://www.javadoc.io/doc/com.cedarsoftware/json-io/4.33.0/index.html)
6767
>- [ ] **Bundling**: Both JPMS (Java Platform Module System) and OSGi (Open Service Gateway initiative)
6868
>- [ ] **Maintained**: Fully
6969
>- [ ] **Java Package**: com.cedarsoftware.io
7070
>- [ ] **Java**: JDK1.8+ (Class file 52 format, includes module-info.class - multi-release JAR)
7171
>- [ ] **API**
72-
> - Static methods on [JsonIo](https://www.javadoc.io/doc/com.cedarsoftware/json-io/latest/com/cedarsoftware/io/JsonIo.html): [toJson()](https://www.javadoc.io/static/com.cedarsoftware/json-io/4.32.0/com/cedarsoftware/io/JsonIo.html#toJson(java.lang.Object,com.cedarsoftware.io.WriteOptions)), [toObjects()](https://www.javadoc.io/static/com.cedarsoftware/json-io/4.32.0/com/cedarsoftware/io/JsonIo.html#toObjects(java.lang.String,com.cedarsoftware.io.ReadOptions,java.lang.Class)), [formatJson()](https://www.javadoc.io/static/com.cedarsoftware/json-io/4.32.0/com/cedarsoftware/io/JsonIo.html#formatJson(java.lang.String,com.cedarsoftware.io.ReadOptions,com.cedarsoftware.io.WriteOptions)), [deepCopy()](https://www.javadoc.io/static/com.cedarsoftware/json-io/4.32.0/com/cedarsoftware/io/JsonIo.html#deepCopy(java.lang.Object,com.cedarsoftware.io.ReadOptions,com.cedarsoftware.io.WriteOptions))
72+
> - Static methods on [JsonIo](https://www.javadoc.io/doc/com.cedarsoftware/json-io/latest/com/cedarsoftware/io/JsonIo.html): [toJson()](https://www.javadoc.io/static/com.cedarsoftware/json-io/4.33.0/com/cedarsoftware/io/JsonIo.html#toJson(java.lang.Object,com.cedarsoftware.io.WriteOptions)), [toObjects()](https://www.javadoc.io/static/com.cedarsoftware/json-io/4.33.0/com/cedarsoftware/io/JsonIo.html#toObjects(java.lang.String,com.cedarsoftware.io.ReadOptions,java.lang.Class)), [formatJson()](https://www.javadoc.io/static/com.cedarsoftware/json-io/4.33.0/com/cedarsoftware/io/JsonIo.html#formatJson(java.lang.String,com.cedarsoftware.io.ReadOptions,com.cedarsoftware.io.WriteOptions)), [deepCopy()](https://www.javadoc.io/static/com.cedarsoftware/json-io/4.33.0/com/cedarsoftware/io/JsonIo.html#deepCopy(java.lang.Object,com.cedarsoftware.io.ReadOptions,com.cedarsoftware.io.WriteOptions))
7373
> - Use [ReadOptionsBuilder](/user-guide-readOptions.md) and [WriteOptionsBuilder](/user-guide-writeOptions.md) to configure `JsonIo`
74-
> - Use [JsonReader.ClassFactory](https://www.javadoc.io/static/com.cedarsoftware/json-io/4.32.0/com/cedarsoftware/io/JsonReader.ClassFactory.html) for difficult classes (hard to instantiate & fill)
75-
> - Use [JsonWriter.JsonClassWriter](https://www.javadoc.io/static/com.cedarsoftware/json-io/4.32.0/com/cedarsoftware/io/JsonWriter.JsonClassWriter.html) to customize the output JSON for a particular class
76-
>- [ ] Updates will be 4.33.0, 4.34.0, ...
74+
> - Use [JsonReader.ClassFactory](https://www.javadoc.io/static/com.cedarsoftware/json-io/4.33.0/com/cedarsoftware/io/JsonReader.ClassFactory.html) for difficult classes (hard to instantiate & fill)
75+
> - Use [JsonWriter.JsonClassWriter](https://www.javadoc.io/static/com.cedarsoftware/json-io/4.33.0/com/cedarsoftware/io/JsonWriter.JsonClassWriter.html) to customize the output JSON for a particular class
76+
>- [ ] Updates will be 4.34.0, 4.35.0, ...
7777
>### 4.14.x (supported)
7878
>- [ ] **Version**: [4.14.3](https://www.javadoc.io/doc/com.cedarsoftware/json-io/4.14.3/index.html)
7979
>- [ ] **Bundling**: Both JPMS (Java Platform Module System) and OSGi (Open Service Gateway initiative)

changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
### Revision History
2+
#### 4.33.0
3+
* New custom `ClassFactory` classes are easier to write:
4+
* See [examples](user-guide.md#classfactory-and-customwriter-examples)
5+
* `ByteBuffer` and `CharBuffer` now natively supported.
6+
* `CompactMap` supported added via `CompactMapFactory` and `CompactMapWriter`.
7+
* `Sealable*` tests moved from `java-util` to `json-io` (and so have the `Sealable*` classes).
28
#### 4.32.0
39
* EnumSet can now be written with @type or @enum, controlled by a WriteOption (writeEnumSetOldWay). Currently, the default is `true,` write the old way for backward compatibility. This will change in a future release.
410
* JsonObject simplified, with `@keys` and `@items` now as explicit fields.

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<groupId>com.cedarsoftware</groupId>
66
<artifactId>json-io</artifactId>
77
<packaging>bundle</packaging>
8-
<version>4.32.0</version>
8+
<version>4.33.0</version>
99
<description>Java JSON serialization</description>
1010
<url>https://github.com/jdereg/json-io</url>
1111

@@ -27,7 +27,7 @@
2727
<!-- remove source encoding warnings from maven output -->
2828
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2929

30-
<version.java-util>3.0.0</version.java-util>
30+
<version.java-util>3.0.2</version.java-util>
3131
<!-- testing only -->
3232
<version.junit-jupiter-params>5.11.4</version.junit-jupiter-params>
3333
<version.assertj-core>3.27.2</version.assertj-core>

src/main/java/com/cedarsoftware/io/JsonIo.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,9 @@ public static WriteOptionsBuilder getWriteOptionsBuilder(Map<String, Object> opt
489489
return builder;
490490
}
491491

492+
public static String PREFIX = "-~";
493+
public static String SUFFIX = "~-";
494+
492495
//
493496
// READ Option Keys (older method of specifying options) -----------------------------------------------------------
494497
//

src/main/java/com/cedarsoftware/io/JsonObject.java

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22

33
import java.lang.reflect.Array;
44
import java.util.AbstractMap;
5-
import java.util.Arrays;
65
import java.util.Collection;
76
import java.util.IdentityHashMap;
7+
import java.util.LinkedHashMap;
88
import java.util.Map;
9+
import java.util.Objects;
910
import java.util.Set;
1011

11-
import com.cedarsoftware.util.CompactLinkedMap;
12-
1312
/**
1413
* This class holds a JSON object in a LinkedHashMap.
1514
* LinkedHashMap used to keep fields in same order as they are
@@ -34,7 +33,8 @@
3433
* limitations under the License.
3534
*/
3635
public class JsonObject extends JsonValue implements Map<Object, Object> {
37-
private final Map<Object, Object> jsonStore = new CompactLinkedMap<>();
36+
// private final Map<Object, Object> jsonStore = CompactMap.builder().compactSize(40).insertionOrder().build();
37+
private final Map<Object, Object> jsonStore = new LinkedHashMap<>();
3838
private Integer hash = null;
3939

4040
// Explicit fields for meta data
@@ -187,16 +187,59 @@ private int hashCode(Object array, Map<Object, Integer> seen) {
187187
return result;
188188
}
189189

190+
@Override
190191
public boolean equals(Object obj) {
191192
if (this == obj) return true;
192193
if (!(obj instanceof JsonObject)) return false;
193194
JsonObject other = (JsonObject) obj;
194195

195-
if (!Arrays.deepEquals(new Object[]{items}, new Object[]{other.items})) return false;
196-
if (!Arrays.deepEquals(new Object[]{keys}, new Object[]{other.keys})) return false;
196+
// Compare 'items' shallowly (element-by-element if both are arrays).
197+
if (!shallowArrayEquals(this.items, other.items)) {
198+
return false;
199+
}
200+
201+
// Compare 'keys' shallowly (element-by-element if both are arrays).
202+
if (!shallowArrayEquals(this.keys, other.keys)) {
203+
return false;
204+
}
205+
206+
// Compare the Map portion the standard way.
197207
return jsonStore.equals(other.jsonStore);
198208
}
199209

210+
/**
211+
* Compare two Objects if both are arrays, element by element,
212+
* otherwise do a simple Object.equals().
213+
*/
214+
private static boolean shallowArrayEquals(Object arr1, Object arr2) {
215+
if (arr1 == arr2) {
216+
return true; // Same reference or both null
217+
}
218+
if (arr1 == null || arr2 == null) {
219+
return false; // One is null, the other is not
220+
}
221+
222+
// If both are arrays, compare lengths and elements with .equals()
223+
if (arr1.getClass().isArray() && arr2.getClass().isArray()) {
224+
int len1 = Array.getLength(arr1);
225+
int len2 = Array.getLength(arr2);
226+
if (len1 != len2) {
227+
return false;
228+
}
229+
for (int i = 0; i < len1; i++) {
230+
Object e1 = Array.get(arr1, i);
231+
Object e2 = Array.get(arr2, i);
232+
if (!Objects.equals(e1, e2)) {
233+
return false;
234+
}
235+
}
236+
return true;
237+
}
238+
239+
// Fallback if not arrays: just do a regular equals check
240+
return Objects.equals(arr1, arr2);
241+
}
242+
200243
public boolean isEmpty() {
201244
return size() < 1;
202245
}
@@ -304,6 +347,13 @@ void rehashMaps() {
304347
for (int i = 0; i < len; i++) {
305348
Object key = Array.get(keys, i);
306349
Object value = Array.get(items, i);
350+
351+
if (key instanceof String) {
352+
String k = (String) key;
353+
if (k.startsWith(JsonIo.PREFIX) && k.endsWith(JsonIo.SUFFIX)) {
354+
continue;
355+
}
356+
}
307357
put(key, value);
308358
if (targetMap != null) {
309359
targetMap.put(key, value);

0 commit comments

Comments
 (0)