@@ -17,7 +17,8 @@ import kotlinx.coroutines.isActive
17
17
import kotlinx.coroutines.launch
18
18
import kotlinx.coroutines.withContext
19
19
import java.io.File
20
- import org.json.JSONObject
20
+ import kotlinx.serialization.json.Json
21
+ import kotlinx.serialization.ExperimentalSerializationApi
21
22
import com.coder.jetbrains.settings.CoderBackendSettings
22
23
23
24
/* *
@@ -56,41 +57,43 @@ class CoderPortForwardService(
56
57
poller?.cancel()
57
58
}
58
59
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
+ }
60
68
61
69
private fun start () {
62
70
val devcontainerFile = CoderBackendSettings .getDevcontainerFile()
63
71
if (devcontainerFile.exists()) {
64
72
try {
65
- val json = devcontainerFile.readText()
66
- val obj = JSONObject ( json)
73
+ val jsonContent = devcontainerFile.readText()
74
+ val config = json.decodeFromString< DevContainerConfig >(jsonContent )
67
75
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" -> {
77
80
logger.info(" found ignored port specification $spec in devcontainer.json" )
78
81
rules.add(0 , PortRule (PortMatcher (spec), false ))
79
- } else if (onAutoForwardStr != " " ) {
82
+ }
83
+ " " -> {}
84
+ else -> {
80
85
logger.info(" found auto-forward port specification $spec in devcontainer.json" )
81
86
rules.add(0 , PortRule (PortMatcher (spec), true ))
82
87
}
83
88
}
84
89
}
85
90
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
+ }
94
97
}
95
98
} catch (e: Exception ) {
96
99
logger.warn(" Failed to parse devcontainer.json" , e)
@@ -151,8 +154,4 @@ class CoderPortForwardService(
151
154
}
152
155
}
153
156
}
154
-
155
- private fun isValidString (value : Any? ): Boolean {
156
- return value != null && value is String
157
- }
158
157
}
0 commit comments