diff --git a/app/build.gradle.kts b/app/build.gradle.kts index dfc685d..bcc8db5 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -5,11 +5,13 @@ plugins { // Apply the Application plugin to add support for building an executable JVM application. application + alias(libs.plugins.kotlinPluginSerialization) } dependencies { // Project "app" depends on project "lib" implementation(project(":lib")) + implementation(libs.kotlinxSerialization) } 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