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

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

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

import net.hamnaberg.json.Json;

import java.util.Optional;

public final class NamedJsonCodec implements JsonCodec {
    public final String name;
    final JsonCodec codec;

    private NamedJsonCodec(String name, JsonCodec codec) {
        this.name = name;
        this.codec = codec;
    }

    public static  NamedJsonCodec of(String name, JsonCodec codec) {
        if (codec instanceof NamedJsonCodec) {
            return new NamedJsonCodec<>(name, ((NamedJsonCodec) codec).codec);
        }
        return new NamedJsonCodec<>(name, codec);
    }

    public FieldDecoder toFieldDecoder() {
        return FieldDecoder.typedFieldOf(name, codec);
    }

    public FieldEncoder toFieldEncoder() {
        return FieldEncoder.typedFieldOf(name, codec);
    }

    @Override
    public DecodeResult fromJson(Json.JValue value) {
        return codec.fromJson(value);
    }

    @Override
    public Json.JValue toJson(A value) {
        return codec.toJson(value);
    }

    @Override
    public Optional defaultValue() {
        return codec.defaultValue();
    }

    @Override
    public String toString() {
        return "NamedJsonCodec{" +
                "name='" + name + '\'' +
                ", codec=" + codec +
                '}';
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy