Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
Signed-off-by: David Kral <[email protected]>
  • Loading branch information
Verdent committed Jan 7, 2025
1 parent 25f5fc2 commit 6fafb9c
Show file tree
Hide file tree
Showing 15 changed files with 268 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import io.helidon.common.Generated;
import io.helidon.common.GenericType;
import io.helidon.common.Size;
import io.helidon.common.Weight;

/**
* Commonly used type names.
Expand Down Expand Up @@ -207,6 +208,10 @@ public final class TypeNames {
* Helidon {@link io.helidon.common.GenericType}.
*/
public static final TypeName GENERIC_TYPE = TypeName.create(GenericType.class);
/**
* Helidon {@link io.helidon.common.Weight}.
*/
public static final TypeName WEIGHT = TypeName.create(Weight.class);
/**
* Type name for {@link io.helidon.common.Size}.
*/
Expand Down
59 changes: 34 additions & 25 deletions json/binding/src/main/java/io/helidon/json/binding/JsonBinding.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@
import java.io.ByteArrayOutputStream;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import io.helidon.builder.api.RuntimeType;
import io.helidon.common.GenericType;
import io.helidon.common.types.ResolvedType;
import io.helidon.common.types.TypeName;
import io.helidon.json.processor.Generator;
import io.helidon.json.processor.JsonParser;

