All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.hamnaberg.json.codec.FieldEncoder Maven / Gradle / Ivy

The newest version!
package net.hamnaberg.json.codec;

import net.hamnaberg.json.Json;

import java.util.List;
import java.util.Optional;
import java.util.function.Function;

public final class FieldEncoder {
    public final String name;
    public final EncodeJson encoder;

    private FieldEncoder(String name, EncodeJson encoder) {
        this.name = name;
        this.encoder = encoder;
    }

    @Override
    public String toString() {
        return String.format("FieldDecoder(%s)", name);
    }

    public  FieldEncoder contramap(Function f) {
        return typedFieldOf(name, encoder.contramap(f));
    }

    public Json.JValue toJson(A value) {
        return encoder.toJson(value);
    }

    public static FieldEncoder EString(String name) {
        return typedFieldOf(name, Encoders.EString);
    }

    public static FieldEncoder EInt(String name) {
        return typedFieldOf(name, Encoders.EInt);
    }

    public static FieldEncoder EDouble(String name) {
        return typedFieldOf(name, Encoders.EDouble);
    }

    public static FieldEncoder ELong(String name) {
        return typedFieldOf(name, Encoders.ELong);
    }

    public static FieldEncoder EBoolean(String name) {
        return typedFieldOf(name, Encoders.EBoolean);
    }

    public static  FieldEncoder> EList(String name, EncodeJson encoder) {
        return typedFieldOf(name, Encoders.listEncoder(encoder));
    }

    public static  FieldEncoder> EOptional(String name, EncodeJson encoder) {
        return typedFieldOf(name, Encoders.optionalencoder(encoder));
    }

    public static  FieldEncoder typedFieldOf(String name, EncodeJson encoder) {
        return new FieldEncoder<>(name, encoder);
    }
}