Skip to content

Commit a0cbc87

Browse files
authored
add a static way to retrieve the default instance (#396)
1 parent d8c7f80 commit a0cbc87

File tree

5 files changed

+19
-6
lines changed

5 files changed

+19
-6
lines changed

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ For types we cannot annotate with `@Json` we can place `@Json.Import(TypeToimpor
9595

9696
```java
9797
// build using defaults
98-
Jsonb jsonb = Jsonb.builder().build();
98+
Jsonb jsonb = Jsonb.instance();
9999

100100
JsonType<Customer> customerType = jsonb.type(Customer.class);
101101

@@ -116,7 +116,7 @@ to include when serializing to json.
116116
For example:
117117

118118
```java
119-
Jsonb jsonb = Jsonb.builder().build();
119+
Jsonb jsonb = Jsonb.instance();
120120

121121
JsonType<Customer> customerType = jsonb.type(Customer.class);
122122

@@ -226,7 +226,7 @@ public final class AddressJsonAdapter implements JsonAdapter<Address>, ViewBuild
226226

227227
## Based on Moshi
228228

229-
`avaje-jsonb` is based on [Moshi](https://github.com/square/moshi) with some changes as summarised below:
229+
`avaje-jsonb` was based on [Moshi](https://github.com/square/moshi) with some changes as summarised below:
230230

231231
#### Changes from Moshi
232232
- Generates Java source code (rather than Kotlin)
@@ -245,7 +245,6 @@ public final class AddressJsonAdapter implements JsonAdapter<Address>, ViewBuild
245245
- Add `@Json.Import` to generate adapters for types that we can't put the annotation on (types we might not 'own')
246246
- Add Mixin feature similar to Jackson Mixins
247247
- Add Types.listOf(), Types.setOf(), Types.mapOf() helper methods
248-
- Provide an SPI with the view to target other json-p implementations JSONP/Yasson, GSON, etc
249248
- Adds more common Java types with default built-in support - java.time types, java.util.UUID
250249
- Adds support for json views
251250

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
class ALBResponseTest {
1212

13-
private final Jsonb jsonb = Jsonb.builder().build();
13+
private final Jsonb jsonb = Jsonb.instance();
1414

1515
@Test
1616
void toJson() {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@ static Builder builder() {
107107
return DefaultBootstrap.builder();
108108
}
109109

110+
/**
111+
* Get the default Jsonb instance with all generated adapters configured.
112+
*
113+
* <p>This is a faster alternative to {@code Jsonb.builder().build()} that will return the same
114+
* singleton instance.
115+
*/
116+
static Jsonb instance() {
117+
return DefaultBootstrap.defaultInstance();
118+
}
119+
110120
/**
111121
* Return json content for the given object.
112122
* <p>

jsonb/src/main/java/io/avaje/jsonb/core/DJsonb.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ public int hashCode() {
258258
*/
259259
static final class DBuilder implements Jsonb.Builder {
260260

261-
private static final Jsonb DEFAULT = Jsonb.builder().build();
261+
static final Jsonb DEFAULT = Jsonb.builder().build();
262262

263263
private final List<AdapterFactory> factories = new ArrayList<>();
264264
private boolean failOnUnknown;

jsonb/src/main/java/io/avaje/jsonb/core/DefaultBootstrap.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,8 @@ public final class DefaultBootstrap {
1313
public static Jsonb.Builder builder() {
1414
return new DJsonb.DBuilder();
1515
}
16+
17+
public static Jsonb defaultInstance() {
18+
return DJsonb.DBuilder.DEFAULT;
19+
}
1620
}

0 commit comments

Comments
 (0)