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

com.codepoetics.octarine.json.serialisation.RecordSerialiser Maven / Gradle / Ivy

There is a newer version: 0.18
Show newest version
package com.codepoetics.octarine.json.serialisation;

import com.codepoetics.octarine.records.Key;
import com.codepoetics.octarine.records.Record;
import com.fasterxml.jackson.core.JsonGenerator;
import org.pcollections.PVector;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Supplier;

public final class RecordSerialiser implements SafeSerialiser {

    public static Builder builder() {
        return new Builder();
    }

    public static final class Builder implements Supplier {
        private final Map, BiConsumer> serialiserMap = new LinkedHashMap<>();

        public  Builder write(Key key, String fieldName, BiConsumer serialiser) {
            BiConsumer keyValueWriter = (JsonGenerator generator, V value) -> {
                try {
                    generator.writeFieldName(fieldName);
                    serialiser.accept(generator, value);
                } catch (IOException e) {
                    throw new SerialisationException(e);
                }
            };
            serialiserMap.put(key, keyValueWriter);
            return this;
        }

        public  Builder write(Key key, BiConsumer serialiser) {
            return write(key, key.name(), serialiser);
        }

        public  Builder write(Key key, Supplier> serialiserSupplier) {
            return write(key, serialiserSupplier.get());
        }

        public  Builder write(Key key, String fieldName, Supplier> serialiserSupplier) {
            return write(key, fieldName, serialiserSupplier.get());
        }

        public Builder writeString(Key key) {
            return write(key, Serialisers.toString);
        }

        public Builder writeString(Key key, String fieldName) {
            return write(key, fieldName, Serialisers.toString);
        }

        public  Builder writeToString(Key key) {
            return writeToString(key, Object::toString);
        }

        public  Builder writeToString(Key key, String fieldName) {
            return writeToString(key, fieldName, Object::toString);
        }

        public  Builder writeToString(Key key, Function converter) {
            return writeToString(key, key.name(), converter);
        }

        public  Builder writeToString(Key key, String fieldName, Function converter) {
            return write(key, fieldName, (g, v) -> Serialisers.toString.accept(g, converter.apply(v)));
        }

        public Builder writeInteger(Key key) {
            return write(key, Serialisers.toInteger);
        }

        public Builder writeInteger(Key key, String fieldName) {
            return write(key, fieldName, Serialisers.toInteger);
        }

        public Builder writeDouble(Key key) {
            return write(key, Serialisers.toDouble);
        }

        public Builder writeDouble(Key key, String fieldName) {
            return write(key, fieldName, Serialisers.toDouble);
        }

        public Builder writeLong(Key key) {
            return write(key, Serialisers.toLong);
        }

        public Builder writeLong(Key key, String fieldName) {
            return write(key, fieldName, Serialisers.toLong);
        }


        public Builder writeBoolean(Key key) {
            return write(key, Serialisers.toBoolean);
        }

        public Builder writeBoolean(Key key, String fieldName) {
            return write(key, fieldName, Serialisers.toBoolean);
        }

        public  Builder writeList(Key> key, Serialiser itemSerialiser) {
            return write(key, ListSerialiser.writingItemsWith(itemSerialiser));
        }

        public  Builder writeList(Key> key, String fieldName, Serialiser itemSerialiser) {
            return write(key, fieldName, ListSerialiser.writingItemsWith(itemSerialiser));
        }

        public  Builder writeMap(Key> key, Serialiser valueSerialiser) {
            return write(key, MapSerialiser.writingValuesWith(valueSerialiser));
        }

        public  Builder writeMap(Key> key, String fieldName, Serialiser valueSerialiser) {
            return write(key, fieldName, MapSerialiser.writingValuesWith(valueSerialiser));
        }

        @Override
        public RecordSerialiser get() {
            return new RecordSerialiser(serialiserMap);
        }
    }

    private final Map, BiConsumer> serialiserMap;

    public RecordSerialiser(Map, BiConsumer> serialiserMap) {
        this.serialiserMap = serialiserMap;
    }

    @Override
    public void unsafeAccept(JsonGenerator generator, Record record) throws IOException {
        generator.writeStartObject();
        serialiserMap.keySet().forEach(k ->
                k.get(record).ifPresent(value ->
                        ((BiConsumer)  serialiserMap.get(k)).accept(generator, value)
        ));
        generator.writeEndObject();
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy