io.activej.json.JsonCodecFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of activej-json Show documentation
Show all versions of activej-json Show documentation
JSON Codecs, Codec Factory and JSON utils for ActiveJ project.
The newest version!
package io.activej.json;
import io.activej.common.builder.AbstractBuilder;
import io.activej.common.builder.Rebuildable;
import io.activej.json.annotations.JsonNullable;
import io.activej.json.annotations.JsonSubclasses;
import io.activej.types.TypeT;
import io.activej.types.scanner.TypeScannerRegistry;
import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Type;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import static io.activej.common.Checks.checkArgument;
import static io.activej.types.Utils.getAnnotation;
import static io.activej.types.Utils.hasAnnotation;
public class JsonCodecFactory implements Rebuildable {
private static final JsonCodecFactory DEFAULT_INSTANCE = JsonCodecFactory.builder().build();
private final TypeScannerRegistry> registry;
private JsonCodecFactory(TypeScannerRegistry> registry) {this.registry = registry;}
public static JsonCodecFactory defaultInstance() {
return DEFAULT_INSTANCE;
}
public static Builder builder() {
JsonCodecFactory factory = new JsonCodecFactory(TypeScannerRegistry.create());
//noinspection unchecked,rawtypes
return factory.new Builder()
.with(String.class, ctx -> JsonCodecs.ofString())
.with(byte.class, ctx -> JsonCodecs.ofByte())
.with(short.class, ctx -> JsonCodecs.ofShort())
.with(int.class, ctx -> JsonCodecs.ofInteger())
.with(long.class, ctx -> JsonCodecs.ofLong())
.with(float.class, ctx -> JsonCodecs.ofFloat())
.with(double.class, ctx -> JsonCodecs.ofDouble())
.with(boolean.class, ctx -> JsonCodecs.ofBoolean())
.with(char.class, ctx -> JsonCodecs.ofCharacter())
.with(Byte.class, ctx -> JsonCodecs.ofByte())
.with(Short.class, ctx -> JsonCodecs.ofShort())
.with(Integer.class, ctx -> JsonCodecs.ofInteger())
.with(Long.class, ctx -> JsonCodecs.ofLong())
.with(Float.class, ctx -> JsonCodecs.ofFloat())
.with(Double.class, ctx -> JsonCodecs.ofDouble())
.with(Boolean.class, ctx -> JsonCodecs.ofBoolean())
.with(Character.class, ctx -> JsonCodecs.ofCharacter())
.with(LocalDate.class, ctx -> JsonCodecs.ofLocalDate())
.with(Enum.class, ctx -> JsonCodecs.ofEnum((Class) ctx.getRawType()))
.with(List.class, ctx -> JsonCodecs.ofList(ctx.scanTypeArgument(0)))
.with(Map.class, ctx -> {
AnnotatedType keyAnnotatedType = ctx.getTypeArgument(0);
Class> keyType = (Class>) keyAnnotatedType.getType();
if (keyAnnotatedType.getAnnotations().length == 0 && keyType == String.class) {
//noinspection unchecked
return JsonCodecs.ofMap((JsonCodec