com.codepoetics.octarine.json.deserialisation.ListDeserialiser Maven / Gradle / Ivy
package com.codepoetics.octarine.json.deserialisation;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import org.pcollections.PVector;
import org.pcollections.TreePVector;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Function;
public final class ListDeserialiser implements SafeDeserialiser> {
public static ListDeserialiser readingItemsWith(Function itemDeserialiser) {
return new ListDeserialiser<>(itemDeserialiser);
}
public static ListDeserialiser readingStrings() {
return readingItemsWith(Deserialisers.ofString);
}
public static ListDeserialiser readingIntegers() {
return readingItemsWith(Deserialisers.ofInteger);
}
public static ListDeserialiser readingLongs() {
return readingItemsWith(Deserialisers.ofLong);
}
public static ListDeserialiser readingDoubles() {
return readingItemsWith(Deserialisers.ofDouble);
}
public static ListDeserialiser readingBooleans() {
return readingItemsWith(Deserialisers.ofBoolean);
}
private final Function itemDeserialiser;
private ListDeserialiser(Function itemDeserialiser) {
this.itemDeserialiser = itemDeserialiser;
}
@Override
public PVector applyUnsafe(JsonParser p) throws IOException {
List values = new LinkedList<>();
while (p.nextToken() != JsonToken.END_ARRAY) {
values.add(itemDeserialiser.apply(p));
}
return TreePVector.from(values);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy