Skip to content

Commit

Permalink
allow simple custom adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
SentryMan committed Aug 3, 2024
1 parent c117e1a commit c3f217d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private void registerCustomAdapters(Set<? extends Element> elements) {
.findAny()
.ifPresentOrElse(
x -> {},
() -> logError(typeElement, "Non-Generic adapters must have a public constructor with a single Jsonb parameter"));
() -> logNote(typeElement, "Non-Generic adapters should have a public constructor with a single Jsonb parameter"));

metaData.add(type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
import io.avaje.jsonb.JsonAdapter;
import io.avaje.jsonb.JsonReader;
import io.avaje.jsonb.JsonWriter;
import io.avaje.jsonb.Jsonb;
import io.avaje.jsonb.generator.models.valid.Example3Packet.Example2Packet;

@CustomAdapter
public class CustomJsonAdapter implements JsonAdapter<Example2Packet> {

public CustomJsonAdapter(Jsonb jsonb) {}
// public CustomJsonAdapter(Jsonb jsonb) {}

@Override
public void toJson(JsonWriter writer, Example2Packet value) {}
Expand Down
6 changes: 6 additions & 0 deletions jsonb/src/main/java/io/avaje/jsonb/Jsonb.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.io.Reader;
import java.io.Writer;
import java.lang.reflect.Type;
import java.util.function.Supplier;

/**
* Provides access to json adapters by type.
Expand Down Expand Up @@ -386,6 +387,11 @@ interface Builder {
*/
<T> Builder add(Type type, JsonAdapter<T> jsonAdapter);

/**
* Add a Supplier which provides a JsonAdapter to use for the given type.
*/
<T> Builder add(Type type, Supplier<JsonAdapter<T>> builder);

/**
* Add a AdapterBuilder which provides a JsonAdapter to use for the given type.
*/
Expand Down
12 changes: 12 additions & 0 deletions jsonb/src/main/java/io/avaje/jsonb/core/DJsonb.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.lang.reflect.Type;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;

import static io.avaje.jsonb.core.Util.*;
import static java.util.Objects.requireNonNull;
Expand Down Expand Up @@ -276,6 +277,11 @@ public <T> Builder add(Type type, JsonAdapter<T> jsonAdapter) {
return add(newAdapterFactory(type, jsonAdapter));
}

@Override
public <T> Builder add(Type type, Supplier<JsonAdapter<T>> jsonAdapter) {
return add(newAdapterFactory(type, jsonAdapter));
}

@Override
public Builder add(JsonbComponent component) {
component.register(this);
Expand Down Expand Up @@ -317,6 +323,12 @@ static <T> JsonAdapter.Factory newAdapterFactory(Type type, JsonAdapter<T> jsonA
return (targetType, jsonb) -> simpleMatch(type, targetType) ? jsonAdapter : null;
}

static <T> JsonAdapter.Factory newAdapterFactory(Type type, Supplier<JsonAdapter<T>> jsonAdapter) {
requireNonNull(type);
requireNonNull(jsonAdapter);
return (targetType, jsonb) -> simpleMatch(type, targetType) ? jsonAdapter.get() : null;
}

static JsonAdapter.Factory newAdapterFactory(Type type, AdapterBuilder builder) {
requireNonNull(type);
requireNonNull(builder);
Expand Down

0 comments on commit c3f217d

Please sign in to comment.