Skip to content

Commit 3433d28

Browse files
committed
Use kotlinx.serialization for JSON deserialization
1 parent 69432d7 commit 3433d28

File tree

3 files changed

+51
-27
lines changed

3 files changed

+51
-27
lines changed

build.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ plugins {
1111
alias(libs.plugins.changelog) // Gradle Changelog Plugin
1212
alias(libs.plugins.qodana) // Gradle Qodana Plugin
1313
alias(libs.plugins.kover) // Gradle Kover Plugin
14+
kotlin("plugin.serialization") version "2.1.10"
1415
}
1516

1617
group = properties("pluginGroup").get()
@@ -24,7 +25,7 @@ repositories {
2425
// Dependencies are managed with Gradle version catalog - read more: https://docs.gradle.org/current/userguide/platforms.html#sub:version-catalog
2526
dependencies {
2627
// implementation(libs.annotations)
27-
implementation("org.json:json:20210307")
28+
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.8.0")
2829
}
2930

3031
// Set the JVM language level used to build the project.

src/main/kotlin/com/coder/jetbrains/services/CoderPortForwardService.kt

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ import kotlinx.coroutines.isActive
1717
import kotlinx.coroutines.launch
1818
import kotlinx.coroutines.withContext
1919
import java.io.File
20-
import org.json.JSONObject
20+
import kotlinx.serialization.json.Json
21+
import kotlinx.serialization.ExperimentalSerializationApi
2122
import com.coder.jetbrains.settings.CoderBackendSettings
2223

2324
/**
@@ -56,41 +57,43 @@ class CoderPortForwardService(
5657
poller?.cancel()
5758
}
5859

59-
class InvalidJsonTypeException(message: String) : Exception(message)
60+
companion object {
61+
@OptIn(ExperimentalSerializationApi::class)
62+
private val json = Json {
63+
ignoreUnknownKeys = true
64+
allowTrailingComma = true
65+
allowComments = true
66+
}
67+
}
6068

6169
private fun start() {
6270
val devcontainerFile = CoderBackendSettings.getDevcontainerFile()
6371
if (devcontainerFile.exists()) {
6472
try {
65-
val json = devcontainerFile.readText()
66-
val obj = JSONObject(json)
73+
val jsonContent = devcontainerFile.readText()
74+
val config = json.decodeFromString<DevContainerConfig>(jsonContent)
6775

68-
val portsAttributes = obj.optJSONObject("portsAttributes") ?: JSONObject()
69-
portsAttributes.keys().forEach { spec ->
70-
portsAttributes.optJSONObject(spec)?.let { attrs ->
71-
val onAutoForward = attrs.opt("onAutoForward")
72-
if (!isValidString(onAutoForward)) {
73-
throw InvalidJsonTypeException("onAutoForward for port $spec is not a string value")
74-
}
75-
val onAutoForwardStr = onAutoForward as String
76-
if (onAutoForwardStr == "ignore") {
76+
// Process port attributes
77+
config.portsAttributes.forEach { (spec, attrs) ->
78+
when (attrs.onAutoForward) {
79+
"ignore" -> {
7780
logger.info("found ignored port specification $spec in devcontainer.json")
7881
rules.add(0, PortRule(PortMatcher(spec), false))
79-
} else if (onAutoForwardStr != "") {
82+
}
83+
"" -> {}
84+
else -> {
8085
logger.info("found auto-forward port specification $spec in devcontainer.json")
8186
rules.add(0, PortRule(PortMatcher(spec), true))
8287
}
8388
}
8489
}
8590

86-
val otherPortsAttributes = obj.optJSONObject("otherPortsAttributes") ?: JSONObject()
87-
val otherPortsAutoForward = otherPortsAttributes.opt("onAutoForward")
88-
if (!isValidString(otherPortsAutoForward)) {
89-
throw InvalidJsonTypeException("otherPortsAttributes.onAutoForward is not a string value")
90-
}
91-
if ((otherPortsAutoForward as String) == "ignore") {
92-
logger.info("found ignored setting for otherPortsAttributes in devcontainer.json")
93-
defaultForward = false
91+
// Process other ports attributes
92+
config.otherPortsAttributes?.let {
93+
if (it.onAutoForward == "ignore") {
94+
logger.info("found ignored setting for otherPortsAttributes in devcontainer.json")
95+
defaultForward = false
96+
}
9497
}
9598
} catch (e: Exception) {
9699
logger.warn("Failed to parse devcontainer.json", e)
@@ -151,8 +154,4 @@ class CoderPortForwardService(
151154
}
152155
}
153156
}
154-
155-
private fun isValidString(value: Any?): Boolean {
156-
return value != null && value is String
157-
}
158157
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.coder.jetbrains.services
2+
3+
import kotlinx.serialization.SerialName
4+
import kotlinx.serialization.Serializable
5+
6+
@Serializable
7+
data class DevContainerConfig(
8+
@SerialName("portsAttributes")
9+
val portsAttributes: Map<String, PortAttributes> = mapOf(),
10+
@SerialName("otherPortsAttributes")
11+
val otherPortsAttributes: OtherPortsAttributes? = null
12+
)
13+
14+
@Serializable
15+
data class PortAttributes(
16+
@SerialName("onAutoForward")
17+
val onAutoForward: String = ""
18+
)
19+
20+
@Serializable
21+
data class OtherPortsAttributes(
22+
@SerialName("onAutoForward")
23+
val onAutoForward: String = ""
24+
)

0 commit comments

Comments
 (0)