Expand All @@ -26,7 +22,7 @@ public final class JsonBinding implements RuntimeType.Api<JsonBindingConfig> {
private final JsonBindingConfig config;
private final Map<Type, JsonSerializer<?>> serializers = new HashMap<>();
private final Map<Type, JsonDeserializer<?>> deserializers = new HashMap<>();
private final Map<Type, JsonGenericTypeBindingFactory<?>> bindingFactories = new HashMap<>();
private final Map<Type, JsonBindingFactory<?>> bindingFactories = new HashMap<>();

private JsonBinding(JsonBindingConfig config) {
this.config = config;
Expand All @@ -41,7 +37,7 @@ private JsonBinding(JsonBindingConfig config) {
deserializers.putIfAbsent(deserializer.type().type(), deserializer);
}
//Fill in binding factories
for (TypedGenericTypeBindingFactory<?> bindingFactory : config.bindingFactories()) {
for (TypedJsonBindingFactory<?> bindingFactory : config.bindingFactories()) {
bindingFactories.putIfAbsent(bindingFactory.type(), bindingFactory);
}
}
Expand Down Expand Up @@ -158,18 +154,25 @@ public <T> JsonSerializer<T> getSerializer(Type type) {
JsonSerializer<T> serializer = (JsonSerializer<T>) serializers.get(type);

if (serializer == null) {
JsonBindingFactory<T> factory;
Type toProcess;
if (type instanceof GenericType<?> genericType) {
JsonGenericTypeBindingFactory<T> factory =
(JsonGenericTypeBindingFactory<T>) bindingFactories.get(genericType.rawType());
if (factory == null) {
throw new IllegalStateException("Serializer/Converter/BindingFactory for type "
+ type + " is not registered.");
}
serializer = factory.createSerializer(this, (GenericType<T>) genericType);
serializers.put(type, serializer);
factory = (JsonBindingFactory<T>) bindingFactories.get(genericType.rawType());
toProcess = genericType.type();
} else if (type instanceof ParameterizedType parameterizedType) {
factory = (JsonBindingFactory<T>) bindingFactories.get(parameterizedType.getRawType());
toProcess = type;
} else {
throw new IllegalStateException("Serializer/Converter for type " + type + " is not registered.");
factory = (JsonBindingFactory<T>) bindingFactories.get(type);
toProcess = type;
}
if (factory == null) {
throw new IllegalStateException("Serializer/Converter/BindingFactory for type "
+ type + " is not registered.");
}
serializer = factory.createSerializer(this, toProcess);
serializers.putIfAbsent(type, serializer);
serializers.putIfAbsent(toProcess, serializer);
}
return serializer;
}
Expand All @@ -179,19 +182,25 @@ public <T> JsonDeserializer<T> getDeserializer(Type type) {
JsonDeserializer<T> deserializer = (JsonDeserializer<T>) deserializers.get(type);

if (deserializer == null) {
JsonBindingFactory<T> factory;
Type toProcess;
if (type instanceof GenericType<?> genericType) {
JsonGenericTypeBindingFactory<T> factory =
(JsonGenericTypeBindingFactory<T>) bindingFactories.get(genericType.rawType());
if (factory == null) {
throw new IllegalStateException("Deserializer/Converter/BindingFactory for type "
+ type + " is not registered.");
}
deserializer = factory.createDeserializer(this, (GenericType<T>) genericType);
deserializers.put(type, deserializer);
deserializers.put(genericType.type(), deserializer);
factory = (JsonBindingFactory<T>) bindingFactories.get(genericType.rawType());
toProcess = genericType.type();
} else if (type instanceof ParameterizedType parameterizedType) {
factory = (JsonBindingFactory<T>) bindingFactories.get(parameterizedType.getRawType());
toProcess = type;
} else {
throw new IllegalStateException("Deserializer/Converter for type " + type + " is not registered.");
factory = (JsonBindingFactory<T>) bindingFactories.get(type);
toProcess = type;
}
if (factory == null) {
throw new IllegalStateException("Deserializer/Converter/BindingFactory for type "
+ type + " is not registered.");
}
deserializer = factory.createDeserializer(this, toProcess);
deserializers.putIfAbsent(type, deserializer);
deserializers.putIfAbsent(toProcess, deserializer);
}
return deserializer;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.helidon.json.binding;

import java.util.List;
import java.util.Map;

import io.helidon.builder.api.Option;
import io.helidon.builder.api.Prototype;
Expand Down Expand Up @@ -37,6 +36,6 @@ interface JsonBindingConfigBlueprint extends Prototype.Factory<JsonBinding> {
*/
@Option.Singular
@Option.RegistryService
List<TypedGenericTypeBindingFactory<?>> bindingFactories();
List<TypedJsonBindingFactory<?>> bindingFactories();

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package io.helidon.json.binding;

import io.helidon.common.GenericType;
import java.lang.reflect.Type;

public interface JsonGenericTypeBindingFactory<T> {
public interface JsonBindingFactory<T> {

JsonDeserializer<T> createDeserializer(JsonBinding jsonBinding, GenericType<T> type);
JsonSerializer<T> createSerializer(JsonBinding jsonBinding, GenericType<T> type);
JsonDeserializer<T> createDeserializer(JsonBinding jsonBinding, Type type);
JsonSerializer<T> createSerializer(JsonBinding jsonBinding, Type type);

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.helidon.json.binding;

public interface TypedJsonBindingFactory<T> extends JsonBindingFactory<T> {

Class<?> type();

}
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package io.helidon.json.binding.factories;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import io.helidon.common.GenericType;
import io.helidon.common.Weight;
import io.helidon.common.Weighted;
import io.helidon.json.binding.JsonBinding;
import io.helidon.json.binding.JsonConverter;
import io.helidon.json.binding.JsonDeserializer;
import io.helidon.json.binding.JsonSerializer;
import io.helidon.json.binding.TypedGenericTypeBindingFactory;
import io.helidon.json.binding.TypedJsonBindingFactory;
import io.helidon.json.binding.TypedJsonConverter;
import io.helidon.json.processor.Generator;
import io.helidon.json.processor.JsonException;
Expand All @@ -19,15 +21,15 @@

@Service.Singleton
@Weight(Weighted.DEFAULT_WEIGHT - 10)
final class ListBindingFactory<T> implements TypedGenericTypeBindingFactory<List<T>> {
final class ListBindingFactory<T> implements TypedJsonBindingFactory<List<T>> {

@Override
public JsonDeserializer<List<T>> createDeserializer(JsonBinding jsonBinding, GenericType<List<T>> type) {
public JsonDeserializer<List<T>> createDeserializer(JsonBinding jsonBinding, Type type) {
return new ListConverter<>(jsonBinding, type);
}

@Override
public JsonSerializer<List<T>> createSerializer(JsonBinding jsonBinding, GenericType<List<T>> type) {
public JsonSerializer<List<T>> createSerializer(JsonBinding jsonBinding, Type type) {
return new ListConverter<>(jsonBinding, type);
}

Expand All @@ -36,15 +38,13 @@ public Class<?> type() {
return List.class;
}

private static final class ListConverter<T> implements TypedJsonConverter<List<T>> {
private static final class ListConverter<T> implements JsonConverter<List<T>> {

private final GenericType<List<T>> type;
private final JsonDeserializer<T> deserializer;
private final JsonSerializer<T> serializer;

private ListConverter(JsonBinding jsonBinding, GenericType<List<T>> type) {
this.type = type;
if (type.type() instanceof ParameterizedType parameterizedType) {
private ListConverter(JsonBinding jsonBinding, Type type) {
if (type instanceof ParameterizedType parameterizedType) {
deserializer = jsonBinding.getDeserializer(parameterizedType.getActualTypeArguments()[0]);
serializer = jsonBinding.getSerializer(parameterizedType.getActualTypeArguments()[0]);
} else {
Expand Down Expand Up @@ -99,11 +99,6 @@ public List<T> fromJson(JsonParser parser) {
return list;
}

@Override
public GenericType<List<T>> type() {
return type;
}

}

}
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package io.helidon.json.binding.factories;

import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;
import java.lang.reflect.Type;
import java.util.HashSet;
import java.util.Set;

import io.helidon.common.GenericType;
import io.helidon.common.Weight;
import io.helidon.common.Weighted;
import io.helidon.json.binding.JsonBinding;
import io.helidon.json.binding.JsonConverter;
import io.helidon.json.binding.JsonDeserializer;
import io.helidon.json.binding.JsonSerializer;
import io.helidon.json.binding.TypedGenericTypeBindingFactory;
import io.helidon.json.binding.TypedJsonBindingFactory;
import io.helidon.json.binding.TypedJsonConverter;
import io.helidon.json.processor.Generator;
import io.helidon.json.processor.JsonException;
Expand All @@ -19,32 +21,30 @@

@Service.Singleton
@Weight(Weighted.DEFAULT_WEIGHT - 10)
final class SetBindingFactory<T> implements TypedGenericTypeBindingFactory<List<T>> {
final class SetBindingFactory<T> implements TypedJsonBindingFactory<Set<T>> {

@Override
public JsonDeserializer<List<T>> createDeserializer(JsonBinding jsonBinding, GenericType<List<T>> type) {
return new ListConverter<>(jsonBinding, type);
public JsonDeserializer<Set<T>> createDeserializer(JsonBinding jsonBinding, Type type) {
return new SetConverter<>(jsonBinding, type);
}

@Override
public JsonSerializer<List<T>> createSerializer(JsonBinding jsonBinding, GenericType<List<T>> type) {
return new ListConverter<>(jsonBinding, type);
public JsonSerializer<Set<T>> createSerializer(JsonBinding jsonBinding, Type type) {
return new SetConverter<>(jsonBinding, type);
}

@Override
public Class<?> type() {
return List.class;
return Set.class;
}

private static final class ListConverter<T> implements TypedJsonConverter<List<T>> {
private static final class SetConverter<T> implements JsonConverter<Set<T>> {

private final GenericType<List<T>> type;
private final JsonDeserializer<T> deserializer;
private final JsonSerializer<T> serializer;

private ListConverter(JsonBinding jsonBinding, GenericType<List<T>> type) {
this.type = type;
if (type.type() instanceof ParameterizedType parameterizedType) {
private SetConverter(JsonBinding jsonBinding, Type type) {
if (type instanceof ParameterizedType parameterizedType) {
deserializer = jsonBinding.getDeserializer(parameterizedType.getActualTypeArguments()[0]);
serializer = jsonBinding.getSerializer(parameterizedType.getActualTypeArguments()[0]);
} else {
Expand All @@ -54,7 +54,7 @@ private ListConverter(JsonBinding jsonBinding, GenericType<List<T>> type) {
}

@Override
public void toJson(Generator generator, List<T> instance) {
public void toJson(Generator generator, Set<T> instance) {
if (instance == null) {
generator.writeNull();
return;
Expand All @@ -74,34 +74,29 @@ public void toJson(Generator generator, List<T> instance) {
}

@Override
public List<T> fromJson(JsonParser parser) {
public Set<T> fromJson(JsonParser parser) {
if (parser.checkNull()) {
return null;
}
List<T> list = new ArrayList<>();
Set<T> set = new HashSet<>();
byte lastByte = parser.lastByte();
if (lastByte != '[') {
throw new JsonException("Array start expected. Found: " + Character.toString(lastByte));
}
lastByte = parser.nextToken();
if (lastByte != ']') {
list.add(deserializer.fromJson(parser));
set.add(deserializer.fromJson(parser));
lastByte = parser.nextToken();
while (lastByte == ',') {
parser.nextToken();
list.add(deserializer.fromJson(parser));
set.add(deserializer.fromJson(parser));
lastByte = parser.nextToken();
}
if (lastByte != ']') {
throw new JsonException("Array end expected, received: " + Character.toString(lastByte));
}
}
return list;
}

@Override
public GenericType<List<T>> type() {
return type;
return set;
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ private static TypeName resolveGenerics(TypeName elementTypeName, TypeInfo typeI
if (needsResolving(elementTypeName)) {
if (elementTypeName.generic()) {
int index = typeInfo.typeName().typeParameters().indexOf(elementTypeName.className());
if (index == -1) {
return elementTypeName;
}
return typeInfo.typeName().typeArguments().get(index);
}
TypeName.Builder builder = TypeName.builder()
Expand Down
Loading

0 comments on commit 6fafb9c

Please sign in to comment.