1
+ package com.fasterxml.jackson.module.kotlin.test
2
+
3
+ import com.fasterxml.jackson.annotation.JsonSubTypes
4
+ import com.fasterxml.jackson.annotation.JsonTypeInfo
5
+ import com.fasterxml.jackson.annotation.JsonTypeName
6
+ import com.fasterxml.jackson.databind.ObjectMapper
7
+ import com.fasterxml.jackson.module.kotlin.KotlinModule
8
+ import com.fasterxml.jackson.module.kotlin.readValue
9
+ import org.junit.Test
10
+ import kotlin.test.assertEquals
11
+
12
+ @JsonTypeInfo(use = JsonTypeInfo .Id .NAME )
13
+ sealed class Github239Either {
14
+
15
+ @JsonTypeName(" a" )
16
+ data class A (var field : String = " " ) : Github239Either()
17
+
18
+ @JsonTypeName(" b" )
19
+ data class B (var otherField : String = " " ) : Github239Either()
20
+
21
+ }
22
+
23
+ @JsonTypeInfo(use = JsonTypeInfo .Id .NAME )
24
+ @JsonSubTypes(
25
+ JsonSubTypes .Type (Github239EitherCustomized .A ::class , name = " a" ),
26
+ JsonSubTypes .Type (Github239EitherCustomized .B ::class , name = " b" )
27
+ )
28
+ sealed class Github239EitherCustomized {
29
+
30
+ data class A (var field : String = " " ) : Github239EitherCustomized()
31
+
32
+ data class B (var otherField : String = " " ) : Github239EitherCustomized()
33
+
34
+ }
35
+
36
+ class TestGithub239 {
37
+ val json = """ [
38
+ {
39
+ "@type": "a",
40
+ "field": "value"
41
+ },
42
+ {
43
+ "@type": "b",
44
+ "otherField": "1234"
45
+ }
46
+ ]"""
47
+
48
+ val mapper = ObjectMapper ()
49
+ .registerModule(KotlinModule ())
50
+
51
+ @Test
52
+ fun test_implicit_subclasses () {
53
+
54
+ val array = mapper.readValue<Array <Github239Either >>(json)
55
+
56
+ assertEquals(2 , array.size)
57
+ assertEquals(Github239Either .A (" value" ), array[0 ])
58
+ assertEquals(Github239Either .B (" 1234" ), array[1 ])
59
+
60
+ }
61
+
62
+ @Test
63
+ fun test_explicit_subclasses () {
64
+
65
+ val array = mapper.readValue<Array <Github239EitherCustomized >>(json)
66
+
67
+ assertEquals(2 , array.size)
68
+ assertEquals(Github239EitherCustomized .A (" value" ), array[0 ])
69
+ assertEquals(Github239EitherCustomized .B (" 1234" ), array[1 ])
70
+
71
+ }
72
+
73
+ }
0 commit comments