From ff603ec5ca6763af2d4250a900564e10b87ed414 Mon Sep 17 00:00:00 2001 From: Hossain Khan Date: Mon, 9 Jun 2025 19:52:10 -0400 Subject: [PATCH 1/2] [UPDATE] Refine test app with models too --- app/build.gradle.kts | 4 +++ app/src/main/kotlin/App.kt | 29 ++++++++++++++++++- app/src/main/kotlin/TestModels.kt | 27 +++++++++++++++++ app/src/main/resources/employee-example.json5 | 18 ++++++++++++ app/src/main/resources/empty-json.json5 | 1 - 5 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 app/src/main/kotlin/TestModels.kt create mode 100644 app/src/main/resources/employee-example.json5 delete mode 100644 app/src/main/resources/empty-json.json5 diff --git a/app/build.gradle.kts b/app/build.gradle.kts index dfc685d..c7c0b61 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -5,11 +5,15 @@ plugins { // Apply the Application plugin to add support for building an executable JVM application. application + + kotlin("plugin.serialization") version "1.9.23" } dependencies { // Project "app" depends on project "lib" implementation(project(":lib")) + + implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3") } application { diff --git a/app/src/main/kotlin/App.kt b/app/src/main/kotlin/App.kt index 1516237..192d95b 100644 --- a/app/src/main/kotlin/App.kt +++ b/app/src/main/kotlin/App.kt @@ -2,9 +2,11 @@ package org.json5.app import dev.hossain.json5kt.JSON5 +/** + * Main application entry point for testing JSON5 parsing and serialization. + */ fun main() { val json5files = listOf( - "empty-json.json5", "simple-object.json5", "array-example.json5", "numeric-formats.json5", @@ -32,4 +34,29 @@ fun main() { } println("\n===============================\n\n") } + + // Test serialization and deserialization of Employee model + testEmployeeSerialization() } + + +fun testEmployeeSerialization() { + val fileName = "employee-example.json5" + val resourceStream = object {}.javaClass.getResourceAsStream("/$fileName") + if (resourceStream != null) { + val content = resourceStream.bufferedReader().use { it.readText() } + println("\n=== Employee Serialization/Deserialization Test ===") + println("Original JSON5:\n$content") + try { + val employee: Employee = JSON5.decodeFromString(Employee.serializer(), content) + println("\nDeserialized Employee object: $employee") + val serialized: String = JSON5.encodeToString(Employee.serializer(), employee) + println("\nSerialized back to JSON5:\n$serialized") + } catch (e: Exception) { + println("\n⚠️ Error during Employee serialization/deserialization: ${e.message}") + } + println("\n===============================================\n") + } else { + println("⚠️ Error: Could not find resource: $fileName") + } +} \ No newline at end of file diff --git a/app/src/main/kotlin/TestModels.kt b/app/src/main/kotlin/TestModels.kt new file mode 100644 index 0000000..01d3e4b --- /dev/null +++ b/app/src/main/kotlin/TestModels.kt @@ -0,0 +1,27 @@ +package org.json5.app + +import kotlinx.serialization.Serializable + +@Serializable +data class Address( + val street: String, + val city: String, + val state: String, + val zip: String +) + +@Serializable +data class Contact( + val email: String, + val phone: String? = null +) + +@Serializable +data class Employee( + val id: Int, + val name: String, + val position: String, + val address: Address, + val contact: Contact, + val isActive: Boolean = true +) diff --git a/app/src/main/resources/employee-example.json5 b/app/src/main/resources/employee-example.json5 new file mode 100644 index 0000000..4b7cdb5 --- /dev/null +++ b/app/src/main/resources/employee-example.json5 @@ -0,0 +1,18 @@ +// Example Employee data in JSON5 format +{ + id: 101, + name: 'Jane Doe', + position: 'Software Engineer', + address: { + street: '123 Main St', + city: 'Springfield', + state: 'IL', + zip: '62704', + }, + contact: { + email: 'jane.doe@example.com', + phone: '+1-555-1234', + }, + isActive: true, // Optional, defaults to true +} + diff --git a/app/src/main/resources/empty-json.json5 b/app/src/main/resources/empty-json.json5 deleted file mode 100644 index 9e26dfe..0000000 --- a/app/src/main/resources/empty-json.json5 +++ /dev/null @@ -1 +0,0 @@ -{} \ No newline at end of file From 03ae352fb0d21a5eb86cdc87acad93b2b3b62ac8 Mon Sep 17 00:00:00 2001 From: Hossain Khan Date: Mon, 9 Jun 2025 20:11:28 -0400 Subject: [PATCH 2/2] [MINOR] Use version catalog --- app/build.gradle.kts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index c7c0b61..bcc8db5 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -5,15 +5,13 @@ plugins { // Apply the Application plugin to add support for building an executable JVM application. application - - kotlin("plugin.serialization") version "1.9.23" + alias(libs.plugins.kotlinPluginSerialization) } dependencies { // Project "app" depends on project "lib" implementation(project(":lib")) - - implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3") + implementation(libs.kotlinxSerialization) } application {