5
5
import codes .laivy .serializable .adapter .Adapter ;
6
6
import codes .laivy .serializable .config .Config ;
7
7
import codes .laivy .serializable .context .*;
8
+ import codes .laivy .serializable .exception .IncompatibleReferenceException ;
8
9
import codes .laivy .serializable .factory .context .ContextFactory ;
9
10
import codes .laivy .serializable .utilities .Classes ;
10
11
import com .google .gson .*;
11
12
import org .jetbrains .annotations .NotNull ;
12
13
import org .jetbrains .annotations .Nullable ;
13
14
14
- import java .io .EOFException ;
15
15
import java .io .IOException ;
16
16
import java .lang .reflect .Array ;
17
17
@@ -30,8 +30,6 @@ public final class JsonSerializer extends AbstractTypeSerializer<JsonElement> {
30
30
31
31
@ Override
32
32
public @ NotNull JsonElement serialize (@ Nullable Object object , @ NotNull Config config ) {
33
- @ NotNull ContextFactory contextFactory = config .getContextFactory ();
34
-
35
33
// Check nullability
36
34
if (object == null ) {
37
35
return JsonNull .INSTANCE ;
@@ -42,9 +40,10 @@ public final class JsonSerializer extends AbstractTypeSerializer<JsonElement> {
42
40
// Adapters
43
41
@ NotNull Class <?> reference = object .getClass ();
44
42
@ Nullable Adapter adapter = config .getAdapter ();
43
+ @ NotNull ContextFactory contextFactory = config .getContextFactory ();
45
44
46
45
if (adapter != null ) {
47
- return serialize ( adapter . write ( object , this , config )) ;
46
+ contextFactory = adapter ;
48
47
}
49
48
50
49
// Serialize
@@ -60,48 +59,39 @@ public final class JsonSerializer extends AbstractTypeSerializer<JsonElement> {
60
59
61
60
// Deserialization
62
61
63
- @ SuppressWarnings ("unchecked" )
64
62
@ Override
65
- public < E > @ Nullable E deserialize (@ NotNull Class <E > reference , @ NotNull Context context , @ NotNull Config config ) {
63
+ public @ Nullable Object deserializeUnsafe (@ NotNull Class <? > reference , @ NotNull Context context , @ NotNull Config config ) throws IncompatibleReferenceException {
66
64
// Start deserialization with compatible reference
67
65
if (!Classes .isConcrete (reference )) {
68
66
throw new IllegalArgumentException ("the references should be all concretes: '" + reference .getName () + "'" );
69
67
}
70
68
71
- // Adapters
72
- @ Nullable Adapter adapter = config .getAdapter ();
73
-
74
- if (adapter != null ) try {
75
- return (E ) adapter .read (reference , this , context , config );
76
- } catch (@ NotNull EOFException e ) {
77
- // todo: exception message
78
- throw new RuntimeException (e );
79
- }
80
-
81
- if (context .isNull ()) {
82
- return null ;
83
- }
69
+ // Adapters and factory
70
+ @ NotNull ContextFactory factory ;
84
71
85
- // Factory
86
- @ NotNull ContextFactory factory = config .getContextFactory ();
72
+ if ( config . getAdapter () != null ) factory = config . getAdapter ();
73
+ else factory = config .getContextFactory ();
87
74
88
75
// Deserialize with factory
89
76
try {
90
- @ Nullable Object deserialized = factory .read (reference , this , context , config );
91
-
92
- // todo: check type
93
- return (E ) deserialized ;
77
+ return factory .read (reference , this , context , config );
94
78
} catch (@ NotNull IOException e ) {
95
79
throw new RuntimeException (e );
96
80
} catch (@ NotNull InstantiationException e ) {
97
81
throw new RuntimeException ("cannot instantiate '" + reference .getName () + "'" , e );
98
82
}
99
83
}
84
+
100
85
@ Override
101
86
public <E > @ Nullable E deserialize (@ NotNull Class <E > reference , @ Nullable JsonElement element , @ NotNull Config config ) {
102
87
return deserialize (reference , toContext (element ), config );
103
88
}
104
89
90
+ @ Override
91
+ public @ Nullable Object deserializeUnsafe (@ NotNull Class <?> reference , @ Nullable JsonElement element , @ NotNull Config config ) throws IncompatibleReferenceException {
92
+ return deserializeUnsafe (reference , toContext (element ), config );
93
+ }
94
+
105
95
// Context
106
96
107
97
@ Override
@@ -110,9 +100,10 @@ public final class JsonSerializer extends AbstractTypeSerializer<JsonElement> {
110
100
return NullContext .create ();
111
101
} else {
112
102
@ NotNull Class <?> reference = object .getClass ();
103
+ @ NotNull ContextFactory contextFactory ;
113
104
114
105
if (adapters .map .containsKey (reference )) {
115
- return adapters .map .get (reference ). write ( object , this , config );
106
+ contextFactory = adapters .map .get (reference );
116
107
} else if (object instanceof Context ) {
117
108
throw new IllegalArgumentException ("you cannot convert a context into a context" );
118
109
} else if (object instanceof Enum <?>) {
@@ -146,15 +137,17 @@ public final class JsonSerializer extends AbstractTypeSerializer<JsonElement> {
146
137
147
138
return context ;
148
139
} else {
149
- @ Nullable ContextFactory factory = config .getContextFactory ();
150
- @ Nullable Object instance = factory .write (reference , object , this , config );
151
-
152
- if (instance instanceof Context ) {
153
- return (Context ) instance ;
154
- } else {
155
- // Repeat recursively the serialization
156
- return toContext (instance );
157
- }
140
+ contextFactory = config .getContextFactory ();
141
+ }
142
+
143
+ // Generate using context factory
144
+ @ Nullable Object instance = contextFactory .write (reference , object , this , config );
145
+
146
+ if (instance instanceof Context ) {
147
+ return (Context ) instance ;
148
+ } else {
149
+ // Repeat recursively the serialization
150
+ return toContext (instance );
158
151
}
159
152
}
160
153
}
0 commit comments