|
5 | 5 |
|
6 | 6 | import io.clientcore.core.http.models.HttpMethod; |
7 | 7 | import io.clientcore.core.implementation.AccessibleByteArrayOutputStream; |
| 8 | +import io.clientcore.core.implementation.TypeUtil; |
8 | 9 | import io.clientcore.core.implementation.utils.JsonSerializer; |
9 | 10 | import io.clientcore.core.models.SimpleClass; |
10 | 11 | import io.clientcore.core.serialization.json.JsonReader; |
|
20 | 21 | import java.io.IOException; |
21 | 22 | import java.io.InputStream; |
22 | 23 | import java.lang.reflect.InvocationTargetException; |
| 24 | +import java.lang.reflect.ParameterizedType; |
23 | 25 | import java.net.URI; |
24 | 26 | import java.net.URL; |
25 | 27 | import java.nio.charset.StandardCharsets; |
26 | 28 | import java.time.OffsetDateTime; |
27 | 29 | import java.time.ZoneOffset; |
28 | 30 | import java.util.Collections; |
29 | 31 | import java.util.HashMap; |
| 32 | +import java.util.LinkedHashMap; |
| 33 | +import java.util.List; |
30 | 34 | import java.util.Map; |
31 | 35 | import java.util.stream.Stream; |
32 | 36 |
|
33 | 37 | import static io.clientcore.core.utils.TestUtils.assertArraysEqual; |
34 | 38 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 39 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
35 | 40 | import static org.junit.jupiter.api.Assertions.assertNull; |
36 | 41 | import static org.junit.jupiter.api.Assertions.assertThrows; |
| 42 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
37 | 43 |
|
38 | 44 | public class JsonSerializerTests { |
39 | 45 | private static final ObjectSerializer SERIALIZER = new JsonSerializer(); |
@@ -266,4 +272,91 @@ private static Stream<Arguments> unsupportedDeserializationSupplier() { |
266 | 272 | Arguments.of(URI.class, IOException.class) // Thrown when the String cannot be parsed by core |
267 | 273 | ); |
268 | 274 | } |
| 275 | + |
| 276 | + @Test |
| 277 | + public void deserializeListOfJsonSerializableTypes() throws IOException { |
| 278 | + byte[] bytes = "[{\"property\":\"value1\"},{\"property\":\"value2\"}]".getBytes(StandardCharsets.UTF_8); |
| 279 | + |
| 280 | + ParameterizedType type = TypeUtil.createParameterizedType(List.class, FooModel.class); |
| 281 | + |
| 282 | + List<FooModel> models = SERIALIZER.deserializeFromBytes(bytes, type); |
| 283 | + assertNotNull(models); |
| 284 | + assertEquals(2, models.size()); |
| 285 | + assertEquals("value1", models.get(0).getProperty()); |
| 286 | + assertEquals("value2", models.get(1).getProperty()); |
| 287 | + } |
| 288 | + |
| 289 | + @SuppressWarnings("unchecked") |
| 290 | + @Test |
| 291 | + public void deserializeListOfNonJsonSerializableTypes() throws IOException { |
| 292 | + byte[] bytes = "[{\"property\":\"value1\"},{\"property\":\"value2\"}]".getBytes(StandardCharsets.UTF_8); |
| 293 | + |
| 294 | + ParameterizedType type = TypeUtil.createParameterizedType(List.class, BarModel.class); |
| 295 | + |
| 296 | + List<?> models = SERIALIZER.deserializeFromBytes(bytes, type); |
| 297 | + assertNotNull(models); |
| 298 | + assertEquals(2, models.size()); |
| 299 | + assertTrue(models.get(0) instanceof LinkedHashMap); |
| 300 | + |
| 301 | + if (models.get(0) instanceof LinkedHashMap) { |
| 302 | + LinkedHashMap<String, String> model = (LinkedHashMap<String, String>) models.get(0); |
| 303 | + assertEquals("value1", model.get("property")); |
| 304 | + } |
| 305 | + } |
| 306 | + |
| 307 | + /** |
| 308 | + * A model that implements {@link JsonSerializable}. |
| 309 | + */ |
| 310 | + public static final class FooModel implements JsonSerializable<FooModel> { |
| 311 | + private final String property; |
| 312 | + |
| 313 | + public FooModel(String property) { |
| 314 | + this.property = property; |
| 315 | + } |
| 316 | + |
| 317 | + public String getProperty() { |
| 318 | + return this.property; |
| 319 | + } |
| 320 | + |
| 321 | + @Override |
| 322 | + public JsonWriter toJson(JsonWriter jsonWriter) throws IOException { |
| 323 | + jsonWriter.writeStartObject(); |
| 324 | + jsonWriter.writeStringField("property", this.property); |
| 325 | + return jsonWriter.writeEndObject(); |
| 326 | + } |
| 327 | + |
| 328 | + public static FooModel fromJson(JsonReader jsonReader) throws IOException { |
| 329 | + return jsonReader.readObject(reader -> { |
| 330 | + String property = null; |
| 331 | + while (reader.nextToken() != JsonToken.END_OBJECT) { |
| 332 | + String fieldName = reader.getFieldName(); |
| 333 | + reader.nextToken(); |
| 334 | + |
| 335 | + if ("property".equals(fieldName)) { |
| 336 | + property = reader.getString(); |
| 337 | + } else { |
| 338 | + reader.skipChildren(); |
| 339 | + } |
| 340 | + } |
| 341 | + FooModel deserializedModel = new FooModel(property); |
| 342 | + |
| 343 | + return deserializedModel; |
| 344 | + }); |
| 345 | + } |
| 346 | + } |
| 347 | + |
| 348 | + /** |
| 349 | + * A model that does not implement {@link JsonSerializable}. |
| 350 | + */ |
| 351 | + public static final class BarModel { |
| 352 | + private final String property; |
| 353 | + |
| 354 | + public BarModel(String property) { |
| 355 | + this.property = property; |
| 356 | + } |
| 357 | + |
| 358 | + public String getProperty() { |
| 359 | + return this.property; |
| 360 | + } |
| 361 | + } |
269 | 362 | } |
0 commit comments