Skip to content

Commit eef72f1

Browse files
authored
Merge pull request #44 from hossain-khan/format
[UPDATE] Applied ktlint formatter
2 parents 088404b + 245a702 commit eef72f1

29 files changed

+1183
-948
lines changed

.github/copilot-instructions.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This is a Kotlin based repository with a JSON5 library for parsing and serializi
77
### Development Flow
88
- Build: `./gradlew build`
99
- Test: `./gradlew check`
10+
- Format: `./gradlew formatKotlin`
1011

1112
## Repository Structure
1213
- `lib` - Gradle module containing the core JSON5 library code with unit tests
@@ -15,6 +16,8 @@ This is a Kotlin based repository with a JSON5 library for parsing and serializi
1516

1617
## Key Guidelines
1718
1. Follow Kotlin best practices and idiomatic patterns
18-
2. Maintain existing code structure and organization
19-
3. Write unit tests for new functionality
20-
4. Update project README as needed to reflect changes
19+
2. Do not use wildcard imports
20+
3. Maintain existing code structure and organization
21+
4. Write unit tests for new functionality
22+
5. Format code using `./gradlew formatKotlin` before committing
23+
6. Update project README as needed to reflect changes

app/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ plugins {
66
// Apply the Application plugin to add support for building an executable JVM application.
77
application
88
alias(libs.plugins.kotlinPluginSerialization)
9+
alias(libs.plugins.kotlinter)
910
}
1011

