Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
29 changes: 28 additions & 1 deletion app/src/main/kotlin/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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")
}
}
27 changes: 27 additions & 0 deletions app/src/main/kotlin/TestModels.kt
Original file line number Diff line number Diff line change
@@ -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
)
18 changes: 18 additions & 0 deletions app/src/main/resources/employee-example.json5
Original file line number Diff line number Diff line change
@@ -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: '[email protected]',
phone: '+1-555-1234',
},
isActive: true, // Optional, defaults to true
}

1 change: 0 additions & 1 deletion app/src/main/resources/empty-json.json5

This file was deleted.