View Javadoc
1   package org.djutils.data.serialization;
2   
3   import org.djunits.value.vdouble.scalar.base.DoubleScalarInterface;
4   import org.djunits.value.vfloat.scalar.base.FloatScalarInterface;
5   import org.djutils.exceptions.Throw;
6   
7   /**
8    * TextSerializer defines the serialize and deserialize methods. <br>
9    * <br>
10   * Copyright (c) 2020-2020 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
11   * for project information <a href="https://djutils.org" target="_blank"> https://djutils.org</a>. The DJUTILS project is
12   * distributed under a three-clause BSD-style license, which can be found at
13   * <a href="https://djutils.org/docs/license.html" target="_blank"> https://djutils.org/docs/license.html</a>. <br>
14   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
15   * @author <a href="https://www.tudelft.nl/pknoppers">Peter Knoppers</a>
16   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
17   * @param <T> the value type
18   */
19  public interface TextSerializer<T>
20  {
21      /**
22       * Serialize a value to text in such a way that it can be deserialized with the corresponding deserializer.
23   * @param value Object; the value to serialize
24  
25       * @return String; a string representation of the value that can later be deserialized
26       */
27      String serialize(Object value);
28  
29      /**
30       * Deserialize a value from text that has been created with the corresponding serializer.
31       * @param text String; the string to deserialize
32       * @return T; an instance of the object created with the corresponding serializer
33       */
34      Object deserialize(String text);
35  
36      /**
37       * Resolve the correct (de)serializer for the given class, and return an instance of the (de)serializer.
38       * @param valueClass Class&lt;?&gt;; the class to resolve the (de)serializer for
39       * @return an instance of the correct (de)serializer
40       * @throws TextSerializationException when there is no corresponding (de)serializer for the class
41       */
42      @SuppressWarnings("rawtypes")
43      static TextSerializer<?> resolve(final Class<?> valueClass) throws TextSerializationException
44      {
45          Throw.whenNull(valueClass, "valueClass cannot be null");
46          if (valueClass.isPrimitive())
47          {
48              if (valueClass.equals(int.class))
49              {
50                  return new PrimitiveSerializer.Int();
51              }
52              else if (valueClass.equals(double.class))
53              {
54                  return new PrimitiveSerializer.Double();
55              }
56              else if (valueClass.equals(float.class))
57              {
58                  return new PrimitiveSerializer.Float();
59              }
60              else if (valueClass.equals(long.class))
61              {
62                  return new PrimitiveSerializer.Long();
63              }
64              else if (valueClass.equals(short.class))
65              {
66                  return new PrimitiveSerializer.Short();
67              }
68              else if (valueClass.equals(byte.class))
69              {
70                  return new PrimitiveSerializer.Byte();
71              }
72              else if (valueClass.equals(boolean.class))
73              {
74                  return new PrimitiveSerializer.Boolean();
75              }
76              else if (valueClass.equals(char.class))
77              {
78                  return new PrimitiveSerializer.Char();
79              }
80          }
81  
82          else if (Number.class.isAssignableFrom(valueClass))
83          {
84              if (valueClass.equals(Integer.class))
85              {
86                  return new IntegerSerializer();
87              }
88              else if (valueClass.equals(Double.class))
89              {
90                  return new DoubleSerializer();
91              }
92              else if (valueClass.equals(Float.class))
93              {
94                  return new FloatSerializer();
95              }
96              else if (valueClass.equals(Long.class))
97              {
98                  return new LongSerializer();
99              }
100             else if (valueClass.equals(Short.class))
101             {
102                 return new ShortSerializer();
103             }
104             else if (valueClass.equals(Byte.class))
105             {
106                 return new ByteSerializer();
107             }
108             else if (DoubleScalarInterface.class.isAssignableFrom(valueClass)) // Scalar is a Number
109             {
110                 return new DoubleScalarSerializer();
111             }
112             else if (FloatScalarInterface.class.isAssignableFrom(valueClass)) // Scalar is a Number
113             {
114                 return new FloatScalarSerializer();
115             }
116         }
117 
118         else if (valueClass.equals(Boolean.class))
119         {
120             return new BooleanSerializer();
121         }
122 
123         else if (valueClass.equals(Character.class))
124         {
125             return new CharacterSerializer();
126         }
127 
128         else if (valueClass.equals(String.class))
129         {
130             return new StringSerializer();
131         }
132 
133         throw new TextSerializationException("Cannot resolve the Text(se)serializer for class " + valueClass.getName());
134     }
135 }