Skip to content

Commit bd4dceb

Browse files
Copilothossain-khan
andcommitted
Add kdoc examples and complex JSON5 build configuration test
Co-authored-by: hossain-khan <[email protected]>
1 parent 89d133d commit bd4dceb

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

lib/src/main/kotlin/io/github/json5/kotlin/JSON5.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ object JSON5 {
5353
* @param serializer The serializer for type [T]
5454
* @param value The value to encode
5555
* @return JSON5 string representation
56+
*
57+
* ```kotlin
58+
* @Serializable
59+
* data class Config(val name: String, val version: Int)
60+
*
61+
* val config = Config("MyApp", 1)
62+
* val json5 = JSON5.encodeToString(Config.serializer(), config)
63+
* // Result: {name:'MyApp',version:1}
64+
* ```
5665
*/
5766
fun <T> encodeToString(serializer: SerializationStrategy<T>, value: T): String {
5867
return format.encodeToString(serializer, value)
@@ -64,6 +73,21 @@ object JSON5 {
6473
* @param deserializer The deserializer for type [T]
6574
* @param string JSON5 string to decode
6675
* @return Decoded value of type [T]
76+
*
77+
* ```kotlin
78+
* @Serializable
79+
* data class Config(val name: String, val version: Int)
80+
*
81+
* val json5 = """
82+
* {
83+
* // Application name
84+
* name: 'MyApp',
85+
* version: 1, // current version
86+
* }
87+
* """.trimIndent()
88+
* val config = JSON5.decodeFromString(Config.serializer(), json5)
89+
* // Result: Config(name="MyApp", version=1)
90+
* ```
6791
*/
6892
fun <T> decodeFromString(deserializer: DeserializationStrategy<T>, string: String): T {
6993
return format.decodeFromString(deserializer, string)

lib/src/test/kotlin/io/github/json5/kotlin/JSON5IntegrationTest.kt

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,87 @@ class JSON5IntegrationTest {
6868
settings = mapOf("theme" to "dark", "lang" to "en")
6969
)
7070
}
71+
72+
@Serializable
73+
data class BuildConfig(
74+
val modelVersion: String,
75+
val dependencies: Map<String, String>,
76+
val execution: ExecutionConfig,
77+
val logging: LoggingConfig,
78+
val debugging: DebuggingConfig,
79+
val nodeOptions: NodeOptionsConfig
80+
)
81+
82+
@Serializable
83+
data class ExecutionConfig(
84+
val analyze: String,
85+
val daemon: Boolean,
86+
val incremental: Boolean,
87+
val parallel: Boolean,
88+
val typeCheck: Boolean
89+
)
90+
91+
@Serializable
92+
data class LoggingConfig(
93+
val level: String
94+
)
95+
96+
@Serializable
97+
data class DebuggingConfig(
98+
val stacktrace: Boolean
99+
)
100+
101+
@Serializable
102+
data class NodeOptionsConfig(
103+
val maxOldSpaceSize: Int,
104+
val exposeGC: Boolean
105+
)
106+
107+
@Test
108+
fun `should handle complex JSON5 build configuration with mixed comment styles`() {
109+
val buildConfigJson5 = """
110+
{
111+
"modelVersion": "5.0.1", // Version of the hvigor base build capability
112+
"dependencies": {
113+
},
114+
"execution": {
115+
"analyze": "normal", /* Build analysis mode */
116+
"daemon": true, /* Whether to enable daemon process build */
117+
"incremental": true, /* Whether to enable incremental build */
118+
"parallel": true, /* Whether to enable parallel build */
119+
"typeCheck": false, /* Whether to enable type check */
120+
},
121+
"logging": {
122+
"level": "info" /* Log level */
123+
},
124+
"debugging": {
125+
"stacktrace": false /* Whether to enable stack trace */
126+
},
127+
"nodeOptions": {
128+
"maxOldSpaceSize": 4096, /* Memory size of the daemon process when the daemon process is enabled for build, in MB */
129+
"exposeGC": true /* Whether to enable GC */
130+
}
131+
}
132+
""".trimIndent()
133+
134+
val buildConfig = JSON5.decodeFromString(BuildConfig.serializer(), buildConfigJson5)
135+
136+
buildConfig shouldBe BuildConfig(
137+
modelVersion = "5.0.1",
138+
dependencies = emptyMap(),
139+
execution = ExecutionConfig(
140+
analyze = "normal",
141+
daemon = true,
142+
incremental = true,
143+
parallel = true,
144+
typeCheck = false
145+
),
146+
logging = LoggingConfig(level = "info"),
147+
debugging = DebuggingConfig(stacktrace = false),
148+
nodeOptions = NodeOptionsConfig(
149+
maxOldSpaceSize = 4096,
150+
exposeGC = true
151+
)
152+
)
153+
}
71154
}

0 commit comments

Comments
 (0)