Skip to content

Commit 0a22f8b

Browse files
committed
add GsonUtil, so Project can use Gson with sealed classes provided by this project
1 parent 2fcee40 commit 0a22f8b

File tree

5 files changed

+714
-173
lines changed

5 files changed

+714
-173
lines changed

kotlin-insight-client/kotlin-insight-client-api/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,11 @@
2828
<version>2.0.1.Final</version>
2929
<scope>provided</scope>
3030
</dependency>
31+
<dependency>
32+
<groupId>com.google.code.gson</groupId>
33+
<artifactId>gson</artifactId>
34+
<version>2.2.2-atlassian-1</version>
35+
<scope>provided</scope>
36+
</dependency>
3137
</dependencies>
3238
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*-
2+
* #%L
3+
* kotlin-insight-client-api
4+
* %%
5+
* Copyright (C) 2022 - 2023 linked-planet GmbH
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package com.linkedplanet.kotlininsightclient.api.impl
21+
22+
import com.google.gson.GsonBuilder
23+
import com.google.gson.TypeAdapter
24+
import com.google.gson.stream.JsonReader
25+
import com.google.gson.stream.JsonWriter
26+
import com.linkedplanet.kotlininsightclient.api.model.InsightAttribute
27+
import com.linkedplanet.kotlininsightclient.api.model.ObjectTypeSchemaAttribute
28+
import java.time.ZonedDateTime
29+
30+
31+
class GsonUtil {
32+
companion object {
33+
fun gsonBuilder(): GsonBuilder = GsonBuilder()
34+
.registerTypeAdapter(ZonedDateTime::class.java, zonedDateTimeAdapter)
35+
.registerTypeAdapterFactory(insightAttributeAdapter)
36+
.registerTypeAdapterFactory(insightSchemaAttributeAdapter)
37+
38+
39+
private val zonedDateTimeAdapter = object : TypeAdapter<ZonedDateTime>() {
40+
override fun write(out: JsonWriter, value: ZonedDateTime?) {
41+
out.value(value.toString())
42+
}
43+
44+
override fun read(`in`: JsonReader): ZonedDateTime {
45+
return ZonedDateTime.parse(`in`.nextString())
46+
}
47+
}
48+
49+
private val insightAttributeAdapter =
50+
RuntimeTypeAdapterFactory.of(InsightAttribute::class.java, "type", true)
51+
.registerSubtype(InsightAttribute.Text::class.java)
52+
.registerSubtype(InsightAttribute.Integer::class.java)
53+
.registerSubtype(InsightAttribute.Bool::class.java)
54+
.registerSubtype(InsightAttribute.DoubleNumber::class.java)
55+
.registerSubtype(InsightAttribute.Select::class.java)
56+
.registerSubtype(InsightAttribute.Date::class.java)
57+
.registerSubtype(InsightAttribute.Time::class.java)
58+
.registerSubtype(InsightAttribute.DateTime::class.java)
59+
.registerSubtype(InsightAttribute.Url::class.java)
60+
.registerSubtype(InsightAttribute.Email::class.java)
61+
.registerSubtype(InsightAttribute.Textarea::class.java)
62+
.registerSubtype(InsightAttribute.Ipaddress::class.java)
63+
.registerSubtype(InsightAttribute.Reference::class.java)
64+
.registerSubtype(InsightAttribute.User::class.java)
65+
.registerSubtype(InsightAttribute.Confluence::class.java)
66+
.registerSubtype(InsightAttribute.Group::class.java)
67+
.registerSubtype(InsightAttribute.Version::class.java)
68+
.registerSubtype(InsightAttribute.Project::class.java)
69+
.registerSubtype(InsightAttribute.Status::class.java)
70+
.registerSubtype(InsightAttribute.Unknown::class.java)!!
71+
72+
private val insightSchemaAttributeAdapter =
73+
RuntimeTypeAdapterFactory.of(ObjectTypeSchemaAttribute::class.java, "type", true)
74+
.registerSubtype(ObjectTypeSchemaAttribute.TextSchema::class.java, "Text")
75+
.registerSubtype(ObjectTypeSchemaAttribute.IntegerSchema::class.java, "Integer")
76+
.registerSubtype(ObjectTypeSchemaAttribute.BoolSchema::class.java, "Bool")
77+
.registerSubtype(ObjectTypeSchemaAttribute.DoubleNumberSchema::class.java, "DoubleNumber")
78+
.registerSubtype(ObjectTypeSchemaAttribute.SelectSchema::class.java, "Select")
79+
.registerSubtype(ObjectTypeSchemaAttribute.DateSchema::class.java, "Date")
80+
.registerSubtype(ObjectTypeSchemaAttribute.TimeSchema::class.java, "Time")
81+
.registerSubtype(ObjectTypeSchemaAttribute.DateTimeSchema::class.java, "DateTime")
82+
.registerSubtype(ObjectTypeSchemaAttribute.UrlSchema::class.java, "Url")
83+
.registerSubtype(ObjectTypeSchemaAttribute.EmailSchema::class.java, "Email")
84+
.registerSubtype(ObjectTypeSchemaAttribute.TextareaSchema::class.java, "Textarea")
85+
.registerSubtype(ObjectTypeSchemaAttribute.IpaddressSchema::class.java, "Ipaddress")
86+
.registerSubtype(ObjectTypeSchemaAttribute.ReferenceSchema::class.java, "Reference")
87+
.registerSubtype(ObjectTypeSchemaAttribute.UserSchema::class.java, "User")
88+
.registerSubtype(ObjectTypeSchemaAttribute.ConfluenceSchema::class.java, "Confluence")
89+
.registerSubtype(ObjectTypeSchemaAttribute.GroupSchema::class.java, "Group")
90+
.registerSubtype(ObjectTypeSchemaAttribute.VersionSchema::class.java, "Version")
91+
.registerSubtype(ObjectTypeSchemaAttribute.ProjectSchema::class.java, "Project")
92+
.registerSubtype(ObjectTypeSchemaAttribute.StatusSchema::class.java, "Status")
93+
.registerSubtype(ObjectTypeSchemaAttribute.UnknownSchema::class.java, "Unknown")
94+
}
95+
96+
}

0 commit comments

Comments
 (0)