@@ -6,6 +6,9 @@ import com.fasterxml.jackson.databind.PropertyName
6
6
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
7
7
import com.fasterxml.jackson.module.kotlin.readValue
8
8
import earth.adi.typeid.jackson.IdJsonDeserializer
9
+ import earth.adi.typeid.jackson.IdJsonSerializer
10
+ import earth.adi.typeid.jackson.RawIdJsonSerializer
11
+ import java.io.ByteArrayOutputStream
9
12
import org.assertj.core.api.Assertions.assertThat
10
13
import org.junit.jupiter.api.Test
11
14
@@ -77,6 +80,29 @@ class JacksonJsonTest {
77
80
assertThat(writtenJson).isEqualTo(JSON_IDS_ONLY )
78
81
}
79
82
83
+ data class RawIds (
84
+ @JsonProperty(" user_id" ) val userId : RawId ,
85
+ @JsonProperty(" org_id" ) val organizationId : RawId ,
86
+ )
87
+
88
+ @Test
89
+ fun `deserialize raw ids from json` () {
90
+ val json = objectMapper.readValue<RawIds >(JSON_IDS_ONLY )
91
+ assertThat(json.userId).isEqualTo(typeId.parse(" user_01hy0d96sgfx0rh975kqkspchh" ))
92
+ assertThat(json.organizationId).isEqualTo(typeId.parse(" org_01hy0sk45qfmdsdme1j703yjet" ))
93
+ }
94
+
95
+ @Test
96
+ fun `serialize raw ids to json` () {
97
+ val json =
98
+ RawIds (
99
+ typeId.parse(" user_01hy0d96sgfx0rh975kqkspchh" ),
100
+ typeId.parse(" org_01hy0sk45qfmdsdme1j703yjet" ),
101
+ )
102
+ val writtenJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(json)
103
+ assertThat(writtenJson).isEqualTo(JSON_IDS_ONLY )
104
+ }
105
+
80
106
@Test
81
107
fun `create IdJsonDeserializer null deserializerContext` () {
82
108
val type = objectMapper.constructType(Id ::class .java)
@@ -101,6 +127,22 @@ class JacksonJsonTest {
101
127
assertThat(idJsonDeserializer.valueType).isNull()
102
128
}
103
129
130
+ @Test
131
+ fun `IdJsonSerializer null id` () {
132
+ ByteArrayOutputStream ().use { outputStream ->
133
+ IdJsonSerializer ().serialize(null , objectMapper.createGenerator(outputStream), null )
134
+ assertThat(outputStream.toString()).isEqualTo(" " )
135
+ }
136
+ }
137
+
138
+ @Test
139
+ fun `RawIdJsonSerializer null id` () {
140
+ ByteArrayOutputStream ().use { outputStream ->
141
+ RawIdJsonSerializer ().serialize(null , objectMapper.createGenerator(outputStream), null )
142
+ assertThat(outputStream.toString()).isEqualTo(" " )
143
+ }
144
+ }
145
+
104
146
companion object {
105
147
private val JSON_USER =
106
148
"""
0 commit comments