io.github.stewseo.client.json.JsonpDeserializer Maven / Gradle / Ivy
package io.github.stewseo.client.json;
import io.github.stewseo.client.util.TriFunction;
import jakarta.json.JsonValue;
import jakarta.json.stream.JsonParser;
import jakarta.json.stream.JsonParser.Event;
import java.io.StringReader;
import java.lang.reflect.Type;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Supplier;
public interface JsonpDeserializer {
EnumSet nativeEvents();
EnumSet acceptedEvents();
default boolean accepts(Event event) {
return acceptedEvents().contains(event);
}
default V deserialize(JsonParser parser, JsonpMapper mapper) {
Event event = parser.next();
// JSON null: return null unless the deserializer can handle it
if (event == Event.VALUE_NULL && !accepts(Event.VALUE_NULL)) {
return null;
}
JsonpUtils.ensureAccepts(this, parser, event);
return deserialize(parser, mapper, event);
}
V deserialize(JsonParser parser, JsonpMapper mapper, Event event);
//---------------------------------------------------------------------------------------------
/**
* Creates a deserializer for a type that delegates to the mapper provided to
* {@link #deserialize(JsonParser, JsonpMapper)}.
*/
static JsonpDeserializer of(Type type) {
return new JsonpDeserializerBase<>(EnumSet.allOf(Event.class)) {
@Override
public T deserialize(JsonParser parser, JsonpMapper mapper) {
return mapper.deserialize(parser, type);
}
@Override
public T deserialize(JsonParser parser, JsonpMapper mapper, Event event) {
throw new UnsupportedOperationException();
}
};
}
static JsonpDeserializer of(EnumSet acceptedEvents, BiFunction fn) {
return new JsonpDeserializerBase<>(acceptedEvents) {
@Override
public T deserialize(JsonParser parser, JsonpMapper mapper) {
return fn.apply(parser, mapper);
}
@Override
public T deserialize(JsonParser parser, JsonpMapper mapper, Event event) {
throw new UnsupportedOperationException();
}
};
}
static JsonpDeserializer of(EnumSet acceptedEvents, TriFunction fn) {
return new JsonpDeserializerBase<>(acceptedEvents) {
@Override
public T deserialize(JsonParser parser, JsonpMapper mapper, Event event) {
return fn.apply(parser, mapper, event);
}
};
}
static JsonpDeserializer lazy(Supplier> ctor) {
return new LazyDeserializer<>(ctor);
}
//----- Builtin types
static JsonpDeserializer fixedValue(T value) {
return new JsonpDeserializerBase<>(EnumSet.noneOf(Event.class)) {
@Override
public T deserialize(JsonParser parser, JsonpMapper mapper, Event event) {
return value;
}
};
}
static JsonpDeserializer emptyObject(T value) {
return new JsonpDeserializerBase<>(EnumSet.of(Event.START_OBJECT)) {
@Override
public T deserialize(JsonParser parser, JsonpMapper mapper, Event event) {
if (event == Event.VALUE_NULL) {
return null;
}
JsonpUtils.expectNextEvent(parser, Event.END_OBJECT);
return value;
}
};
}
static JsonpDeserializer stringDeserializer() {
return JsonpDeserializerBase.STRING;
}
static JsonpDeserializer integerDeserializer() {
return JsonpDeserializerBase.INTEGER;
}
static JsonpDeserializer booleanDeserializer() {
return JsonpDeserializerBase.BOOLEAN;
}
static JsonpDeserializer longDeserializer() {
return JsonpDeserializerBase.LONG;
}
static JsonpDeserializer floatDeserializer() {
return JsonpDeserializerBase.FLOAT;
}
static JsonpDeserializer doubleDeserializer() {
return JsonpDeserializerBase.DOUBLE;
}
static JsonpDeserializer doubleOrNullDeserializer(double defaultValue) {
return new JsonpDeserializerBase.DoubleOrNullDeserializer(defaultValue);
}
static JsonpDeserializer intOrNullDeserializer(int defaultValue) {
return new JsonpDeserializerBase.IntOrNullDeserializer(defaultValue);
}
static JsonpDeserializer numberDeserializer() {
return JsonpDeserializerBase.NUMBER;
}
static JsonpDeserializer jsonValueDeserializer() {
return JsonpDeserializerBase.JSON_VALUE;
}
static JsonpDeserializer voidDeserializer() {
return JsonpDeserializerBase.VOID;
}
static JsonpDeserializer> arrayDeserializer(JsonpDeserializer itemDeserializer) {
return new JsonpDeserializerBase.ArrayDeserializer<>(itemDeserializer);
}
static JsonpDeserializer
© 2015 - 2025 Weber Informatics LLC | Privacy Policy