|
1 | 1 | package net.lecousin.framework.io.serialization;
|
2 | 2 |
|
| 3 | +import java.io.InputStream; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.Collection; |
3 | 6 | import java.util.List;
|
| 7 | +import java.util.Map; |
4 | 8 |
|
5 | 9 | import net.lecousin.framework.concurrent.Task;
|
6 | 10 | import net.lecousin.framework.concurrent.synch.ISynchronizationPoint;
|
|
9 | 13 | import net.lecousin.framework.io.IO;
|
10 | 14 | import net.lecousin.framework.io.serialization.SerializationClass.Attribute;
|
11 | 15 | import net.lecousin.framework.io.serialization.SerializationContext.AttributeContext;
|
| 16 | +import net.lecousin.framework.io.serialization.SerializationContext.CollectionContext; |
12 | 17 | import net.lecousin.framework.io.serialization.SerializationContext.ObjectContext;
|
| 18 | +import net.lecousin.framework.io.serialization.SerializationUtil.MapEntry; |
13 | 19 | import net.lecousin.framework.io.serialization.annotations.AttributeAnnotationToRuleOnAttribute;
|
14 | 20 | import net.lecousin.framework.io.serialization.annotations.AttributeAnnotationToRuleOnType;
|
15 | 21 | import net.lecousin.framework.io.serialization.annotations.TypeAnnotationToRule;
|
@@ -73,43 +79,129 @@ protected List<SerializationRule> addRulesForAttribute(Attribute a, List<Seriali
|
73 | 79 | protected ISynchronizationPoint<? extends Exception> specifyValue(
|
74 | 80 | SerializationContext context, TypeDefinition typeDef, List<SerializationRule> rules
|
75 | 81 | ) {
|
| 82 | + for (SerializationRule rule : rules) |
| 83 | + typeDef = rule.getDeserializationType(typeDef, context); |
| 84 | + |
76 | 85 | Class<?> type = typeDef.getBase();
|
77 | 86 |
|
| 87 | + if (type.isArray()) { |
| 88 | + Class<?> elementType = type.getComponentType(); |
| 89 | + CollectionContext ctx = new CollectionContext(context, null, typeDef, new TypeDefinition(elementType)); |
| 90 | + return specifyCollectionValue(ctx, rules); |
| 91 | + } |
| 92 | + |
78 | 93 | if (boolean.class.equals(type))
|
79 |
| - return specifyBooleanValue(false); |
| 94 | + return specifyBooleanValue(context, false); |
80 | 95 | if (Boolean.class.equals(type))
|
81 |
| - return specifyBooleanValue(true); |
| 96 | + return specifyBooleanValue(context, true); |
82 | 97 |
|
83 | 98 | if (byte.class.equals(type))
|
84 |
| - return specifyNumericValue(Byte.class, false, Byte.valueOf(Byte.MIN_VALUE), Byte.valueOf(Byte.MAX_VALUE)); |
| 99 | + return specifyNumericValue(context, Byte.class, false, Byte.valueOf(Byte.MIN_VALUE), Byte.valueOf(Byte.MAX_VALUE)); |
85 | 100 | if (Byte.class.equals(type))
|
86 |
| - return specifyNumericValue(Byte.class, true, Byte.valueOf(Byte.MIN_VALUE), Byte.valueOf(Byte.MAX_VALUE)); |
| 101 | + return specifyNumericValue(context, Byte.class, true, Byte.valueOf(Byte.MIN_VALUE), Byte.valueOf(Byte.MAX_VALUE)); |
87 | 102 | if (short.class.equals(type))
|
88 |
| - return specifyNumericValue(Short.class, false, Short.valueOf(Short.MIN_VALUE), Short.valueOf(Short.MAX_VALUE)); |
| 103 | + return specifyNumericValue(context, Short.class, false, Short.valueOf(Short.MIN_VALUE), Short.valueOf(Short.MAX_VALUE)); |
89 | 104 | if (Short.class.equals(type))
|
90 |
| - return specifyNumericValue(Short.class, true, Short.valueOf(Short.MIN_VALUE), Short.valueOf(Short.MAX_VALUE)); |
| 105 | + return specifyNumericValue(context, Short.class, true, Short.valueOf(Short.MIN_VALUE), Short.valueOf(Short.MAX_VALUE)); |
91 | 106 | if (int.class.equals(type))
|
92 |
| - return specifyNumericValue(Integer.class, false, Integer.valueOf(Integer.MIN_VALUE), Integer.valueOf(Integer.MAX_VALUE)); |
| 107 | + return specifyNumericValue(context, Integer.class, false, |
| 108 | + Integer.valueOf(Integer.MIN_VALUE), Integer.valueOf(Integer.MAX_VALUE)); |
93 | 109 | if (Integer.class.equals(type))
|
94 |
| - return specifyNumericValue(Integer.class, true, Integer.valueOf(Integer.MIN_VALUE), Integer.valueOf(Integer.MAX_VALUE)); |
| 110 | + return specifyNumericValue(context, Integer.class, true, |
| 111 | + Integer.valueOf(Integer.MIN_VALUE), Integer.valueOf(Integer.MAX_VALUE)); |
95 | 112 | if (long.class.equals(type))
|
96 |
| - return specifyNumericValue(Long.class, false, Long.valueOf(Long.MIN_VALUE), Long.valueOf(Long.MAX_VALUE)); |
| 113 | + return specifyNumericValue(context, Long.class, false, Long.valueOf(Long.MIN_VALUE), Long.valueOf(Long.MAX_VALUE)); |
97 | 114 | if (Long.class.equals(type))
|
98 |
| - return specifyNumericValue(Long.class, true, Long.valueOf(Long.MIN_VALUE), Long.valueOf(Long.MAX_VALUE)); |
| 115 | + return specifyNumericValue(context, Long.class, true, Long.valueOf(Long.MIN_VALUE), Long.valueOf(Long.MAX_VALUE)); |
| 116 | + if (float.class.equals(type) || |
| 117 | + double.class.equals(type) || |
| 118 | + Number.class.isAssignableFrom(type)) |
| 119 | + return specifyNumericValue(context, type, !type.isPrimitive(), null, null); |
| 120 | + |
| 121 | + if (char.class.equals(type)) |
| 122 | + return specifyCharacterValue(context, false); |
| 123 | + if (Character.class.equals(type)) |
| 124 | + return specifyCharacterValue(context, true); |
99 | 125 |
|
100 | 126 | if (CharSequence.class.isAssignableFrom(type))
|
101 | 127 | return specifyStringValue(context, typeDef);
|
| 128 | + |
| 129 | + if (type.isEnum()) |
| 130 | + return specifyEnumValue(context, typeDef); |
| 131 | + |
| 132 | + if (Collection.class.isAssignableFrom(type)) { |
| 133 | + TypeDefinition elementType; |
| 134 | + if (typeDef.getParameters().isEmpty()) |
| 135 | + elementType = null; |
| 136 | + else |
| 137 | + elementType = typeDef.getParameters().get(0); |
| 138 | + CollectionContext ctx = new CollectionContext(context, null, typeDef, elementType); |
| 139 | + return specifyCollectionValue(ctx, rules); |
| 140 | + } |
102 | 141 |
|
103 |
| - // TODO |
| 142 | + if (Map.class.isAssignableFrom(type)) |
| 143 | + return specifyMapValue(context, typeDef, rules); |
| 144 | + |
| 145 | + if (InputStream.class.isAssignableFrom(type)) |
| 146 | + return specifyInputStreamValue(context, rules); |
| 147 | + |
| 148 | + if (IO.Readable.class.isAssignableFrom(type)) |
| 149 | + return specifyIOReadableValue(context, rules); |
104 | 150 |
|
105 | 151 | return specifyObjectValue(context, typeDef, rules);
|
106 | 152 | }
|
107 | 153 |
|
108 |
| - protected abstract ISynchronizationPoint<? extends Exception> specifyBooleanValue(boolean nullable); |
| 154 | + // *** boolean *** |
| 155 | + |
| 156 | + protected abstract ISynchronizationPoint<? extends Exception> specifyBooleanValue(SerializationContext context, boolean nullable); |
| 157 | + |
| 158 | + // *** number *** |
109 | 159 |
|
110 |
| - protected abstract ISynchronizationPoint<? extends Exception> specifyNumericValue(Class<?> type, boolean nullable, Number min, Number max); |
| 160 | + protected abstract ISynchronizationPoint<? extends Exception> specifyNumericValue( |
| 161 | + SerializationContext context, Class<?> type, boolean nullable, Number min, Number max); |
| 162 | + |
| 163 | + // *** character *** |
| 164 | + |
| 165 | + protected abstract ISynchronizationPoint<? extends Exception> specifyCharacterValue(SerializationContext context, boolean nullable); |
| 166 | + |
| 167 | + // *** string *** |
111 | 168 |
|
112 | 169 | protected abstract ISynchronizationPoint<? extends Exception> specifyStringValue(SerializationContext context, TypeDefinition type);
|
| 170 | + |
| 171 | + // *** enum *** |
| 172 | + |
| 173 | + protected abstract ISynchronizationPoint<? extends Exception> specifyEnumValue(SerializationContext context, TypeDefinition type); |
| 174 | + |
| 175 | + // *** collection *** |
| 176 | + |
| 177 | + protected abstract ISynchronizationPoint<? extends Exception> specifyCollectionValue( |
| 178 | + CollectionContext context, List<SerializationRule> rules); |
| 179 | + |
| 180 | + // *** map *** |
| 181 | + |
| 182 | + protected ISynchronizationPoint<? extends Exception> specifyMapValue( |
| 183 | + SerializationContext context, TypeDefinition type, List<SerializationRule> rules |
| 184 | + ) { |
| 185 | + TypeDefinition elementType = new TypeDefinition(MapEntry.class, type.getParameters()); |
| 186 | + TypeDefinition colType = new TypeDefinition(ArrayList.class, elementType); |
| 187 | + CollectionContext ctx = new CollectionContext(context, null, colType, elementType); |
| 188 | + return specifyCollectionValue(ctx, rules); |
| 189 | + } |
| 190 | + |
| 191 | + // *** InputStream *** |
| 192 | + |
| 193 | + protected ISynchronizationPoint<? extends Exception> specifyInputStreamValue( |
| 194 | + SerializationContext context, List<SerializationRule> rules |
| 195 | + ) { |
| 196 | + return specifyIOReadableValue(context, rules); |
| 197 | + } |
| 198 | + |
| 199 | + // *** IO.Readable *** |
| 200 | + |
| 201 | + protected abstract ISynchronizationPoint<? extends Exception> specifyIOReadableValue( |
| 202 | + SerializationContext context, List<SerializationRule> rules); |
| 203 | + |
| 204 | + // *** object *** |
113 | 205 |
|
114 | 206 | protected ISynchronizationPoint<? extends Exception> specifyObjectValue(
|
115 | 207 | SerializationContext context, TypeDefinition typeDef, List<SerializationRule> rules
|
|
0 commit comments