5
5
import codes .laivy .serializable .config .Config ;
6
6
import codes .laivy .serializable .context .*;
7
7
import codes .laivy .serializable .exception .IncompatibleReferenceException ;
8
+ import codes .laivy .serializable .factory .context .ContextFactory ;
8
9
import com .google .gson .*;
9
10
import org .jetbrains .annotations .NotNull ;
10
11
import org .jetbrains .annotations .Nullable ;
11
12
12
13
import java .io .IOException ;
14
+ import java .util .Objects ;
13
15
14
16
public final class JsonSerializer extends AbstractTypeSerializer <JsonElement > {
15
17
@@ -40,17 +42,46 @@ public final class JsonSerializer extends AbstractTypeSerializer<JsonElement> {
40
42
return serialize ((Context ) response );
41
43
} else {
42
44
// Serialize again
43
- return serialize (response );
45
+ return Objects . requireNonNull ( serialize (response ), "serialized response returned an unexpected null" );
44
46
}
45
47
}
46
48
47
49
// Deserialization
48
50
49
51
@ Override
50
52
public @ Nullable Object deserializeUnsafe (@ NotNull Class <?> reference , @ NotNull Context context , @ NotNull Config config ) throws IncompatibleReferenceException {
51
- // Deserialize with factory
52
- try {
53
- return config .getContextFactory ().read (reference , this , context , config );
53
+ if (reference == Context .class ) {
54
+ return context ;
55
+ } else if (MapContext .class .isAssignableFrom (reference )) {
56
+ if (!context .isMap ()) {
57
+ throw new IncompatibleReferenceException ("to deserialize a map context the context must be a map: " + context );
58
+ }
59
+
60
+ return context .getAsMap ();
61
+ } else if (ArrayContext .class .isAssignableFrom (reference )) {
62
+ if (!context .isArray ()) {
63
+ throw new IncompatibleReferenceException ("to deserialize an array context the context must be an array: " + context );
64
+ }
65
+
66
+ return context .getAsArray ();
67
+ } else if (PrimitiveContext .class .isAssignableFrom (reference )) {
68
+ if (!context .isPrimitive ()) {
69
+ throw new IncompatibleReferenceException ("to deserialize a primitive context the context must be a primitive: " + context );
70
+ }
71
+
72
+ return context .isPrimitive ();
73
+ } else if (NullContext .class .isAssignableFrom (reference )) {
74
+ if (!context .isNull ()) {
75
+ throw new IncompatibleReferenceException ("to deserialize a null context the context must be a null: " + context );
76
+ }
77
+
78
+ return context .getAsNull ();
79
+ } else if (Context .class .isAssignableFrom (reference )) {
80
+ throw new UnsupportedOperationException ("illegal context type '" + reference + "'. You should only use Context, ArrayContext, MapContext, PrimitiveContext or NullContext" );
81
+ } else try {
82
+ // Deserialize with factory
83
+ @ NotNull ContextFactory factory = config .getContextFactory ();
84
+ return factory .read (reference , this , context , config );
54
85
} catch (@ NotNull IOException e ) {
55
86
throw new RuntimeException (e );
56
87
} catch (@ NotNull InstantiationException e ) {
@@ -70,7 +101,8 @@ public final class JsonSerializer extends AbstractTypeSerializer<JsonElement> {
70
101
return NullContext .create ();
71
102
} else {
72
103
// Generate using context factory
73
- @ Nullable Object instance = config .getContextFactory ().write (object .getClass (), object , this , config );
104
+ @ NotNull ContextFactory factory = config .getContextFactory ();
105
+ @ Nullable Object instance = factory .write (object .getClass (), object , this , config );
74
106
75
107
if (instance instanceof Context ) {
76
108
return (Context ) instance ;
0 commit comments