A robust JSON5 parser and serializer for Kotlin that extends JSON with helpful features like comments, trailing commas, and unquoted keys while maintaining full backward compatibility with JSON.
Built with kotlinx.serialization for seamless integration with Kotlin projects and type-safe serialization.
All valid JSON is valid JSON5, ensuring backward compatibility with existing JSON data.
Simple drop-in replacement for standard JSON with enhanced functionality.
Thoroughly benchmarked against other implementations to ensure reliable performance.
Add JSON5 support to your Kotlin project in minutes
View on GitHub Quick Start GuideJSON5 extends JSON with the following developer-friendly features:
Both line comments //
and block comments /* */
are supported.
{ // This is a line comment "name": "John", /* block comment */ "age": 30 }
Object keys can be unquoted if they're valid identifiers.
{ name: "John", age: 30, isActive: true }
Trailing commas are allowed in objects and arrays.
{ "items": [ "apple", "banana", // ← trailing comma OK ], }
Strings can use single quotes in addition to double quotes.
{ 'name': 'John', "age": 30 }
Strings can span multiple lines with backslash at line end.
{ "description": "This is a \ very long string that \ spans multiple lines" }
Support for hexadecimal, infinity, NaN, and leading/trailing decimal points.
{ hex: 0xFF, leadingDecimal: .5, trailingDecimal: 2., infinity: Infinity, notANumber: NaN }
Add the dependency to your build.gradle.kts
:
repositories { maven { name = "GitHubPackages" url = uri("https://maven.pkg.github.com/hossain-khan/json5-kotlin") credentials { username = project.findProperty("gpr.user") as String? ?: System.getenv("USERNAME") password = project.findProperty("gpr.key") as String? ?: System.getenv("TOKEN") } } } dependencies { implementation("hossain.dev:json5kt:1.2.0") // Or, latest release }
import dev.hossain.json5kt.JSON5 // Parse JSON5 string val json5String = """ { // User information name: 'John Doe', age: 30, skills: [ 'Kotlin', 'JSON5', // trailing comma ], } """ val user = JSON5.decodeFromString<User>(json5String) // Serialize to JSON5 val json5Output = JSON5.encodeToString(user)
@Serializable data class User( val name: String, val age: Int, val skills: List<String> ) // Configure JSON5 with custom settings val json5 = JSON5 { prettyPrint = true ignoreUnknownKeys = true } val user = json5.decodeFromString<User>(json5String)
Interactive Performance Comparison: JSON5 vs JSON vs External-JSON5
Generated from benchmark data: June 25, 2025
Test Environment: JVM warmup with 100 iterations, followed by 1000 iterations per test case.
Data Types: SimplePerson, ComplexPerson, Company, NumberTypes, CollectionTypes, SimplePersonList100, ComplexPersonList50
Operations: Serialization (object to JSON string) and Deserialization (JSON string to object)
Libraries: