Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitdd73756

Browse files
committed
Switch from Gson to Moshi
The issue I am having with Gson is that it relies on some unsafe thingthat is not available in all cases.Since Moshi is by the same folks that wrote the HTTP and REST clients weare using, it seemed like the best replacement.Thankfully, for the most part this was a simple search and replace, butcalling toJson and fromJson is a bit more burdensome as you have to call`adapter` and include the type, and it has no built-in converter forUUID. On the flip side, the converters require slightly less code.
1 parentbe860cc commitdd73756

27 files changed

+332
-438
lines changed

‎build.gradle.kts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ dependencies {
2525
implementation("com.squareup.retrofit2:retrofit:2.9.0")
2626
// define a BOM and its version
2727
implementation(platform("com.squareup.okhttp3:okhttp-bom:4.12.0"))
28-
implementation("com.squareup.retrofit2:converter-gson:2.9.0")
28+
implementation("com.squareup.retrofit2:converter-moshi:2.9.0")
2929
implementation("com.squareup.okhttp3:okhttp")
3030
implementation("com.squareup.okhttp3:logging-interceptor")
3131

‎src/main/kotlin/com/coder/gateway/cli/CoderCLIManager.kt‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ import com.coder.gateway.util.getHeaders
1616
importcom.coder.gateway.util.getOS
1717
importcom.coder.gateway.util.safeHost
1818
importcom.coder.gateway.util.sha1
19-
importcom.google.gson.Gson
20-
importcom.google.gson.JsonSyntaxException
2119
importcom.intellij.openapi.diagnostic.Logger
2220
importcom.intellij.openapi.progress.ProgressIndicator
21+
importcom.squareup.moshi.Json
22+
importcom.squareup.moshi.Moshi
2323
importorg.zeroturnaround.exec.ProcessExecutor
24+
importjava.io.EOFException
2425
importjava.io.FileInputStream
2526
importjava.io.FileNotFoundException
2627
importjava.net.ConnectException
@@ -328,7 +329,7 @@ class CoderCLIManager(
328329
* Version output from the CLI's version command.
329330
*/
330331
privatedata classVersion(
331-
valversion:String,
332+
@Json(name="version")valversion:String,
332333
)
333334

334335
/**
@@ -338,11 +339,15 @@ class CoderCLIManager(
338339
*/
339340
funversion():SemVer {
340341
val raw= exec("version","--output","json")
341-
val json=Gson().fromJson(raw,Version::class.java)
342-
if (json?.version==null) {
342+
try {
343+
val json=Moshi.Builder().build().adapter(Version::class.java).fromJson(raw)
344+
if (json?.version==null) {
345+
throwMissingVersionException("No version found in output")
346+
}
347+
returnSemVer.parse(json.version)
348+
}catch (exception:EOFException) {
343349
throwMissingVersionException("No version found in output")
344350
}
345-
returnSemVer.parse(json.version)
346351
}
347352

348353
/**
@@ -353,7 +358,6 @@ class CoderCLIManager(
353358
version()
354359
}catch (e:Exception) {
355360
when (e) {
356-
isJsonSyntaxException,
357361
isInvalidVersionException-> {
358362
logger.info("Got invalid version from$localBinaryPath:${e.message}")
359363
}

‎src/main/kotlin/com/coder/gateway/sdk/BaseCoderRestClient.kt‎

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.coder.gateway.icons.toRetinaAwareIcon
55
importcom.coder.gateway.sdk.convertors.ArchConverter
66
importcom.coder.gateway.sdk.convertors.InstantConverter
77
importcom.coder.gateway.sdk.convertors.OSConverter
8+
importcom.coder.gateway.sdk.convertors.UUIDConverter
89
importcom.coder.gateway.sdk.ex.AuthenticationResponseException
910
importcom.coder.gateway.sdk.ex.TemplateResponseException
1011
importcom.coder.gateway.sdk.ex.WorkspaceResponseException
@@ -19,28 +20,24 @@ import com.coder.gateway.sdk.v2.models.WorkspaceResource
1920
importcom.coder.gateway.sdk.v2.models.WorkspaceTransition
2021
importcom.coder.gateway.services.CoderSettingsState
2122
importcom.coder.gateway.settings.CoderSettings
22-
importcom.coder.gateway.util.Arch
2323
importcom.coder.gateway.util.CoderHostnameVerifier
24-
importcom.coder.gateway.util.OS
2524
importcom.coder.gateway.util.coderSocketFactory
2625
importcom.coder.gateway.util.coderTrustManagers
2726
importcom.coder.gateway.util.getHeaders
2827
importcom.coder.gateway.util.toURL
2928
importcom.coder.gateway.util.withPath
30-
importcom.google.gson.Gson
31-
importcom.google.gson.GsonBuilder
3229
importcom.intellij.openapi.util.SystemInfo
3330
importcom.intellij.util.ImageLoader
3431
importcom.intellij.util.ui.ImageUtil
32+
importcom.squareup.moshi.Moshi
3533
importokhttp3.Credentials
3634
importokhttp3.OkHttpClient
3735
importokhttp3.logging.HttpLoggingInterceptor
3836
importorg.imgscalr.Scalr
3937
importretrofit2.Retrofit
40-
importretrofit2.converter.gson.GsonConverterFactory
38+
importretrofit2.converter.moshi.MoshiConverterFactory
4139
importjava.net.HttpURLConnection
4240
importjava.net.URL
43-
importjava.time.Instant
4441
importjava.util.UUID
4542
importjavax.net.ssl.X509TrustManager
4643
importjavax.swing.Icon
@@ -61,11 +58,12 @@ open class BaseCoderRestClient(
6158
lateinitvar buildVersion:String
6259

6360
init {
64-
val gson:Gson=GsonBuilder()
65-
.registerTypeAdapter(Instant::class.java,InstantConverter())
66-
.registerTypeAdapter(Arch::class.java,ArchConverter())
67-
.registerTypeAdapter(OS::class.java,OSConverter())
68-
.setPrettyPrinting().create()
61+
val moshi=Moshi.Builder()
62+
.add(ArchConverter())
63+
.add(InstantConverter())
64+
.add(OSConverter())
65+
.add(UUIDConverter())
66+
.build()
6967

7068
val socketFactory= coderSocketFactory(settings.tls)
7169
val trustManagers= coderTrustManagers(settings.tls.caPath)
@@ -104,7 +102,7 @@ open class BaseCoderRestClient(
104102
.build()
105103

106104
retroRestClient=Retrofit.Builder().baseUrl(url.toString()).client(httpClient)
107-
.addConverterFactory(GsonConverterFactory.create(gson))
105+
.addConverterFactory(MoshiConverterFactory.create(moshi))
108106
.build().create(CoderV2RestFacade::class.java)
109107
}
110108

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,13 @@
11
packagecom.coder.gateway.sdk.convertors
22

33
importcom.coder.gateway.util.Arch
4-
importcom.google.gson.JsonDeserializationContext
5-
importcom.google.gson.JsonDeserializer
6-
importcom.google.gson.JsonElement
7-
importcom.google.gson.JsonParseException
8-
importcom.google.gson.JsonPrimitive
9-
importcom.google.gson.JsonSerializationContext
10-
importcom.google.gson.JsonSerializer
11-
importjava.lang.reflect.Type
4+
importcom.squareup.moshi.FromJson
5+
importcom.squareup.moshi.ToJson
126

137
/**
14-
*GSON serialiser/deserialiser for converting [Arch] objects.
8+
*Serializer/deserializer for converting [Arch] objects.
159
*/
16-
classArchConverter :JsonSerializer<Arch?>,JsonDeserializer<Arch?> {
17-
overridefunserialize(src:Arch?,typeOfSrc:Type?,context:JsonSerializationContext?):JsonElement {
18-
returnJsonPrimitive(src?.toString()?:"")
19-
}
20-
21-
@Throws(JsonParseException::class)
22-
overridefundeserialize(json:JsonElement,typeOfT:Type,context:JsonDeserializationContext):Arch? {
23-
returnArch.from(json.asString)
24-
}
10+
classArchConverter {
11+
@ToJsonfuntoJson(src:Arch?):String= src?.toString()?:""
12+
@FromJsonfunfromJson(src:String):Arch?=Arch.from(src)
2513
}
Lines changed: 8 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,20 @@
11
packagecom.coder.gateway.sdk.convertors
22

3-
importcom.google.gson.JsonDeserializationContext
4-
importcom.google.gson.JsonDeserializer
5-
importcom.google.gson.JsonElement
6-
importcom.google.gson.JsonParseException
7-
importcom.google.gson.JsonPrimitive
8-
importcom.google.gson.JsonSerializationContext
9-
importcom.google.gson.JsonSerializer
10-
importjava.lang.reflect.Type
3+
importcom.squareup.moshi.FromJson
4+
importcom.squareup.moshi.ToJson
115
importjava.time.Instant
126
importjava.time.format.DateTimeFormatter
137
importjava.time.temporal.TemporalAccessor
148

159
/**
16-
*GSON serialiser/deserialiser for converting [Instant] objects.
10+
*Serializer/deserializer for converting [Instant] objects.
1711
*/
18-
classInstantConverter :JsonSerializer<Instant?>,JsonDeserializer<Instant?> {
19-
/**
20-
* Gson invokes this call-back method during serialization when it encounters a field of the
21-
* specified type.
22-
*
23-
*
24-
*
25-
* In the implementation of this call-back method, you should consider invoking
26-
* [JsonSerializationContext.serialize] method to create JsonElements for any
27-
* non-trivial field of the `src` object. However, you should never invoke it on the
28-
* `src` object itself since that will cause an infinite loop (Gson will call your
29-
* call-back method again).
30-
*
31-
* @param src the object that needs to be converted to Json.
32-
* @param typeOfSrc the actual type (fully genericized version) of the source object.
33-
* @return a JsonElement corresponding to the specified object.
34-
*/
35-
overridefunserialize(src:Instant?,typeOfSrc:Type?,context:JsonSerializationContext?):JsonElement {
36-
returnJsonPrimitive(FORMATTER.format(src))
37-
}
38-
39-
/**
40-
* Gson invokes this call-back method during deserialization when it encounters a field of the
41-
* specified type.
42-
*
43-
*
44-
*
45-
* In the implementation of this call-back method, you should consider invoking
46-
* [JsonDeserializationContext.deserialize] method to create objects
47-
* for any non-trivial field of the returned object. However, you should never invoke it on the
48-
* the same type passing `json` since that will cause an infinite loop (Gson will call your
49-
* call-back method again).
50-
*
51-
* @param json The Json data being deserialized
52-
* @param typeOfT The type of the Object to deserialize to
53-
* @return a deserialized object of the specified type typeOfT which is a subclass of `T`
54-
* @throws JsonParseException if json is not in the expected format of `typeOfT`
55-
*/
56-
@Throws(JsonParseException::class)
57-
overridefundeserialize(json:JsonElement,typeOfT:Type,context:JsonDeserializationContext):Instant {
58-
returnFORMATTER.parse(json.asString) { temporal:TemporalAccessor?->Instant.from(temporal) }
59-
}
12+
classInstantConverter {
13+
@ToJsonfuntoJson(src:Instant?):String=FORMATTER.format(src)
14+
@FromJsonfunfromJson(src:String):Instant?=FORMATTER.parse(src) {
15+
temporal:TemporalAccessor?->Instant.from(temporal) }
6016

6117
companionobject {
62-
/** Formatter.*/
6318
privatevalFORMATTER=DateTimeFormatter.ISO_INSTANT
6419
}
65-
}
20+
}
Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,13 @@
11
packagecom.coder.gateway.sdk.convertors
22

33
importcom.coder.gateway.util.OS
4-
importcom.google.gson.JsonDeserializationContext
5-
importcom.google.gson.JsonDeserializer
6-
importcom.google.gson.JsonElement
7-
importcom.google.gson.JsonParseException
8-
importcom.google.gson.JsonPrimitive
9-
importcom.google.gson.JsonSerializationContext
10-
importcom.google.gson.JsonSerializer
11-
importjava.lang.reflect.Type
4+
importcom.squareup.moshi.FromJson
5+
importcom.squareup.moshi.ToJson
126

137
/**
14-
*GSON serialiser/deserialiser for converting [OS] objects.
8+
*Serializer/deserializer for converting [OS] objects.
159
*/
16-
classOSConverter :JsonSerializer<OS?>,JsonDeserializer<OS?> {
17-
overridefunserialize(src:OS?,typeOfSrc:Type?,context:JsonSerializationContext?):JsonElement {
18-
returnJsonPrimitive(src?.toString()?:"")
19-
}
20-
21-
@Throws(JsonParseException::class)
22-
overridefundeserialize(json:JsonElement,typeOfT:Type,context:JsonDeserializationContext):OS? {
23-
returnOS.from(json.asString)
24-
}
10+
classOSConverter {
11+
@ToJsonfuntoJson(src:OS?):String= src?.toString()?:""
12+
@FromJsonfunfromJson(src:String):OS?=OS.from(src)
2513
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
packagecom.coder.gateway.sdk.convertors
2+
3+
importcom.squareup.moshi.FromJson
4+
importcom.squareup.moshi.ToJson
5+
importjava.util.UUID
6+
7+
/**
8+
* Serializer/deserializer for converting [UUID] objects.
9+
*/
10+
classUUIDConverter {
11+
@ToJsonfuntoJson(src:UUID):String= src.toString()
12+
@FromJsonfunfromJson(src:String):UUID=UUID.fromString(src)
13+
}

‎src/main/kotlin/com/coder/gateway/sdk/v2/models/BuildInfo.kt‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
packagecom.coder.gateway.sdk.v2.models
22

3-
importcom.google.gson.annotations.SerializedName
3+
importcom.squareup.moshi.Json
44

55
/**
66
* Contains build information for a Coder instance.
@@ -12,6 +12,6 @@ import com.google.gson.annotations.SerializedName
1212
* @param version the semantic version of the build.
1313
*/
1414
data classBuildInfo(
15-
@SerializedName("external_url")valexternalUrl:String,
16-
@SerializedName("version")valversion:String
15+
@Json(name="external_url")valexternalUrl:String,
16+
@Json(name="version")valversion:String
1717
)
Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
packagecom.coder.gateway.sdk.v2.models
22

3-
importcom.google.gson.annotations.SerializedName
3+
importcom.squareup.moshi.Json
44

55
enumclassBuildReason {
66
// "initiator" is used when a workspace build is triggered by a user.
77
// Combined with the initiator id/username, it indicates which user initiated the build.
8-
@SerializedName("initiator")
9-
INITIATOR,
10-
8+
@Json(name="initiator")INITIATOR,
119
// "autostart" is used when a build to start a workspace is triggered by Autostart.
1210
// The initiator id/username in this case is the workspace owner and can be ignored.
13-
@SerializedName("autostart")
14-
AUTOSTART,
15-
11+
@Json(name="autostart")AUTOSTART,
1612
// "autostop" is used when a build to stop a workspace is triggered by Autostop.
1713
// The initiator id/username in this case is the workspace owner and can be ignored.
18-
@SerializedName("autostop")
19-
AUTOSTOP
20-
}
14+
@Json(name="autostop")AUTOSTOP
15+
}
Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,23 @@
11
packagecom.coder.gateway.sdk.v2.models
22

3-
importcom.google.gson.annotations.SerializedName
3+
importcom.squareup.moshi.Json
44
importjava.util.UUID
55

66
data classCreateParameterRequest(
7-
@SerializedName("copy_from_parameter")valcloneID:UUID?,
8-
@SerializedName("name")valname:String,
9-
@SerializedName("source_value")valsourceValue:String,
10-
@SerializedName("source_scheme")valsourceScheme:ParameterSourceScheme,
11-
@SerializedName("destination_scheme")valdestinationScheme:ParameterDestinationScheme
7+
@Json(name="copy_from_parameter")valcloneID:UUID?,
8+
@Json(name="name")valname:String,
9+
@Json(name="source_value")valsourceValue:String,
10+
@Json(name="source_scheme")valsourceScheme:ParameterSourceScheme,
11+
@Json(name="destination_scheme")valdestinationScheme:ParameterDestinationScheme
1212
)
1313

1414
enumclassParameterSourceScheme {
15-
@SerializedName("none")
16-
NONE,
17-
18-
@SerializedName("data")
19-
DATA
15+
@Json(name="none")NONE,
16+
@Json(name="data")DATA
2017
}
2118

2219
enumclassParameterDestinationScheme {
23-
@SerializedName("none")
24-
NONE,
25-
26-
@SerializedName("environment_variable")
27-
ENVIRONMENT_VARIABLE,
28-
29-
@SerializedName("provisioner_variable")
30-
PROVISIONER_VARIABLE
31-
}
20+
@Json(name="none")NONE,
21+
@Json(name="environment_variable")ENVIRONMENT_VARIABLE,
22+
@Json(name="provisioner_variable")PROVISIONER_VARIABLE
23+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp