Class PropertyDeserializer
By default, this class provides deserialization for the following types:
- int/Integer
- long/Long
- float/Float
- double/Double
- boolean/Boolean
- String
- Date (date format detection based on
DateParser
) - LocalDate (date format detection based on
DateParser
) - LocalDateTime (date format detection based on
DateParser
) - File
- UUID
- Version
Support for additional types can be added by defining custom deserialization behavior.
This class does not use reflection, which means it can also be used in environments where reflection is not allowed or not supported. However, this class can also be embedded in other mechanisms that do rely on reflection, allowing for a less low-level and more convenient usage in situations where reflection is allowed.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescription<T> Optional
<T> Attempts to deserialize a text-based property to the specified type, but returns an empty optional when parsing results in an exception or when the value isnull
.static PropertyDeserializer
Returns aPropertyDeserializer
that acts as a live view for parsing values from the specified CSV record.static PropertyDeserializer
Returns aPropertyDeserializer
that acts as a live view for parsing values from the specified map.static PropertyDeserializer
fromProperties
(Properties properties) Returns aPropertyDeserializer
that acts as a live view for parsing values from the specifiedProperties
object.<T> T
Deserializes a text-based property to the specified type.<T> T
Attempts to deserialize a text-based property to the specified type, but returns a default value when parsing results in an exception or when the value isnull
.boolean
double
parseDouble
(String value, double defaultValue) float
parseFloat
(String value, float defaultValue) int
long
parseString
(String value, String defaultValue) <T> void
Registers the specified function as a type mapper.void
registerPreprocessor
(Function<String, String> preprocessor) Registers a function that should be used to pre-process property values before trying to deserialize those values.
-
Constructor Details
-
PropertyDeserializer
public PropertyDeserializer()
-
-
Method Details
-
register
Registers the specified function as a type mapper. Once registered, this class will use the type mapper when it encounters properties that match the specified type.- Throws:
IllegalArgumentException
- if a type mapper has already been registered for the same type.
-
registerPreprocessor
Registers a function that should be used to pre-process property values before trying to deserialize those values. It is possible to register multiple preprocessors, which will then be used in order. -
parse
Deserializes a text-based property to the specified type.If the type mapper throws an exception, this exception is forwarded to the caller of this method. For example, integers are parsed using
Integer.parseInt(String)
. If calling this method results in aNumberFormatException
, that exception will be rethrown by this method.- Throws:
UnsupportedOperationException
- if no type mapper has been registered for the specified type.NullPointerException
- when trying to deserialize a null value.
-
parse
Attempts to deserialize a text-based property to the specified type, but returns a default value when parsing results in an exception or when the value isnull
.- Throws:
UnsupportedOperationException
- if no type mapper has been registered for the specified type.
-
attempt
Attempts to deserialize a text-based property to the specified type, but returns an empty optional when parsing results in an exception or when the value isnull
.- Throws:
UnsupportedOperationException
- if no type mapper has been registered for the specified type.
-
parseString
-
parseInt
-
parseLong
-
parseFloat
-
parseDouble
-
parseBool
-
fromProperties
Returns aPropertyDeserializer
that acts as a live view for parsing values from the specifiedProperties
object. -
fromMap
Returns aPropertyDeserializer
that acts as a live view for parsing values from the specified map. -
fromCSV
Returns aPropertyDeserializer
that acts as a live view for parsing values from the specified CSV record.- Throws:
IllegalStateException
- if the CSV does not include any column information.
-