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

io.github.stewseo.client.json.ObjectBuilderDeserializer Maven / Gradle / Ivy

Go to download

java client to build api objects, handle http transport, and parse/deserialize/serialize json to/from json

There is a newer version: 1.8.0
Show newest version
package io.github.stewseo.client.json;


import io.github.stewseo.client.util.ObjectBuilder;
import jakarta.json.stream.JsonParser;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

public class ObjectBuilderDeserializer> extends DelegatingDeserializer {

    private final JsonpDeserializer builderDeserializer;

    public static , T> JsonpDeserializer lazy(
            Supplier builderCtor,
            Consumer> builderDeserializerSetup
    ) {
        return new LazyDeserializer<>(() -> {
            ObjectDeserializer builderDeser = new ObjectDeserializer<>(builderCtor);
            builderDeserializerSetup.accept(builderDeser);
            return new ObjectBuilderDeserializer<>(builderDeser);
        });
    }

    public static  JsonpDeserializer lazy(
            Supplier builderCtor,
            Consumer> builderDeserializerSetup,
            Function buildFn
    ) {
        return new LazyDeserializer<>(() -> {
            ObjectDeserializer builderDeser = new ObjectDeserializer<>(builderCtor);
            builderDeserializerSetup.accept(builderDeser);
            return new BuildFunctionDeserializer<>(builderDeser, buildFn);
        });
    }

    public static > JsonpDeserializer createForObject(
            Supplier ctor,
            Consumer> configurer
    ) {
        ObjectDeserializer op = new ObjectDeserializer<>(ctor);
        configurer.accept(op);
        return new ObjectBuilderDeserializer<>(op);
    }

    public ObjectBuilderDeserializer(JsonpDeserializer builderDeserializer) {
        this.builderDeserializer = builderDeserializer;
    }

    @Override
    protected JsonpDeserializer unwrap() {
        return builderDeserializer;
    }

    @Override
    public T deserialize(JsonParser parser, JsonpMapper mapper) {
        ObjectBuilder builder = builderDeserializer.deserialize(parser, mapper);
        return builder == null ? null : builder.build();
    }

    @Override
    public T deserialize(JsonParser parser, JsonpMapper mapper, JsonParser.Event event) {
        ObjectBuilder builder = builderDeserializer.deserialize(parser, mapper, event);
        return builder == null ? null : builder.build();
    }
}