Skip to content

Commit

Permalink
Merge pull request #106 from SentryMan/interface-adapter
Browse files Browse the repository at this point in the history
Make JsonAdapter an Interface
  • Loading branch information
rbygrave committed May 8, 2023
2 parents 8168ca3 + fe6b1db commit c375aef
Show file tree
Hide file tree
Showing 20 changed files with 59 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.example.customer;

import io.avaje.jsonb.Json;

@Json
public record ErrorResponse(String id, String text) {}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public void register(Jsonb.Builder builder) {
builder.add(MyCustomScalarType.class, new CustomTypeAdapterWithStar().nullSafe());
}

static class CustomTypeAdapterWithStar extends JsonAdapter<MyCustomScalarType> {
static class CustomTypeAdapterWithStar implements JsonAdapter<MyCustomScalarType> {

@Override
public void toJson(JsonWriter writer, MyCustomScalarType value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void toJson_fromJson() {
assertThat(wrapper1.custom()).isEqualTo(wrapper.custom());
}

static class CustomTypeAdapter extends JsonAdapter<MyCustomScalarType> {
static class CustomTypeAdapter implements JsonAdapter<MyCustomScalarType> {

@Override
public void toJson(JsonWriter writer, MyCustomScalarType value) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package io.avaje.jsonb.generator;

import static io.avaje.jsonb.generator.ProcessingContext.*;
import javax.tools.JavaFileObject;
import static io.avaje.jsonb.generator.ProcessingContext.createWriter;

import java.io.IOException;
import java.io.Writer;

import javax.tools.JavaFileObject;

final class SimpleAdapterWriter {

private final BeanReader beanReader;
Expand All @@ -17,7 +19,7 @@ final class SimpleAdapterWriter {

SimpleAdapterWriter(BeanReader beanReader) {
this.beanReader = beanReader;
AdapterName adapterName = new AdapterName(beanReader.getBeanType());
final AdapterName adapterName = new AdapterName(beanReader.getBeanType());
this.adapterShortName = adapterName.shortName();
this.adapterPackage = adapterName.adapterPackage();
this.adapterFullName = adapterName.fullName();
Expand All @@ -29,7 +31,7 @@ String fullName() {
}

private Writer createFileWriter() throws IOException {
JavaFileObject jfo = createWriter(adapterFullName);
final JavaFileObject jfo = createWriter(adapterFullName);
return jfo.openWriter();
}

Expand All @@ -53,7 +55,7 @@ void write() throws IOException {
private void writeFactory() {
if (genericParamsCount > 0) {
String typeName = adapterShortName;
int nestedIndex = adapterShortName.indexOf("$");
final int nestedIndex = adapterShortName.indexOf("$");
if (nestedIndex != -1) {
typeName = typeName.substring(nestedIndex + 1);
}
Expand Down Expand Up @@ -107,9 +109,9 @@ private void writeClassEnd() {

private void writeClassStart() {
writer.append("@Generated").eol();
writer.append("public final class %sJsonAdapter extends JsonAdapter<%s> ", adapterShortName, beanReader.shortName());
writer.append("public final class %sJsonAdapter implements JsonAdapter<%s> ", adapterShortName, beanReader.shortName());
if (!beanReader.hasSubtypes()) {
writer.append("implements ViewBuilderAware ");
writer.append(", ViewBuilderAware ");
}
writer.append("{").eol().eol();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import java.lang.invoke.MethodHandle;

public class AddressJsonAdapter extends JsonAdapter<Address> implements ViewBuilderAware {
public class AddressJsonAdapter implements JsonAdapter<Address>, ViewBuilderAware {

private final JsonAdapter<String> stringAdapter;
private final PropertyNames names;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import java.lang.invoke.MethodHandle;

public class ContactJsonAdapter extends JsonAdapter<Contact> implements ViewBuilderAware {
public class ContactJsonAdapter implements JsonAdapter<Contact>, ViewBuilderAware {

private final JsonAdapter<Long> longAdapter;
private final JsonAdapter<String> stringAdapter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.time.Instant;
import java.util.List;

public class CustomerJsonAdapter extends JsonAdapter<Customer> implements ViewBuilderAware {
public class CustomerJsonAdapter implements JsonAdapter<Customer>, ViewBuilderAware {

private final JsonAdapter<Integer> intAdapter;
private final JsonAdapter<String> stringAdapter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.lang.invoke.MethodHandle;

@Generated
public final class MyBasicJsonAdapter extends JsonAdapter<StreamBasicTest.MyBasic> implements ViewBuilderAware {
public final class MyBasicJsonAdapter implements JsonAdapter<StreamBasicTest.MyBasic>, ViewBuilderAware {

// naming convention Match
// id [int] name:id constructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import io.avaje.jsonb.spi.Generated;

@Generated
public final class MyEnumJsonAdapter extends JsonAdapter<MyEnum> {
public final class MyEnumJsonAdapter implements JsonAdapter<MyEnum> {

private static final Map<MyEnum, String> toValue = new EnumMap<>(MyEnum.class);
private static final Map<String, MyEnum> toEnum = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import io.avaje.jsonb.JsonWriter;
import io.avaje.jsonb.Jsonb;

public final class MyIntEnumJsonAdapter extends JsonAdapter<MyIntEnum> {
public final class MyIntEnumJsonAdapter implements JsonAdapter<MyIntEnum> {

private static final Map<MyIntEnum, Integer> toValue = new EnumMap<>(MyIntEnum.class);
private static final Map<Integer, MyIntEnum> toEnum = new HashMap<>();
Expand Down
16 changes: 8 additions & 8 deletions jsonb/src/main/java/io/avaje/jsonb/JsonAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,29 @@
*/
package io.avaje.jsonb;

import io.avaje.jsonb.spi.ViewBuilderAware;

import java.lang.reflect.Type;

import io.avaje.jsonb.spi.ViewBuilderAware;

/**
* The core API for serialization to and from json.
*/
public abstract class JsonAdapter<T> {
public interface JsonAdapter<T> {

/**
* Write the value to the writer.
*/
public abstract void toJson(JsonWriter writer, T value);
void toJson(JsonWriter writer, T value);

/**
* Read the type from the reader.
*/
public abstract T fromJson(JsonReader reader);
T fromJson(JsonReader reader);

/**
* Return a null safe version of this adapter.
*/
public final JsonAdapter<T> nullSafe() {
default JsonAdapter<T> nullSafe() {
if (this instanceof NullSafeAdapter) {
return this;
}
Expand All @@ -47,14 +47,14 @@ public final JsonAdapter<T> nullSafe() {
/**
* Return true if this adapter represents a json object or json array of objects that supports json views.
*/
public boolean isViewBuilderAware() {
default boolean isViewBuilderAware() {
return false;
}

/**
* Return the ViewBuilder.Aware for this adapter.
*/
public ViewBuilderAware viewBuild() {
default ViewBuilderAware viewBuild() {
throw new IllegalStateException("This adapter is not ViewBuilderAware");
}

Expand Down
5 changes: 3 additions & 2 deletions jsonb/src/main/java/io/avaje/jsonb/NullSafeAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import io.avaje.jsonb.spi.ViewBuilderAware;

final class NullSafeAdapter<T> extends JsonAdapter<T> {
final class NullSafeAdapter<T> implements JsonAdapter<T> {

private final JsonAdapter<T> delegate;

Expand All @@ -43,7 +43,8 @@ public T fromJson(JsonReader reader) {
}
}

public boolean isViewBuilderAware() {
@Override
public boolean isViewBuilderAware() {
return delegate.isViewBuilderAware();
}

Expand Down
4 changes: 2 additions & 2 deletions jsonb/src/main/java/io/avaje/jsonb/core/ArrayAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* Converts arrays to JSON arrays containing their converted contents.
* This supports both primitive and object arrays.
*/
final class ArrayAdapter extends JsonAdapter<Object> {
final class ArrayAdapter implements JsonAdapter<Object> {
static final Factory FACTORY = (type, jsonb) -> {
Type elementType = Util.arrayComponentType(type);
if (elementType == null) return null;
Expand Down Expand Up @@ -75,7 +75,7 @@ public String toString() {
return elementAdapter + ".array()";
}

static final class ByteArray extends JsonAdapter<byte[]> {
static final class ByteArray implements JsonAdapter<byte[]> {
@Override
public byte[] fromJson(JsonReader reader) {
return reader.readBinary();
Expand Down
28 changes: 14 additions & 14 deletions jsonb/src/main/java/io/avaje/jsonb/core/BasicTypeAdapters.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ final class BasicTypeAdapters {
return null;
};

private static final class UuidAdapter extends JsonAdapter<UUID> {
private static final class UuidAdapter implements JsonAdapter<UUID> {
@Override
public UUID fromJson(JsonReader reader) {
return UUID.fromString(reader.readString());
Expand All @@ -77,7 +77,7 @@ public String toString() {
}
}

private static final class UrlAdapter extends JsonAdapter<URL> {
private static final class UrlAdapter implements JsonAdapter<URL> {
@Override
public URL fromJson(JsonReader reader) {
try {
Expand All @@ -98,7 +98,7 @@ public String toString() {
}
}

private static final class UriAdapter extends JsonAdapter<URI> {
private static final class UriAdapter implements JsonAdapter<URI> {
@Override
public URI fromJson(JsonReader reader) {
return URI.create(reader.readString());
Expand All @@ -115,7 +115,7 @@ public String toString() {
}
}

static final class BooleanAdapter extends JsonAdapter<Boolean> {
static final class BooleanAdapter implements JsonAdapter<Boolean> {
@Override
public Boolean fromJson(JsonReader reader) {
return reader.readBoolean();
Expand All @@ -132,7 +132,7 @@ public String toString() {
}
}

static final class ByteAdapter extends JsonAdapter<Byte> {
static final class ByteAdapter implements JsonAdapter<Byte> {
@Override
public Byte fromJson(JsonReader reader) {
return (byte) rangeCheckNextInt(reader, "a byte", -128, 255);
Expand All @@ -149,7 +149,7 @@ public String toString() {
}
}

static final class CharacterAdapter extends JsonAdapter<Character> {
static final class CharacterAdapter implements JsonAdapter<Character> {
@Override
public Character fromJson(JsonReader reader) {
String value = reader.readString();
Expand All @@ -171,7 +171,7 @@ public String toString() {
}
}

static final class DoubleAdapter extends JsonAdapter<Double> {
static final class DoubleAdapter implements JsonAdapter<Double> {
@Override
public Double fromJson(JsonReader reader) {
return reader.readDouble();
Expand All @@ -188,7 +188,7 @@ public String toString() {
}
}

static final class FloatAdapter extends JsonAdapter<Float> {
static final class FloatAdapter implements JsonAdapter<Float> {
@Override
public Float fromJson(JsonReader reader) {
float value = (float) reader.readDouble();
Expand All @@ -211,7 +211,7 @@ public String toString() {
}
}

static final class IntegerAdapter extends JsonAdapter<Integer> {
static final class IntegerAdapter implements JsonAdapter<Integer> {
@Override
public Integer fromJson(JsonReader reader) {
return reader.readInt();
Expand All @@ -228,7 +228,7 @@ public String toString() {
}
}

static final class LongAdapter extends JsonAdapter<Long> {
static final class LongAdapter implements JsonAdapter<Long> {
@Override
public Long fromJson(JsonReader reader) {
return reader.readLong();
Expand All @@ -245,7 +245,7 @@ public String toString() {
}
}

static final class ShortAdapter extends JsonAdapter<Short> {
static final class ShortAdapter implements JsonAdapter<Short> {
@Override
public Short fromJson(JsonReader reader) {
return (short) rangeCheckNextInt(reader, "a short", -32768, 32767);
Expand All @@ -262,7 +262,7 @@ public String toString() {
}
}

static final class StringAdapter extends JsonAdapter<String> {
static final class StringAdapter implements JsonAdapter<String> {
@Override
public String fromJson(JsonReader reader) {
return reader.readString();
Expand All @@ -289,7 +289,7 @@ static int rangeCheckNextInt(JsonReader reader, String typeMessage, int min, int
}

@SuppressWarnings("rawtypes")
static final class ObjectJsonAdapter extends JsonAdapter<Object> {
static final class ObjectJsonAdapter implements JsonAdapter<Object> {
private final Jsonb jsonb;
private final JsonAdapter<List> listJsonAdapter;
private final JsonAdapter<Map> mapAdapter;
Expand Down Expand Up @@ -351,7 +351,7 @@ public String toString() {
}
}

static class EnumJsonAdapter<T extends Enum<T>> extends JsonAdapter<T> {
static class EnumJsonAdapter<T extends Enum<T>> implements JsonAdapter<T> {

protected final Class<T> enumType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
/**
* Converts collection types to JSON arrays containing their converted contents.
*/
abstract class CollectionAdapter<C extends Collection<T>, T> extends JsonAdapter<C> implements ViewBuilderAware {
abstract class CollectionAdapter<C extends Collection<T>, T> implements JsonAdapter<C>, ViewBuilderAware {

static final JsonAdapter.Factory FACTORY = (type, jsonb) -> {
Class<?> rawType = Util.rawType(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ IllegalArgumentException exceptionWithLookupStack(IllegalArgumentException e) {
/**
* This class implements {@code JsonAdapter} so it can be used as a stub for re-entrant calls.
*/
static final class Lookup<T> extends JsonAdapter<T> {
static final class Lookup<T> implements JsonAdapter<T> {
final Type type;
final Object cacheKey;
JsonAdapter<T> adapter;
Expand Down
2 changes: 1 addition & 1 deletion jsonb/src/main/java/io/avaje/jsonb/core/MapAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
/**
* Converts maps with string keys to JSON objects.
*/
final class MapAdapter<V> extends JsonAdapter<Map<String, V>> {
final class MapAdapter<V> implements JsonAdapter<Map<String, V>> {

static final Factory FACTORY = (type, jsonb) -> {
Class<?> rawType = Util.rawType(type);
Expand Down
Loading

0 comments on commit c375aef

Please sign in to comment.