Package org.djutils.data.serialization
Interface TextSerializer<T>
-
- Type Parameters:
T- the value type
- All Known Subinterfaces:
SpecificTextSerializer<T>
- All Known Implementing Classes:
BooleanSerializer,ByteSerializer,CharacterSerializer,DoubleScalarSerializer,DoubleSerializer,FloatScalarSerializer,FloatSerializer,IntegerSerializer,LongSerializer,ShortSerializer,StringSerializer
public interface TextSerializer<T>TextSerializer defines the serialize and deserialize methods.Copyright (c) 2020-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
BSD-style license. See DJUTILS License.- Author:
- Alexander Verbraeck, Peter Knoppers, Wouter Schakel
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Modifier and Type Method Description Tdeserialize(Class<T> type, String text, String unit)Deserialize a value from text that has been created with the corresponding serializer.static <T> Tdeserialize(TextSerializer<?> serializer, String text, Column<?> column)Helper function to deal with casting when callingTextSerializer.deserialize().static TextSerializer<?>resolve(Class<?> valueClass)Resolve the correct (de)serializer for the given class, and return an instance of the (de)serializer.static <T> Stringserialize(TextSerializer<?> serializer, Object value, String unit)Helper function to deal with casting when callingTextSerializer.serialize().Stringserialize(T value, String unit)Serialize a value to text in such a way that it can be deserialized with the corresponding deserializer.
-
-
-
Method Detail
-
serialize
String serialize(T value, String unit)
Serialize a value to text in such a way that it can be deserialized with the corresponding deserializer. Note thatnullvalues for value are allowed. Anullvalues stands for an empty column value in a CVS-file, a missing tag in an XML-file, etc.- Parameters:
value- T; the value to serialize, may benullunit- String; the unit used to convert the data to and store, so all valus in a column may have the same unit. The value may benullor blank- Returns:
- String; a string representation of the value that can later be deserialized, or
nullto denote a missing value
-
deserialize
T deserialize(Class<T> type, String text, String unit)
Deserialize a value from text that has been created with the corresponding serializer. Note thatnullvalues for text are allowed. Anullvalues stands for an empty column value in a CVS-file, a missing tag in an XML-file, etc. In this way, we can explicitly show values that were not specified in the file. Also, the type may benull; this is, for instance, the case for anyTextSerializerimplementingSpecificTextSerializer, where no class needs to be provided (although it can).- Parameters:
type- Class<T>; class of the value type, may benulltext- String; the string to deserialize, may benullor blankunit- String; unit with the value, may benullor blank- Returns:
- T; an instance of the object created with the corresponding serializer, may be
nullwhen a value was not specified in the source from which the deserializer was called
-
resolve
static TextSerializer<?> resolve(Class<?> valueClass) throws TextSerializationException
Resolve the correct (de)serializer for the given class, and return an instance of the (de)serializer.- Parameters:
valueClass- Class<?>; the class to resolve the (de)serializer for- Returns:
- an instance of the correct (de)serializer
- Throws:
TextSerializationException- when there is no corresponding (de)serializer for the class
-
serialize
static <T> String serialize(TextSerializer<?> serializer, Object value, String unit)
Helper function to deal with casting when callingTextSerializer.serialize(). When theresolve(class)method returns an 'unspecified' serializer, thisserializemethod allows you to use it. Note thatnullvalues for value are allowed. Anullvalues stands for an empty column value in a CVS-file, a missing tag in an XML-file, etc.- Type Parameters:
T- value type- Parameters:
serializer- TextSerializer<?>; serializervalue- Object; value, may benullunit- String; the unit used to convert the data to and store, so all valus in a column may have the same unit. The value may benullor blank- Returns:
- String; serialized value, or
nullto denote a missing value
-
deserialize
static <T> T deserialize(TextSerializer<?> serializer, String text, Column<?> column)
Helper function to deal with casting when callingTextSerializer.deserialize(). When theresolve(class)method returns an 'unspecified' serializer, thisserializemethod allows you to use it. Note thatnullvalues for text are allowed. Anullvalues stands for an empty column value in a CVS-file, a missing tag in an XML-file, etc. In this way, we can explicitly show values that were not specified in the file for a certain column.- Type Parameters:
T- value type- Parameters:
serializer- TextSerializer<?>; serializertext- String; value, may benullcolumn- Column<?>; columns- Returns:
- T; deserialized value, may be
nullwhen a value was not specified in the source for which the deserializer was called
-
-