Skip to content

Commit e0ea1af

Browse files
committed
#1 - Improve javadoc on use of type(Object.class)
1 parent 3ded3d3 commit e0ea1af

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

blackbox-test/src/test/java/org/example/customer/CustomerTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,19 @@ private Map<String, Object> contactMap(String fn, String ln) {
9393
contactMap.put("lastName", ln);
9494
return contactMap;
9595
}
96+
97+
@Test
98+
void toJson_viaTypeObject() {
99+
100+
Object customerAsObject = new Customer().id(42L).name("rob").status(Customer.Status.ACTIVE);
101+
102+
Jsonb jsonb = Jsonb.newBuilder().build();
103+
String asJson = jsonb.type(Object.class).toJson(customerAsObject);
104+
assertThat(asJson).isEqualTo("{\"id\":42,\"name\":\"rob\",\"status\":\"ACTIVE\"}");
105+
106+
Customer from1 = jsonb.type(Customer.class).fromJson(asJson);
107+
assertThat(from1.id()).isEqualTo(42L);
108+
assertThat(from1.name()).isEqualTo("rob");
109+
assertThat(from1.status()).isEqualTo(Customer.Status.ACTIVE);
110+
}
96111
}

blackbox-test/src/test/java/org/example/customer/IntegerTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,15 @@ void array_viaTypes() {
8181
Integer[] fromJson = (Integer[]) integerArrayJson.fromJson(asJson);
8282
assertThat(fromJson).containsExactly(41, 42);
8383
}
84+
85+
@Test
86+
void toJson_viaTypeObject() {
87+
88+
Set<Integer> integers = new LinkedHashSet<>();
89+
integers.add(52);
90+
integers.add(55);
91+
92+
String asJson = jsonb.type(Object.class).toJson(integers);
93+
assertThat(asJson).isEqualTo("[52,55]");
94+
}
8495
}

jsonb/src/main/java/io/avaje/jsonb/Jsonb.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,35 @@ static Builder newBuilder() {
8383

8484
/**
8585
* Return the JsonType used to read and write json for the given class.
86+
*
87+
* <h3>Using Object.class</h3>
88+
* <p>
89+
* We can use <code>type(Object.class)</code> when we don't know the specific type that is being
90+
* written toJson or read fromJson.
91+
* <p>
92+
* When using <code>Object.class</code> and writing <code>toJson()</code> then the underlying JsonAdapter
93+
* is determined dynamically based on the type of the object value passed in.
94+
* <p>
95+
* When using <code>Object.class</code> and reading <code>fromJson()</code> then the java types used in
96+
* the result are determined dynamically based on the JSON types being read and the resulting java types
97+
* are ArrayList, LinkedHashMap, String, boolean, and double.
8698
*/
8799
<T> JsonType<T> type(Class<T> cls);
88100

89101
/**
90102
* Return the JsonType used to read and write json for the given type.
103+
*
104+
* <h3>Using Object.class</h3>
105+
* <p>
106+
* We can use <code>type(Object.class)</code> when we don't know the specific type that is being
107+
* written toJson or read fromJson.
108+
* <p>
109+
* When using <code>Object.class</code> and writing <code>toJson()</code> then the underlying JsonAdapter
110+
* is determined dynamically based on the type of the object value passed in.
111+
* <p>
112+
* When using <code>Object.class</code> and reading <code>fromJson()</code> then the java types used in
113+
* the result are determined dynamically based on the JSON types being read and the resulting java types
114+
* are ArrayList, LinkedHashMap, String, boolean, and double.
91115
*/
92116
<T> JsonType<T> type(Type type);
93117

0 commit comments

Comments
 (0)