dsl_json.java.util.OptionalIntDslJsonConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dsl-json-java8 Show documentation
Show all versions of dsl-json-java8 Show documentation
DSL Platform compatible Java JSON library (https://dsl-platform.com)
package dsl_json.java.util;
import com.dslplatform.json.*;
import java.io.IOException;
import java.util.OptionalInt;
public class OptionalIntDslJsonConverter implements Configuration {
@Override
public void configure(DslJson json) {
json.registerWriter(OptionalInt.class, new JsonWriter.WriteObject() {
@Override
public void write(JsonWriter writer, @Nullable OptionalInt value) {
if (value != null && value.isPresent()) NumberConverter.serialize(value.getAsInt(), writer);
else writer.writeNull();
}
});
json.registerReader(OptionalInt.class, new JsonReader.ReadObject() {
@Override
public OptionalInt read(JsonReader reader) throws IOException {
return reader.wasNull() ? OptionalInt.empty() : OptionalInt.of(NumberConverter.deserializeInt(reader));
}
});
json.registerDefault(OptionalInt.class, OptionalInt.empty());
}
}