io.activej.json.ObjectJsonCodec 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 org.jetbrains.annotations.Nullable;
import java.util.*;
import java.util.function.Function;
import java.util.function.Supplier;
import static io.activej.common.Utils.*;
@SuppressWarnings("unchecked")
public final class ObjectJsonCodec extends AbstractMapJsonCodec {
public interface JsonCodecProvider extends JsonEncoderProvider, JsonDecoderProvider {
}
public interface JsonEncoderProvider {
@Nullable JsonCodec encoder(String key, int index, T item, V value);
static JsonEncoderProvider of(JsonCodec codec) {
return (key, index, item, value) -> codec;
}
}
public interface JsonDecoderProvider {
@Nullable JsonCodec decoder(String key, int index, A accumulator) throws JsonValidationException;
static JsonDecoderProvider of(JsonCodec codec) {
return (key, index, accumulator) -> codec;
}
}
private record Field(int index, String key,
Function getter, JsonSetter setter,
JsonEncoderProvider encoderFn, JsonDecoderProvider decoderFn
) {
}
private final Supplier accumulatorSupplier;
private final Field[] fields;
private final Map> map;
private final JsonFunction constructor;
private ObjectJsonCodec(Supplier accumulatorSupplier, JsonFunction constructor,
Field[] fields, Map> map
) {
this.accumulatorSupplier = accumulatorSupplier;
this.constructor = constructor;
this.fields = fields;
this.map = map;
}
public static BuilderObject builder(Supplier accumulatorSupplier, JsonFunction constructor) {
return new BuilderObject<>(accumulatorSupplier, constructor);
}
public static BuilderArray builder(JsonConstructorN constructor) {
return new BuilderArray<>(constructor);
}
public static class BuilderObject extends AbstractBuilder, ObjectJsonCodec> {
private final Supplier accumulatorSupplier;
private final JsonFunction constructor;
private final List> fields = new ArrayList<>();
public BuilderObject(Supplier accumulatorSupplier, JsonFunction constructor) {
this.accumulatorSupplier = accumulatorSupplier;
this.constructor = constructor;
}
public BuilderObject with(String key,
Function getter, JsonSetter setter,
JsonCodec codec
) {
return with(key, getter, setter, JsonEncoderProvider.of(codec), JsonDecoderProvider.of(codec));
}
public BuilderObject with(String key,
Function getter, JsonSetter setter,
JsonCodecProvider codecFn
) {
return with(key, getter, setter, codecFn, codecFn);
}
public BuilderObject with(String key,
Function getter, JsonSetter setter,
JsonEncoderProvider encoderFn, JsonDecoderProvider decoderFn
) {
Field field = new Field<>(fields.size(), key, getter, setter, encoderFn, decoderFn);
fields.add((Field) field);
return this;
}
@Override
protected ObjectJsonCodec doBuild() {
return new ObjectJsonCodec<>(
accumulatorSupplier,
constructor,
fields.toArray(Field[]::new),
fields.stream().collect(toHashMap(f -> f.key, f -> f)));
}
}
public static final class BuilderArray extends AbstractBuilder, ObjectJsonCodec> {
private static final Object NO_DEFAULT_VALUE = new Object();
private final List> fields = new ArrayList<>();
private final JsonConstructorN constructor;
private final List