Skip to content

Commit

Permalink
ReadOptionsBuilder.returnAsNativeJsonObjects() changed to ReadOptions…
Browse files Browse the repository at this point in the history
…Builder.returnAsJsonObjects(). Both methods kept, but returnAsNativeJsonObjects() has been deprecated.
  • Loading branch information
jdereg committed Feb 17, 2025
1 parent 66c1e4d commit e88e8b9
Show file tree
Hide file tree
Showing 36 changed files with 211 additions and 187 deletions.
16 changes: 8 additions & 8 deletions src/main/java/com/cedarsoftware/io/JsonIo.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ private JsonIo() {}
* <p>
* This method serializes the given Java source object into its JSON representation. It supports both standard Java
* objects and instances of {@code JsonObject} (e.g. a parsed Map-of-Maps obtained via a previous call to
* {@code toObjects(...)} when using {@code readOptions.returnAsNativeJsonObjects()}). The conversion respects the
* {@code toObjects(...)} when using {@code readOptions.returnAsJsonObjects()}). The conversion respects the
* configuration specified in the provided {@code WriteOptions}. If no write options are provided, the default settings
* will be applied.
* </p>
Expand Down Expand Up @@ -254,7 +254,7 @@ public static String toJson(Object srcObject, WriteOptions writeOptions) {
* <p>
* This method serializes the given Java source object into JSON and writes the output to the provided
* {@code OutputStream}. It supports both standard Java objects and {@code JsonObject} instances (which may have been
* produced earlier via {@code toObjects(...)} when using {@code readOptions.returnAsNativeJsonObjects()}). The
* produced earlier via {@code toObjects(...)} when using {@code readOptions.returnAsJsonObjects()}). The
* serialization process is governed by the supplied {@code WriteOptions}. If no options are provided, the default
* settings are used.
* </p>
Expand Down Expand Up @@ -381,7 +381,7 @@ public static void toJson(OutputStream out, Object source, WriteOptions writeOpt
* <tr>
* <td style="border: 1px solid gray;"><b>Deserialization Mode</b></td>
* <td style="border: 1px solid gray;">Either into Java DTOs or into native {@code JsonObject} maps,
* depending on {@code ReadOptions} (e.g., {@code returnAsNativeJsonObjects()}).</td>
* depending on {@code ReadOptions} (e.g., {@code returnAsJsonObjects()}).</td>
* </tr>
* <tr>
* <td style="border: 1px solid gray;"><b>Type Hint</b></td>
Expand Down Expand Up @@ -640,7 +640,7 @@ public static <T> T toObjectsGeneric(String json, ReadOptions readOptions, TypeH
* configuration in {@code ReadOptions}, the result may be:
* <ul>
* <li>A user-defined DTO (if a concrete {@code rootType} is provided).</li>
* <li>A native {@code JsonObject} (Map-of-Maps) when {@code ReadOptions.returnAsNativeJsonObjects()} is enabled.</li>
* <li>A native {@code JsonObject} (Map-of-Maps) when {@code ReadOptions.returnAsJsonObjects()} is enabled.</li>
* <li>An array or {@code Collection} if the JSON represents an array.</li>
* <li>A primitive (String, Number, Boolean, or null) if the JSON represents a JSON primitive.</li>
* </ul>
Expand Down Expand Up @@ -789,7 +789,7 @@ public static <T> T toObjectsGeneric(InputStream in, ReadOptions readOptions, Ty
* <p>
* This method converts a root {@code JsonObject} (a Map-of-Maps representation of parsed JSON) into an actual Java object
* graph. The parsed {@code JsonObject} is typically produced by calling {@code JsonIo.toObjects(String)} or
* {@code JsonIo.toObjects(InputStream)} with {@code ReadOptions.returnAsNativeJsonObjects()} enabled. This intermediate
* {@code JsonIo.toObjects(InputStream)} with {@code ReadOptions.returnAsJsonObjects()} enabled. This intermediate
* representation contains metadata (such as an {@code @type} field) that allows the correct resolution of Java objects.
* </p>
*
Expand Down Expand Up @@ -839,7 +839,7 @@ public static <T> T toObjectsGeneric(InputStream in, ReadOptions readOptions, Ty
* <li>
* <b>jsonObject</b> - The {@code JsonObject} (Map-of-Maps) that represents the parsed JSON data. This is generally
* obtained via a prior call to {@code toObjects(String)} or {@code toObjects(InputStream)} when using
* {@code ReadOptions.returnAsNativeJsonObjects()}.
* {@code ReadOptions.returnAsJsonObjects()}.
* </li>
* <li>
* <b>readOptions</b> - A {@code ReadOptions} instance that controls the resolution process. If {@code null}, default
Expand Down Expand Up @@ -935,7 +935,7 @@ public static <T> T toObjects(JsonObject jsonObject, ReadOptions readOptions, Cl
* <li>
* <b>jsonObject</b> - The native {@code JsonObject} (Map-of-Maps) representing parsed JSON data. This object is typically
* produced by a previous call to {@code toObjects(String)} or {@code toObjects(InputStream)} when using
* {@code ReadOptions.returnAsNativeJsonObjects()}.
* {@code ReadOptions.returnAsJsonObjects()}.
* </li>
* <li>
* <b>readOptions</b> - A {@code ReadOptions} instance that configures the resolution process (e.g., whether to return
Expand Down Expand Up @@ -1043,7 +1043,7 @@ public static ReadOptionsBuilder getReadOptionsBuilder(Map<String, Object> optio
boolean useMaps = com.cedarsoftware.util.Converter.convert(optionalArgs.get(USE_MAPS), boolean.class);

if (useMaps) {
builder.returnAsNativeJsonObjects();
builder.returnAsJsonObjects();
} else {
builder.returnAsJavaObjects();
}
Expand Down
15 changes: 10 additions & 5 deletions src/main/java/com/cedarsoftware/io/ObjectResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -563,8 +563,16 @@ private void markUntypedObjects(final Type type, final JsonObject rhs) {
Object[] item = stack.removeFirst();
final Type t = (Type) item[0];
final Object instance = item[1];

if (instance == null) {
continue;
}

if (t instanceof ParameterizedType) {
Class<?> clazz = TypeUtilities.getRawClass(t);
if (clazz.isArray()) {
System.out.println(clazz.getName());
}
ParameterizedType pType = (ParameterizedType) t;
Type[] typeArgs = pType.getActualTypeArguments();

Expand All @@ -586,11 +594,8 @@ private void markUntypedObjects(final Type type, final JsonObject rhs) {
Object[] array = (Object[]) instance;
for (int i = 0; i < array.length; i++) {
Object vals = array[i];
stack.addFirst(new Object[]{t, vals});

if (vals instanceof JsonObject) {
stack.addFirst(new Object[]{t, vals});
} else if (vals instanceof Object[]) {
if (vals instanceof Object[]) {
JsonObject coll = new JsonObject();
coll.setType(clazz);
coll.setItems(vals);
Expand All @@ -611,7 +616,7 @@ private void markUntypedObjects(final Type type, final JsonObject rhs) {
final Object array = jObj.getItems();
if (array != null) {
int len = Array.getLength(array);
for (int i=0; i < len; i++) {
for (int i = 0; i < len; i++) {
Object o = Array.get(array, i);
stack.addFirst(new Object[]{typeArgs[0], o});
}
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/cedarsoftware/io/ReadOptionsBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -439,12 +439,23 @@ public ReadOptionsBuilder addClassFactory(Class<?> clazz, JsonReader.ClassFactor
/**
* Set to return as JSON_OBJECTS's (faster, useful for large, more simple object data sets). This returns as
* native json types (Map, Object[], long, double, boolean)
* @deprecated use {@link ReadOptionsBuilder#returnAsJsonObjects()} instead.
*/
@Deprecated
public ReadOptionsBuilder returnAsNativeJsonObjects() {
options.returnType = ReadOptions.ReturnType.JSON_OBJECTS;
return this;
}

/**
* Set to return as JSON_OBJECTS's (faster, useful for large, more simple object data sets). This returns as
* native json types (Map, Object[], long, double, boolean)
*/
public ReadOptionsBuilder returnAsJsonObjects() {
options.returnType = ReadOptions.ReturnType.JSON_OBJECTS;
return this;
}

/**
* Return as JAVA_OBJECT's the returned value will be of the class type passed into JsonReader.toJava(json, rootClass).
* This mode is good for cloning exact objects.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/cedarsoftware/io/Resolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public Converter getConverter() {
* <p>
* This method converts a root-level {@code JsonObject}—a Map-of-Maps representation of parsed JSON—into an actual
* Java object instance. The {@code JsonObject} is typically produced by a prior call to {@code JsonIo.toObjects(String)}
* or {@code JsonIo.toObjects(InputStream)} when using the {@code ReadOptions.returnAsNativeJsonObjects()} setting.
* or {@code JsonIo.toObjects(InputStream)} when using the {@code ReadOptions.returnAsJsonObjects()} setting.
* The conversion process uses the provided <code>root</code> parameter, a {@link java.lang.reflect.Type} that represents
* the expected root type (including any generic type parameters). Although the full type information is preserved for
* resolution, the {@code JsonObject}'s legacy {@code hintType} field (which remains a {@code Class<?>}) is set using the
Expand Down
18 changes: 9 additions & 9 deletions src/test/java/com/cedarsoftware/io/ArrayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ void testReconstituteObjectArray()
Object[] two = new Object[]{objs, "bella", objs};
String json0 = TestUtil.toJson(two);

Object[] array = TestUtil.toObjects(json0, new ReadOptionsBuilder().returnAsNativeJsonObjects().build(), null);
Object[] array = TestUtil.toObjects(json0, new ReadOptionsBuilder().returnAsJsonObjects().build(), null);
String json1 = TestUtil.toJson(array);

// Read back into typed Java objects, the Maps of Maps versus what was dumped out
Expand Down Expand Up @@ -491,7 +491,7 @@ void testReconstituteObjectArray()
json0 = TestUtil.toJson(ta);
TestUtil.printLine("json0=" + json0);

Map map = TestUtil.toObjects(json0, new ReadOptionsBuilder().returnAsNativeJsonObjects().build(), null);
Map map = TestUtil.toObjects(json0, new ReadOptionsBuilder().returnAsJsonObjects().build(), null);
json1 = TestUtil.toJson(map);
TestUtil.printLine("json1=" + json1);

Expand Down Expand Up @@ -609,7 +609,7 @@ private void testReconstituteArrayHelper(Object foo)
String json0 = TestUtil.toJson(foo);
TestUtil.printLine("json0=" + json0);

Object array = TestUtil.toObjects(json0, new ReadOptionsBuilder().returnAsNativeJsonObjects().build(), null);
Object array = TestUtil.toObjects(json0, new ReadOptionsBuilder().returnAsJsonObjects().build(), null);
String json1 = TestUtil.toJson(array);
TestUtil.printLine("json1=" + json1);
assertEquals(json0, json1);
Expand All @@ -622,7 +622,7 @@ void testReconstituteEmptyArray()
String json0 = TestUtil.toJson(empty);
TestUtil.printLine("json0=" + json0);

empty = TestUtil.toObjects(json0, new ReadOptionsBuilder().returnAsNativeJsonObjects().build(), null);
empty = TestUtil.toObjects(json0, new ReadOptionsBuilder().returnAsJsonObjects().build(), null);
assertNotNull(empty);
assertNotNull(empty);
assertEquals(0, empty.length);
Expand All @@ -635,7 +635,7 @@ void testReconstituteEmptyArray()
json0 = TestUtil.toJson(list);
TestUtil.printLine("json0=" + json0);

list = TestUtil.toObjects(json0, new ReadOptionsBuilder().returnAsNativeJsonObjects().build(), null);
list = TestUtil.toObjects(json0, new ReadOptionsBuilder().returnAsJsonObjects().build(), null);
assertNotNull(list);
assertEquals(2, list.length);
JsonObject e1 = (JsonObject) list[0];
Expand All @@ -655,7 +655,7 @@ void testReconstituteTypedArray()
Object[] objs = new Object[]{strs, "a", strs};
String json0 = TestUtil.toJson(objs);
TestUtil.printLine("json0=" + json0);
Object array = TestUtil.toObjects(json0, new ReadOptionsBuilder().returnAsNativeJsonObjects().build(), null);
Object array = TestUtil.toObjects(json0, new ReadOptionsBuilder().returnAsJsonObjects().build(), null);
String json1 = TestUtil.toJson(array);
TestUtil.printLine("json1=" + json1);

Expand All @@ -681,7 +681,7 @@ void testReconstituteArray()
testArray.init();
String json0 = TestUtil.toJson(testArray);
TestUtil.printLine("json0=" + json0);
Map testArray2 = TestUtil.toObjects(json0, new ReadOptionsBuilder().returnAsNativeJsonObjects().build(), null);
Map testArray2 = TestUtil.toObjects(json0, new ReadOptionsBuilder().returnAsJsonObjects().build(), null);

String json1 = TestUtil.toJson(testArray2);
TestUtil.printLine("json1=" + json1);
Expand All @@ -698,7 +698,7 @@ void testReconstituteEmptyObject()
String json0 = TestUtil.toJson(empty);
TestUtil.printLine("json0=" + json0);

Map m = TestUtil.toObjects(json0, new ReadOptionsBuilder().returnAsNativeJsonObjects().build(), null);
Map m = TestUtil.toObjects(json0, new ReadOptionsBuilder().returnAsJsonObjects().build(), null);
assertTrue(m.isEmpty());

String json1 = TestUtil.toJson(m);
Expand Down Expand Up @@ -1038,7 +1038,7 @@ void testArrayWithCircularReference()
String json0 = TestUtil.toJson(ta, new WriteOptionsBuilder().build());

// First convert to Maps
Object objAsMap = TestUtil.toObjects(json0, new ReadOptionsBuilder().returnAsNativeJsonObjects().build(), null);
Object objAsMap = TestUtil.toObjects(json0, new ReadOptionsBuilder().returnAsJsonObjects().build(), null);
String json1 = TestUtil.toJson(objAsMap);

// Then convert back to objects
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/cedarsoftware/io/AtomicBooleanTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void testAssignAtomicBoolean()
void testAssignAtomicBooleanStringToMaps()
{
String json = "{\"@type\":\"" + TestAtomicBooleanField.class.getName() + "\",\"strValue\":\"\"}";
Map map = TestUtil.toObjects(json, new ReadOptionsBuilder().returnAsNativeJsonObjects().build(), null);
Map map = TestUtil.toObjects(json, new ReadOptionsBuilder().returnAsJsonObjects().build(), null);
assertNull(map.get("fromString")); // allowing "" to null out non-primitive fields in map-of-map mode
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/cedarsoftware/io/AtomicIntegerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void testAssignAtomicInteger()
void testAssignAtomicIntegerStringToMaps()
{
String json = "{\"@type\":\"" + TestAtomicIntegerField.class.getName() + "\",\"strValue\":\"\"}";
Map map = TestUtil.toObjects(json, new ReadOptionsBuilder().returnAsNativeJsonObjects().build(), null);
Map map = TestUtil.toObjects(json, new ReadOptionsBuilder().returnAsJsonObjects().build(), null);
assertNull(map.get("fromString")); // allowing "" to null out non-primitive fields in map-of-map mode
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/cedarsoftware/io/AtomicLongTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void testAssignAtomicLong()
void testAssignAtomicLongStringToMaps()
{
String json = "{\"@type\":\"" + TestAtomicLongField.class.getName() + "\",\"strValue\":\"\"}";
Map map = TestUtil.toObjects(json, new ReadOptionsBuilder().returnAsNativeJsonObjects().build(), null);
Map map = TestUtil.toObjects(json, new ReadOptionsBuilder().returnAsJsonObjects().build(), null);
assertNull(map.get("fromString")); // allowing "" to null out non-primitive fields in map-of-map mode
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/cedarsoftware/io/BigDecimalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void testAssignBigDecimal()
assertEquals(new BigDecimal(1), tbd.values[7]);
assertEquals(new BigDecimal("72.72"), tbd.values[8]);

Map map = TestUtil.toObjects(json, new ReadOptionsBuilder().returnAsNativeJsonObjects().build(), null);
Map map = TestUtil.toObjects(json, new ReadOptionsBuilder().returnAsJsonObjects().build(), null);
json = TestUtil.toJson(map);
tbd = TestUtil.toObjects(json, null);
assertEquals(new BigDecimal("3.14159"), tbd.fromString);
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/cedarsoftware/io/BigIntegerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void testAssignBigInteger()
assertEquals(new BigInteger("1"), tbi.values[5]);
assertEquals(new BigInteger("999"), tbi.values[6]);

Map map = TestUtil.toObjects(json, new ReadOptionsBuilder().returnAsNativeJsonObjects().build(), null);
Map map = TestUtil.toObjects(json, new ReadOptionsBuilder().returnAsJsonObjects().build(), null);
json = TestUtil.toJson(map);
tbi = TestUtil.toObjects(json, null);
assertEquals(new BigInteger("314159"), tbi.fromString);
Expand Down Expand Up @@ -99,7 +99,7 @@ void testAssignBigInteger()
void testAssignBigIntegerStringToMaps()
{
String json = "{\"@type\":\"" + TestBigIntegerField.class.getName() + "\",\"fromString\":\"\"}";
Map map = TestUtil.toObjects(json, new ReadOptionsBuilder().returnAsNativeJsonObjects().build(), null);
Map map = TestUtil.toObjects(json, new ReadOptionsBuilder().returnAsJsonObjects().build(), null);
assert map.get("fromString") instanceof BigInteger;
BigInteger bigInt = (BigInteger) map.get("fromString");
assert bigInt.longValue() == 0;
Expand Down
34 changes: 21 additions & 13 deletions src/test/java/com/cedarsoftware/io/BigJsonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.Map;

import com.cedarsoftware.util.ClassUtilities;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand All @@ -28,25 +28,33 @@
*/
class BigJsonTest
{
@RepeatedTest(2)
@Test
void testBigJsonToJava()
{
ReadOptions readOptions = new ReadOptionsBuilder().returnAsJavaObjects().build();
String json = ClassUtilities.loadResourceAsString("big/big5D.json");
Map map = TestUtil.toObjects(json, null);
assertEquals("big5D", map.get("ncube"));
assertEquals(0L, map.get("defaultCellValue"));
assertNotNull(map.get("axes"));
assertNotNull(map.get("cells"));

for (int i=0; i < 1; i++) {
Map map = TestUtil.toObjects(json, readOptions,null);
assertEquals("big5D", map.get("ncube"));
assertEquals(0L, map.get("defaultCellValue"));
assertNotNull(map.get("axes"));
assertNotNull(map.get("cells"));
}
}

@RepeatedTest(2)
@Test
void testBigJsonToMaps()
{
ReadOptions readOptions = new ReadOptionsBuilder().returnAsJsonObjects().build();
String json = ClassUtilities.loadResourceAsString("big/big5D.json");
Map map = TestUtil.toObjects(json, new ReadOptionsBuilder().returnAsNativeJsonObjects().build(), null);
assertEquals("big5D", map.get("ncube"));
assertEquals(0L, map.get("defaultCellValue"));
assertNotNull(map.get("axes"));
assertNotNull(map.get("cells"));

for (int i=0; i < 1; i++) {
Map map = TestUtil.toObjects(json, readOptions, null);
assertEquals("big5D", map.get("ncube"));
assertEquals(0L, map.get("defaultCellValue"));
assertNotNull(map.get("axes"));
assertNotNull(map.get("cells"));
}
}
}
2 changes: 1 addition & 1 deletion src/test/java/com/cedarsoftware/io/CalendarTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ void testCalendarInObjectArray()
String json = TestUtil.toJson(new Object[]{now});
TestUtil.printLine("json=" + json);

Object[] items = TestUtil.toObjects(json, new ReadOptionsBuilder().returnAsNativeJsonObjects().build(), null);
Object[] items = TestUtil.toObjects(json, new ReadOptionsBuilder().returnAsJsonObjects().build(), null);
Calendar item = (Calendar) items[0];
assertEquals(item, now);
}
Expand Down
Loading

0 comments on commit e88e8b9

Please sign in to comment.