Skip to content

JosephSanjaya/ktoon

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

16 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐ŸŽจ KToon

Efficient serialization for Kotlin Multiplatform

Kotlin Compose Multiplatform License Platform

Cut your API payload sizes by 30-60% without changing your code

Features โ€ข Quick Start โ€ข Demo Apps โ€ข Documentation โ€ข Roadmap


๐ŸŽฏ What is KToon?

KToon is a Kotlin Multiplatform serialization library implementing the TOON format (Token-Oriented Object Notation). Think of it as JSON's efficient cousinโ€”perfect for mobile apps, IoT devices, and anywhere bandwidth matters. Build for kiroween hackaton

Read Articles: here

JSON (553 bytes):

[
  {"id": 1, "name": "Alice", "email": "alice@example.com", "age": 30},
  {"id": 2, "name": "Bob", "email": "bob@example.com", "age": 25},
  {"id": 3, "name": "Charlie", "email": "charlie@example.com", "age": 35}
]

TOON (179 bytes - 67% smaller!):

users[3]{id,name,email,age}:
  1,Alice,alice@example.com,30
  2,Bob,bob@example.com,25
  3,Charlie,charlie@example.com,35

โœจ Features

  • ๐Ÿš€ 30-60% smaller payloads - Less data = faster loading
  • ๐Ÿ“ฑ True multiplatform - Android, iOS, Desktop, Web (JS + WASM)
  • ๐Ÿ”Œ Drop-in replacement - Works with your existing @Serializable classes
  • โšก Fast - Single-pass O(N) serialization
  • ๐ŸŽฏ Type-safe - Full kotlinx.serialization integration
  • ๐Ÿ› ๏ธ Ktor ready - Client and server ContentNegotiation support
  • ๐Ÿงฉ Minimal - ~1000 lines of pure Kotlin, zero platform-specific code

๐Ÿš€ Quick Start

Installation

โš ๏ธ TODO: Publish to Maven Central

For now, clone and include as a local module:

git clone https://github.com/JosephSanjaya/ktoon.git

Add to your settings.gradle.kts:

include(:<"path/to/ktoon">)

Basic Usage

import io.ktoon.Toon
import kotlinx.serialization.Serializable

@Serializable
data class User(val id: Int, val name: String, val email: String)

fun main() {
    val users = listOf(
        User(1, "Alice", "alice@example.com"),
        User(2, "Bob", "bob@example.com")
    )
    
    // Serialize to TOON
    val toon = Toon.encodeToString(users)
    println(toon)
    // Output:
    // users[2]{id,name,email}:
    //   1,Alice,alice@example.com
    //   2,Bob,bob@example.com
    
    // Deserialize from TOON
    val decoded = Toon.decodeFromString<List<User>>(toon)
    println(decoded) // [User(1, Alice, alice@example.com), User(2, Bob, bob@example.com)]
}

Ktor Client Integration

import io.ktor.client.*
import io.ktor.client.engine.cio.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.client.request.*
import io.ktoon.ktor.toon

val client = HttpClient(CIO) {
    install(ContentNegotiation) {
        json()  // Fallback to JSON
        toon()  // Add TOON support
    }
}

// Automatic serialization/deserialization
val users = client.get("https://api.example.com/users") {
    headers {
        append("Accept", "application/toon")
    }
}.body<List<User>>()

Ktor Server Integration

import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import io.ktor.server.plugins.contentnegotiation.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktoon.ktor.server.toon

fun main() {
    embeddedServer(Netty, port = 8080) {
        install(ContentNegotiation) {
            json()  // For application/json
            toon()  // For application/toon
        }
        
        routing {
            get("/users") {
                val users = listOf(
                    User(1, "Alice", "alice@example.com"),
                    User(2, "Bob", "bob@example.com")
                )
                call.respond(users) // Automatically uses TOON if Accept: application/toon
            }
        }
    }.start(wait = true)
}

๐ŸŽฎ Demo Apps

This repository includes two demo applications showcasing KToon's versatility:

1. API Demo App (Network-focused)

Real-time format comparison with live backend server.

# Start the backend server
./gradlew :backend:run

# Run the Compose Multiplatform app
./gradlew :composeApp:run

# Or test with curl
curl -H "Accept: application/json" http://localhost:8080/users
curl -H "Accept: application/toon" http://localhost:8080/users

Features:

  • Side-by-side JSON vs TOON comparison
  • Real-time byte count and savings metrics
  • Interactive API testing
  • Content negotiation demonstration

2. Offline Data Manager (Coming Soon)

Local-first architecture with efficient data storage.

Features:

  • Efficient local data storage using TOON
  • Batch sync operations
  • Configuration file management
  • Seed data bundling

๐Ÿ“š Documentation

๐Ÿ—๏ธ Project Structure

ktoon/
โ”œโ”€โ”€ ktoon-core/              # Core serialization engine (~1000 lines)
โ”‚   โ”œโ”€โ”€ Toon.kt             # Public API
โ”‚   โ”œโ”€โ”€ ToonEncoder.kt      # Serialization logic
โ”‚   โ”œโ”€โ”€ ToonDecoder.kt      # Deserialization logic
โ”‚   โ””โ”€โ”€ ToonLexer.kt        # Tokenization
โ”œโ”€โ”€ ktoon-ktor/             # Ktor client integration
โ”œโ”€โ”€ ktoon-ktor-server/      # Ktor server integration
โ”œโ”€โ”€ backend/                # Demo backend server
โ””โ”€โ”€ composeApp/             # Demo Compose Multiplatform app

๐Ÿ›ฃ๏ธ Roadmap

v0.3.0 - More Integrations

  • Retrofit support - ContentConverter for Retrofit
  • OkHttp interceptor

v1.0.0 - Production Ready

  • Maven Central publication
  • Comprehensive documentation site
  • Binary TOON format
  • Compression integration (TOON + gzip)
  • Android Studio plugin for visualization

๐Ÿค Contributing

Contributions are welcome! Whether it's:

  • ๐Ÿ› Bug reports
  • ๐Ÿ’ก Feature requests
  • ๐Ÿ“ Documentation improvements
  • ๐Ÿ”ง Code contributions

Please open an issue or PR on GitHub.

๐ŸŒŸ Why KToon?

Built by Android engineers who noticed the LLM industry was already benefiting from token-efficient formats. We asked: why should only AI companies get these benefits? Mobile apps face the same challengesโ€”limited bandwidth, expensive data plans, slow networks.

KToon brings proven efficiency techniques to the entire Kotlin ecosystem. Whether you're building a social app, e-commerce platform, or IoT controller, you deserve efficient data serialization.

The skeleton is ready. What will you build with it?


Star History

Star History

Made with โ˜• by developers who care about efficiency

โญ Star us on GitHub โ€ข ๐Ÿ“– Read the docs โ€ข ๐Ÿ’ฌ Join discussions

About

KToon is a Kotlin Multiplatform serialization library implementing the TOON format (Token-Oriented Object Notation). Think of it as JSON's efficient cousin perfect for mobile apps, IoT devices, and anywhere bandwidth matters.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages