2
2
3
3
import com .google .gson .Gson ;
4
4
import com .google .gson .GsonBuilder ;
5
+ import com .google .gson .JsonSyntaxException ;
5
6
import org .junit .Test ;
6
7
import org .tensorics .core .lang .Tensorics ;
7
8
import org .tensorics .core .tensor .Position ;
8
9
import org .tensorics .core .tensor .Tensor ;
9
10
import org .tensorics .core .tensorbacked .AbstractTensorbacked ;
10
11
import org .tensorics .core .tensorbacked .annotation .Dimensions ;
12
+ import org .tensorics .core .tensorbacked .dimtyped .Tensorbacked1d ;
11
13
import org .tensorics .core .tensorbacked .dimtyped .Tensorbacked2d ;
12
14
import org .tensorics .core .tensorbacked .dimtyped .TensorbackedScalar ;
13
15
16
+ import java .util .Objects ;
17
+
14
18
import static org .assertj .core .api .Assertions .assertThat ;
19
+ import static org .assertj .core .api .Assertions .assertThatThrownBy ;
15
20
import static org .tensorics .core .lang .Tensorics .at ;
16
21
import static org .tensorics .core .lang .Tensorics .sizeOf ;
17
22
@@ -26,21 +31,33 @@ public class TensorbackedGsonAdapterTest {
26
31
27
32
private static final String JSON_STRING = "{\" A\" :{\" 1\" :0.11,\" 2\" :0.12},\" B\" :{\" 1\" :0.21,\" 2\" :0.22}}" ;
28
33
29
- private final Gson gson = new GsonBuilder ()//
30
- .registerTypeAdapterFactory (new TensorbackedGsonAdapterFactory ())//
34
+ private final Gson simpleGson = new GsonBuilder ()//
35
+ .registerTypeAdapterFactory (TensorbackedGsonAdapter .FACTORY )//
36
+ .create ();
37
+
38
+ private static final AComplexCoordTensorbacked COMPLEX_COORD_TB = Tensorics .builderFor (AComplexCoordTensorbacked .class )//
39
+ .put (at (new Pair ("a1" , "b1" )), 0.11 )//
40
+ .put (at (new Pair ("a2" , "b2" )), 0.22 )//
41
+ .build ();
42
+ private static final String COMPLEX_COORD_JSON_STRING = "[[{\" a\" :\" a1\" ,\" b\" :\" b1\" },0.11],[{\" a\" :\" a2\" ,\" b\" :\" b2\" },0.22]]" ;
43
+
44
+ private final Gson complexMapKeyGson = new GsonBuilder ()//
45
+ .registerTypeAdapterFactory (TensorbackedGsonAdapter .FACTORY )//
46
+ .enableComplexMapKeySerialization () //
31
47
.create ();
32
48
49
+
33
50
@ Test
34
51
public void simpleTensorSerializationIsOk () {
35
- String string = gson .toJson (TENSORBACKED );
52
+ String string = simpleGson .toJson (TENSORBACKED );
36
53
assertThat (string ).isNotNull ();
37
54
assertThat (string ).isNotEmpty ();
38
55
assertThat (string ).isEqualTo (JSON_STRING );
39
56
}
40
57
41
58
@ Test
42
59
public void simpleTensorDeserializationIsOk () {
43
- AnInheritedTensorbacked val = gson .fromJson (JSON_STRING , AnInheritedTensorbacked .class );
60
+ AnInheritedTensorbacked val = simpleGson .fromJson (JSON_STRING , AnInheritedTensorbacked .class );
44
61
assertThat (val ).isNotNull ();
45
62
assertThat (val ).isEqualTo (TENSORBACKED );
46
63
}
@@ -49,15 +66,15 @@ public void simpleTensorDeserializationIsOk() {
49
66
public void simpleScalarSerializationIsOk () {
50
67
AScalarBacked val = Tensorics .builderForScalar (AScalarBacked .class ).put (0.33 ).build ();
51
68
52
- String string = gson .toJson (val );
69
+ String string = simpleGson .toJson (val );
53
70
assertThat (string ).isNotNull ();
54
71
assertThat (string ).isNotEmpty ();
55
72
assertThat (string ).isEqualTo ("0.33" );
56
73
}
57
74
58
75
@ Test
59
76
public void simpleScalarDeserializationIsOk () {
60
- AScalarBacked val = gson .fromJson ("0.33" , AScalarBacked .class );
77
+ AScalarBacked val = simpleGson .fromJson ("0.33" , AScalarBacked .class );
61
78
assertThat (val ).isNotNull ();
62
79
assertThat (sizeOf (val )).isEqualTo (1 );
63
80
assertThat (val .get ()).isEqualTo (0.33 );
@@ -71,7 +88,7 @@ public void simpleScalarDeserializationIsOk() {
71
88
@ Test
72
89
public void deserializationIntoDifferentTbWorks () {
73
90
/* This shows cross-deserialization: Into another tensoribacked with the same dimensions.*/
74
- AnInterfaceTensorbacked val = gson .fromJson (JSON_STRING , AnInterfaceTensorbacked .class );
91
+ AnInterfaceTensorbacked val = simpleGson .fromJson (JSON_STRING , AnInterfaceTensorbacked .class );
75
92
assertThat (val ).isNotNull ();
76
93
77
94
/* The tensorbacked objects are not equal in this case ....*/
@@ -82,7 +99,7 @@ public void deserializationIntoDifferentTbWorks() {
82
99
}
83
100
84
101
/**
85
- * The context is not serialized or deserialzed currently. This is demonstrated here.
102
+ * The context is not serialized or deserialized currently. This is demonstrated here.
86
103
* Can be discussed, if this is good behaviour.... tricky to change anyhow, as the context
87
104
* dimensions are not well defined by a tensorbacked.
88
105
*/
@@ -93,8 +110,8 @@ public void contextIsNotSerialized() {
93
110
.context (Position .of (AB .A ))//
94
111
.build ();
95
112
96
- String string = gson .toJson (tbWithContext );
97
- AnInheritedTensorbacked deserialized = gson .fromJson (string , AnInheritedTensorbacked .class );
113
+ String string = simpleGson .toJson (tbWithContext );
114
+ AnInheritedTensorbacked deserialized = simpleGson .fromJson (string , AnInheritedTensorbacked .class );
98
115
99
116
/* As the context is currently neither serialized nor deserialized,
100
117
the equality to the original object does currently not hold.*/
@@ -105,6 +122,32 @@ public void contextIsNotSerialized() {
105
122
assertThat (deserialized ).isEqualTo (TENSORBACKED );
106
123
}
107
124
125
+ @ Test
126
+ public void complexCoordinateNotSupportedPerDefault () {
127
+ String string = simpleGson .toJson (COMPLEX_COORD_TB );
128
+
129
+ /* This cannot be deserialized anymore, as the simple result of "toString" was put as key...*/
130
+ assertThatThrownBy (() -> simpleGson .fromJson (string , AComplexCoordTensorbacked .class )) //
131
+ .isInstanceOf (JsonSyntaxException .class ) //
132
+ .hasMessageContaining ("Expected BEGIN_OBJECT but was STRING" );
133
+ }
134
+
135
+ @ Test
136
+ public void complexCoordinateSerializationWithComplexMapKeySupport () {
137
+ String string = complexMapKeyGson .toJson (COMPLEX_COORD_TB );
138
+ System .out .println (string );
139
+ assertThat (string ).isEqualTo (COMPLEX_COORD_JSON_STRING );
140
+
141
+ /* deserialization works, no matter if the flag is set or not */
142
+ AComplexCoordTensorbacked deserialized = simpleGson .fromJson (string , AComplexCoordTensorbacked .class );
143
+ }
144
+
145
+ @ Test
146
+ public void complexCoordinateDeserializationIsOk () {
147
+ /* deserialization works with any gson (as it is an if in the adapter) */
148
+ AComplexCoordTensorbacked deserialized = simpleGson .fromJson (COMPLEX_COORD_JSON_STRING , AComplexCoordTensorbacked .class );
149
+ assertThat (deserialized ).isEqualTo (COMPLEX_COORD_TB );
150
+ }
108
151
109
152
public interface AScalarBacked extends TensorbackedScalar <Double > {
110
153
@@ -124,8 +167,43 @@ public static interface AnInterfaceTensorbacked extends Tensorbacked2d<String, I
124
167
125
168
}
126
169
170
+ public static interface AComplexCoordTensorbacked extends Tensorbacked1d <Pair , Double > {
171
+
172
+ }
173
+
127
174
public static enum AB {
128
175
A , B
129
176
}
130
177
178
+ public static class Pair {
179
+ public final String a ;
180
+ public final String b ;
181
+
182
+ public Pair (String a , String b ) {
183
+ this .a = a ;
184
+ this .b = b ;
185
+ }
186
+
187
+ @ Override
188
+ public boolean equals (Object o ) {
189
+ if (this == o ) return true ;
190
+ if (o == null || getClass () != o .getClass ()) return false ;
191
+ Pair pair = (Pair ) o ;
192
+ return Objects .equals (a , pair .a ) && Objects .equals (b , pair .b );
193
+ }
194
+
195
+ @ Override
196
+ public int hashCode () {
197
+ return Objects .hash (a , b );
198
+ }
199
+
200
+ @ Override
201
+ public String toString () {
202
+ return "Pair{" +
203
+ "a='" + a + '\'' +
204
+ ", b='" + b + '\'' +
205
+ '}' ;
206
+ }
207
+ }
208
+
131
209
}
0 commit comments