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-2021 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       * @return String; a string representation of the value that can later be deserialized
25       */
26      String serialize(Object value);
27  
28      /**
29       * Deserialize a value from text that has been created with the corresponding serializer.
30       * @param text String; the string to deserialize
31       * @return T; an instance of the object created with the corresponding serializer
32       */
33      Object deserialize(String text);
34  
35      /**
36       * Resolve the correct (de)serializer for the given class, and return an instance of the (de)serializer.
37       * @param valueClass Class&lt;?&gt;; the class to resolve the (de)serializer for
38       * @return an instance of the correct (de)serializer
39       * @throws TextSerializationException when there is no corresponding (de)serializer for the class
40       */
41      @SuppressWarnings("rawtypes")
42      static TextSerializer<?> resolve(final Class<?> valueClass) throws TextSerializationException
43      {
44          Throw.whenNull(valueClass, "valueClass cannot be null");
45          if (valueClass.isPrimitive())
46          {
47              if (valueClass.equals(int.class))
48              {
49                  return new PrimitiveSerializer.Int();
50              }
51              else if (valueClass.equals(double.class))
52              {
53                  return new PrimitiveSerializer.Double();
54              }
55              else if (valueClass.equals(float.class))
56              {
57                  return new PrimitiveSerializer.Float();
58              }
59              else if (valueClass.equals(long.class))
60              {
61                  return new PrimitiveSerializer.Long();
62              }
63              else if (valueClass.equals(short.class))
64              {
65                  return new PrimitiveSerializer.Short();
66              }
67              else if (valueClass.equals(byte.class))
68              {
69                  return new PrimitiveSerializer.Byte();
70              }
71              else if (valueClass.equals(boolean.class))
72              {
73                  return new PrimitiveSerializer.Boolean();
74              }
75              else if (valueClass.equals(char.class))
76              {
77                  return new PrimitiveSerializer.Char();
78              }
79          }
80  
81          else if (Number.class.isAssignableFrom(valueClass))
82          {
83              if (valueClass.equals(Integer.class))
84              {
85                  return new IntegerSerializer();
86              }
87              else if (valueClass.equals(Double.class))
88              {
89                  return new DoubleSerializer();
90              }
91              else if (valueClass.equals(Float.class))
92              {
93                  return new FloatSerializer();
94              }
95              else if (valueClass.equals(Long.class))
96              {
97                  return new LongSerializer();
98              }
99              else if (valueClass.equals(Short.class))
100             {
101                 return new ShortSerializer();
102             }
103             else if (valueClass.equals(Byte.class))
104             {
105                 return new ByteSerializer();
106             }
107             else if (DoubleScalarInterface.class.isAssignableFrom(valueClass)) // Scalar is a Number
108             {
109                 return new DoubleScalarSerializer();
110             }
111             else if (FloatScalarInterface.class.isAssignableFrom(valueClass)) // Scalar is a Number
112             {
113                 return new FloatScalarSerializer();
114             }
115         }
116 
117         else if (valueClass.equals(Boolean.class))
118         {
119             return new BooleanSerializer();
120         }
121 
122         else if (valueClass.equals(Character.class))
123         {
124             return new CharacterSerializer();
125         }
126 
127         else if (valueClass.equals(String.class))
128         {
129             return new StringSerializer();
130         }
131 
132         throw new TextSerializationException("Cannot resolve the Text(se)serializer for class " + valueClass.getName());
133     }
134 }