com.codepoetics.octarine.json.serialisation.RecordSerialiser Maven / Gradle / Ivy
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 extends V> 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 extends V> key, BiConsumer serialiser) {
return write(key, key.name(), serialiser);
}
public Builder write(Key extends V> key, Supplier> serialiserSupplier) {
return write(key, serialiserSupplier.get());
}
public Builder write(Key extends V> 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
© 2015 - 2024 Weber Informatics LLC | Privacy Policy