1112
dependencies {

app/src/main/kotlin/App.kt

Lines changed: 91 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import kotlinx.serialization.Serializable
1010
fun main() {
1111
// Test serialization and deserialization of Employee model
1212
testEmployeeSerialization()
13-
13+
1414
// Run README sample code validation tests
1515
println("\n=== README Sample Code Validation ===")
1616
testBasicParsingAndStringifying()
@@ -23,13 +23,14 @@ fun main() {
2323
}
2424

2525
private fun testSampleJson5Files() {
26-
val json5files = listOf(
27-
"simple-object.json5",
28-
"array-example.json5",
29-
"numeric-formats.json5",
30-
"string-and-identifiers.json5",
31-
"root-string.json5"
32-
)
26+
val json5files =
27+
listOf(
28+
"simple-object.json5",
29+
"array-example.json5",
30+
"numeric-formats.json5",
31+
"string-and-identifiers.json5",
32+
"root-string.json5",
33+
)
3334

3435
json5files.forEach { fileName ->
3536
println("\n=== Processing file: $fileName ===")
@@ -53,7 +54,6 @@ private fun testSampleJson5Files() {
5354
}
5455
}
5556

56-
5757
fun testEmployeeSerialization() {
5858
val fileName = "employee-example.json5"
5959
val resourceStream = object {}.javaClass.getResourceAsStream("/$fileName")
@@ -80,17 +80,18 @@ fun testEmployeeSerialization() {
8080
*/
8181
fun testBasicParsingAndStringifying() {
8282
println("\n--- Testing Basic Parsing and Stringifying ---")
83-
83+
8484
try {
8585
// Parse JSON5 to strongly-typed JSON5Value objects
86-
val json5 = """
87-
{
88-
// Configuration for my app
89-
name: 'MyApp',
90-
version: 2,
91-
features: ['auth', 'analytics',], // trailing comma
92-
}
93-
""".trimIndent()
86+
val json5 =
87+
"""
88+
{
89+
// Configuration for my app
90+
name: 'MyApp',
91+
version: 2,
92+
features: ['auth', 'analytics',], // trailing comma
93+
}
94+
""".trimIndent()
9495

9596
val parsed = JSON5.parse(json5)
9697
println("✓ Parsed JSON5 successfully: $parsed")
@@ -101,23 +102,25 @@ fun testBasicParsingAndStringifying() {
101102
val name = parsed.value["name"] as? JSON5Value.String
102103
val version = parsed.value["version"] as? JSON5Value.Number
103104
val features = parsed.value["features"] as? JSON5Value.Array
104-
105+
105106
println("✓ App name: ${name?.value}") // "MyApp"
106-
println("✓ Version: ${(version as? JSON5Value.Number.Integer)?.value ?: (version as? JSON5Value.Number.Decimal)?.value?.toInt()}") // 2
107+
println(
108+
"✓ Version: ${(version as? JSON5Value.Number.Integer)?.value ?: (version as? JSON5Value.Number.Decimal)?.value?.toInt()}",
109+
) // 2
107110
println("✓ Features: ${features?.value?.map { (it as JSON5Value.String).value }}") // ["auth", "analytics"]
108111
}
109112
else -> println("Parsed value is not an object")
110113
}
111114

112115
// Stringify Kotlin objects to JSON5
113-
val data = mapOf(
114-
"name" to "MyApp",
115-
"version" to 2,
116-
"enabled" to true
117-
)
116+
val data =
117+
mapOf(
118+
"name" to "MyApp",
119+
"version" to 2,
120+
"enabled" to true,
121+
)
118122
val json5String = JSON5.stringify(data)
119123
println("✓ Stringified to JSON5: $json5String")
120-
121124
} catch (e: Exception) {
122125
println("⚠️ Error in basic parsing and stringifying test: ${e.message}")
123126
e.printStackTrace()
@@ -129,47 +132,48 @@ fun testBasicParsingAndStringifying() {
129132
*/
130133
fun testKotlinxSerializationIntegration() {
131134
println("\n--- Testing kotlinx.serialization Integration ---")
132-
135+
133136
try {
134137
@Serializable
135138
data class Config(
136139
val appName: String,
137140
val version: Int,
138141
val features: List<String>,
139-
val settings: Map<String, String>
142+
val settings: Map<String, String>,
140143
)
141144

142145
// Serialize to JSON5
143-
val config = Config(
144-
appName = "MyApp",
145-
version = 2,
146-
features = listOf("auth", "analytics"),
147-
settings = mapOf("theme" to "dark", "lang" to "en")
148-
)
146+
val config =
147+
Config(
148+
appName = "MyApp",
149+
version = 2,
150+
features = listOf("auth", "analytics"),
151+
settings = mapOf("theme" to "dark", "lang" to "en"),
152+
)
149153

150154
val json5 = JSON5.encodeToString(Config.serializer(), config)
151155
println("✓ Serialized to JSON5: $json5")
152156

153157
// Deserialize from JSON5 (with comments and formatting)
154-
val json5WithComments = """
155-
{
156-
// Application configuration
157-
appName: 'MyApp',
158-
version: 2, // current version
159-
features: [
160-
'auth',
161-
'analytics', // trailing comma OK
162-
],
163-
settings: {
164-
theme: 'dark',
165-
lang: 'en',
158+
val json5WithComments =
159+
"""
160+
{
161+
// Application configuration
162+
appName: 'MyApp',
163+
version: 2, // current version
164+
features: [
165+
'auth',
166+
'analytics', // trailing comma OK
167+
],
168+
settings: {
169+
theme: 'dark',
170+
lang: 'en',
171+
}
166172
}
167-
}
168-
""".trimIndent()
173+
""".trimIndent()
169174

170175
val decoded = JSON5.decodeFromString(Config.serializer(), json5WithComments)
171176
println("✓ Deserialized from JSON5 with comments: $decoded")
172-
173177
} catch (e: Exception) {
174178
println("⚠️ Error in kotlinx.serialization integration test: ${e.message}")
175179
e.printStackTrace()
@@ -181,21 +185,24 @@ fun testKotlinxSerializationIntegration() {
181185
*/
182186
fun testAdvancedFeatures() {
183187
println("\n--- Testing Advanced Features ---")
184-
188+
185189
try {
186190
// JSON5 supports various number formats
187-
val numbers = JSON5.parse("""
188-
{
189-
hex: 0xDECAF,
190-
leadingDot: .8675309,
191-
trailingDot: 8675309.,
192-
positiveSign: +1,
193-
scientific: 6.02e23,
194-
infinity: Infinity,
195-
negativeInfinity: -Infinity,
196-
notANumber: NaN
197-
}
198-
""".trimIndent())
191+
val numbers =
192+
JSON5.parse(
193+
"""
194+
{
195+
hex: 0xDECAF,
196+
leadingDot: .8675309,
197+
trailingDot: 8675309.,
198+
positiveSign: +1,
199+
scientific: 6.02e23,
200+
infinity: Infinity,
201+
negativeInfinity: -Infinity,
202+
notANumber: NaN
203+
}
204+
""".trimIndent(),
205+
)
199206

200207
println("✓ Parsed numbers JSON5 successfully: $numbers")
201208

@@ -205,25 +212,30 @@ fun testAdvancedFeatures() {
205212
val hex = numbers.value["hex"] as? JSON5Value.Number
206213
val infinity = numbers.value["infinity"] as? JSON5Value.Number.PositiveInfinity
207214
val nan = numbers.value["notANumber"] as? JSON5Value.Number.NaN
208-
209-
println("✓ Hex value: ${(hex as? JSON5Value.Number.Hexadecimal)?.value ?: (hex as? JSON5Value.Number.Decimal)?.value?.toLong()}") // 912559
215+
216+
println(
217+
"✓ Hex value: ${(hex as? JSON5Value.Number.Hexadecimal)?.value ?: (hex as? JSON5Value.Number.Decimal)?.value?.toLong()}",
218+
) // 912559
210219
println("✓ Is infinity: ${infinity != null}") // true
211220
println("✓ Is NaN: ${nan != null}") // true
212221
}
213222
else -> println("Numbers value is not an object")
214223
}
215224

216225
// Multi-line strings and comments
217-
val complex = JSON5.parse("""
218-
{
219-
multiLine: "This is a \
220-
multi-line string",
221-
/* Block comment
222-
spanning multiple lines */
223-
singleQuoted: 'Can contain "double quotes"',
224-
unquoted: 'keys work too'
225-
}
226-
""".trimIndent())
226+
val complex =
227+
JSON5.parse(
228+
"""
229+
{
230+
multiLine: "This is a \
231+
multi-line string",
232+
/* Block comment
233+
spanning multiple lines */
234+
singleQuoted: 'Can contain "double quotes"',
235+
unquoted: 'keys work too'
236+
}
237+
""".trimIndent(),
238+
)
227239

228240
println("✓ Parsed complex JSON5 successfully: $complex")
229241

@@ -232,13 +244,12 @@ fun testAdvancedFeatures() {
232244
is JSON5Value.Object -> {
233245
val multiLine = complex.value["multiLine"] as? JSON5Value.String
234246
val singleQuoted = complex.value["singleQuoted"] as? JSON5Value.String
235-
247+
236248
println("✓ Multi-line: ${multiLine?.value}")
237249
println("✓ Single quoted: ${singleQuoted?.value}")
238250
}
239251
else -> println("Complex value is not an object")
240252
}
241-
242253
} catch (e: Exception) {
243254
println("⚠️ Error in advanced features test: ${e.message}")
244255
e.printStackTrace()
@@ -250,7 +261,7 @@ fun testAdvancedFeatures() {
250261
*/
251262
fun testMigrationCompatibility() {
252263
println("\n--- Testing Migration Compatibility ---")
253-
264+
254265
try {
255266
// New API - Type-safe approach (recommended)
256267
val result = JSON5.parse("""{"key": "value"}""")
@@ -263,8 +274,8 @@ fun testMigrationCompatibility() {
263274
}
264275

265276
// Alternative: Convert to raw objects when needed
266-
fun JSON5Value.toRawObject(): Any? {
267-
return when (this) {
277+
fun JSON5Value.toRawObject(): Any? =
278+
when (this) {
268279
is JSON5Value.Null -> null
269280
is JSON5Value.Boolean -> this.value
270281
is JSON5Value.String -> this.value
@@ -277,15 +288,13 @@ fun testMigrationCompatibility() {
277288
is JSON5Value.Object -> this.value.mapValues { it.value.toRawObject() }
278289
is JSON5Value.Array -> this.value.map { it.toRawObject() }
279290
}
280-
}
281291

282292
// Using the helper for compatibility
283293
val rawResult = JSON5.parse("""{"key": "value"}""").toRawObject()
284294
val map = rawResult as Map<String, Any?>
285295
println("✓ Compatibility helper result: ${map["key"]}") // "value"
286-
287296
} catch (e: Exception) {
288297
println("⚠️ Error in migration compatibility test: ${e.message}")
289298
e.printStackTrace()
290299
}
291-
}
300+
}

app/src/main/kotlin/TestModels.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ data class Address(
77
val street: String,
88
val city: String,
99
val state: String,
10-
val zip: String
10+
val zip: String,
1111
)
1212

1313
@Serializable
1414
data class Contact(
1515
val email: String,
16-
val phone: String? = null
16+
val phone: String? = null,
1717
)
1818

1919
@Serializable
@@ -23,5 +23,5 @@ data class Employee(
2323
val position: String,
2424
val address: Address,
2525
val contact: Contact,
26-
val isActive: Boolean = true
26+
val isActive: Boolean = true,
2727
)

benchmark/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ plugins {
22
// Apply the shared build logic from a convention plugin.
33
id("buildsrc.convention.kotlin-jvm")
44
alias(libs.plugins.kotlinPluginSerialization)
5+
alias(libs.plugins.kotlinter)
56
application
67
}
78

0 commit comments

Comments
 (0)