Skip to content

Commit ff603ec

Browse files
committed
[UPDATE] Refine test app with models too
1 parent 2c08f01 commit ff603ec

File tree

5 files changed

+77
-2
lines changed

5 files changed

+77
-2
lines changed

app/build.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ plugins {
55

66
// Apply the Application plugin to add support for building an executable JVM application.
77
application
8+
9+
kotlin("plugin.serialization") version "1.9.23"
810
}
911

1012
dependencies {
1113
// Project "app" depends on project "lib"
1214
implementation(project(":lib"))
15+
16+
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3")
1317
}
1418

1519
application {

app/src/main/kotlin/App.kt

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package org.json5.app
22

33
import dev.hossain.json5kt.JSON5
44

5+
/**
6+
* Main application entry point for testing JSON5 parsing and serialization.
7+
*/
58
fun main() {
69
val json5files = listOf(
7-
"empty-json.json5",
810
"simple-object.json5",
911
"array-example.json5",
1012
"numeric-formats.json5",
@@ -32,4 +34,29 @@ fun main() {
3234
}
3335
println("\n===============================\n\n")
3436
}
37+
38+
// Test serialization and deserialization of Employee model
39+
testEmployeeSerialization()
3540
}
41+
42+
43+
fun testEmployeeSerialization() {
44+
val fileName = "employee-example.json5"
45+
val resourceStream = object {}.javaClass.getResourceAsStream("/$fileName")
46+
if (resourceStream != null) {
47+
val content = resourceStream.bufferedReader().use { it.readText() }
48+
println("\n=== Employee Serialization/Deserialization Test ===")
49+
println("Original JSON5:\n$content")
50+
try {
51+
val employee: Employee = JSON5.decodeFromString(Employee.serializer(), content)
52+
println("\nDeserialized Employee object: $employee")
53+
val serialized: String = JSON5.encodeToString(Employee.serializer(), employee)
54+
println("\nSerialized back to JSON5:\n$serialized")
55+
} catch (e: Exception) {
56+
println("\n⚠️ Error during Employee serialization/deserialization: ${e.message}")
57+
}
58+
println("\n===============================================\n")
59+
} else {
60+
println("⚠️ Error: Could not find resource: $fileName")
61+
}
62+
}

app/src/main/kotlin/TestModels.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.json5.app
2+
3+
import kotlinx.serialization.Serializable
4+
5+
@Serializable
6+
data class Address(
7+
val street: String,
8+
val city: String,
9+
val state: String,
10+
val zip: String
11+
)
12+
13+
@Serializable
14+
data class Contact(
15+
val email: String,
16+
val phone: String? = null
17+
)
18+
19+
@Serializable
20+
data class Employee(
21+
val id: Int,
22+
val name: String,
23+
val position: String,
24+
val address: Address,
25+
val contact: Contact,
26+
val isActive: Boolean = true
27+
)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Example Employee data in JSON5 format
2+
{
3+
id: 101,
4+
name: 'Jane Doe',
5+
position: 'Software Engineer',
6+
address: {
7+
street: '123 Main St',
8+
city: 'Springfield',
9+
state: 'IL',
10+
zip: '62704',
11+
},
12+
contact: {
13+
14+
phone: '+1-555-1234',
15+
},
16+
isActive: true, // Optional, defaults to true
17+
}
18+

app/src/main/resources/empty-json.json5

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)