dsl_json.java.util.OptionalLongDslJsonConverter 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.OptionalLong;
public class OptionalLongDslJsonConverter implements Configuration {
@Override
public void configure(DslJson json) {
json.registerWriter(OptionalLong.class, new JsonWriter.WriteObject() {
@Override
public void write(JsonWriter writer, @Nullable OptionalLong value) {
if (value != null && value.isPresent()) NumberConverter.serialize(value.getAsLong(), writer);
else writer.writeNull();
}
});
json.registerReader(OptionalLong.class, new JsonReader.ReadObject() {
@Override
public OptionalLong read(JsonReader reader) throws IOException {
return reader.wasNull() ? OptionalLong.empty() : OptionalLong.of(NumberConverter.deserializeLong(reader));
}
});
json.registerDefault(OptionalLong.class, OptionalLong.empty());
}
}