1
1
package io .avaje .jsonb .core ;
2
2
3
- import io .avaje .json .JsonAdapter ;
4
- import io .avaje .json .JsonReader ;
5
- import io .avaje .json .JsonWriter ;
6
- import io .avaje .json .PropertyNames ;
7
- import io .avaje .json .stream .*;
8
- import io .avaje .jsonb .*;
9
- import io .avaje .jsonb .AdapterFactory ;
10
- import io .avaje .jsonb .spi .*;
3
+ import static io .avaje .json .stream .BufferRecycleStrategy .HYBRID_POOL ;
4
+ import static io .avaje .jsonb .core .Util .canonicalize ;
5
+ import static io .avaje .jsonb .core .Util .canonicalizeClass ;
6
+ import static io .avaje .jsonb .core .Util .removeSubtypeWildcard ;
7
+ import static java .util .Objects .requireNonNull ;
11
8
12
9
import java .io .InputStream ;
13
10
import java .io .OutputStream ;
14
11
import java .io .Reader ;
15
12
import java .io .Writer ;
16
13
import java .lang .reflect .Type ;
17
- import java .util .*;
14
+ import java .util .ArrayList ;
15
+ import java .util .List ;
16
+ import java .util .Map ;
17
+ import java .util .Objects ;
18
18
import java .util .concurrent .ConcurrentHashMap ;
19
19
import java .util .function .Supplier ;
20
20
21
- import static io .avaje .jsonb .core .Util .*;
22
- import static java .util .Objects .requireNonNull ;
21
+ import io .avaje .json .JsonAdapter ;
22
+ import io .avaje .json .JsonReader ;
23
+ import io .avaje .json .JsonWriter ;
24
+ import io .avaje .json .PropertyNames ;
25
+ import io .avaje .json .stream .BufferRecycleStrategy ;
26
+ import io .avaje .json .stream .BufferedJsonWriter ;
27
+ import io .avaje .json .stream .BytesJsonWriter ;
28
+ import io .avaje .json .stream .JsonOutput ;
29
+ import io .avaje .json .stream .JsonStream ;
30
+ import io .avaje .jsonb .AdapterFactory ;
31
+ import io .avaje .jsonb .JsonType ;
32
+ import io .avaje .jsonb .JsonView ;
33
+ import io .avaje .jsonb .Jsonb ;
34
+ import io .avaje .jsonb .spi .GeneratedComponent ;
35
+ import io .avaje .jsonb .spi .JsonbComponent ;
23
36
24
37
/**
25
38
* Default implementation of Jsonb.
@@ -245,6 +258,8 @@ public int hashCode() {
245
258
*/
246
259
static final class DBuilder implements Jsonb .Builder {
247
260
261
+ private static final Jsonb DEFAULT = Jsonb .builder ().build ();
262
+
248
263
private final List <AdapterFactory > factories = new ArrayList <>();
249
264
private boolean failOnUnknown ;
250
265
private boolean failOnNullPrimitives ;
@@ -253,7 +268,7 @@ static final class DBuilder implements Jsonb.Builder {
253
268
private boolean serializeNulls ;
254
269
private boolean serializeEmpty = true ;
255
270
private JsonStream adapter ;
256
- private BufferRecycleStrategy strategy = BufferRecycleStrategy . HYBRID_POOL ;
271
+ private BufferRecycleStrategy strategy = HYBRID_POOL ;
257
272
258
273
@ Override
259
274
public Builder serializeNulls (boolean serializeNulls ) {
@@ -341,12 +356,28 @@ private void registerComponents() {
341
356
}
342
357
343
358
@ Override
344
- public DJsonb build () {
359
+ public Jsonb build () {
360
+ if (!hasCustomizations ()) {
361
+ return DEFAULT ;
362
+ }
345
363
registerComponents ();
346
364
return new DJsonb (adapter , factories , serializeNulls , serializeEmpty , failOnUnknown , failOnNullPrimitives , mathTypesAsString , calendarAsString , strategy );
347
365
}
348
366
349
- static <T > AdapterFactory newAdapterFactory (Type type , JsonAdapter <T > jsonAdapter ) {
367
+ private boolean hasCustomizations () {
368
+ return DEFAULT == null
369
+ || adapter != null
370
+ || !factories .isEmpty ()
371
+ || failOnUnknown
372
+ || failOnNullPrimitives
373
+ || mathTypesAsString
374
+ || calendarAsString
375
+ || serializeNulls
376
+ || !serializeEmpty
377
+ || HYBRID_POOL != strategy ;
378
+ }
379
+
380
+ static <T > AdapterFactory newAdapterFactory (Type type , JsonAdapter <T > jsonAdapter ) {
350
381
requireNonNull (type );
351
382
requireNonNull (jsonAdapter );
352
383
return (targetType , jsonb ) -> simpleMatch (type , targetType ) ? jsonAdapter : null ;
0 commit comments