Packable

July 20, 2024 · View on GitHub

Maven Central

1. Overview

Packable is a well-designed serialization framework.
It has been implemented multiple languages, it's effective and easy to use.
It can be used for object serialization/deserialization and message pack up, to storage or RPC.

Packable has the following advantages:

  1. Fast encoding/decoding.
  2. Encoded message is compact (with small size).
  3. Easy to use.
  4. The code is light.
  5. Support multiple types and compression strategies.
  6. Supports multiple languages and cross-platforms.

Packable currently implements version of Java, Kotlin, C++, C#, Objective-C and Go.

How to use

Here is usage of Kotlin version, and APIs on other platform are similar to Kotlin.

The jar has been published to Maven Central:

java:

dependencies {
    implementation 'io.github.billywei01:packable-java:2.1.2'
}

kotlin:

dependencies {
    implementation 'io.github.billywei01:packable-kotlin:2.1.3'
}

Suppose there is a class like this:

data class Person(
    val name: String,
    val age: Int
)

Make an adapter like this:

object PersonAdapter : TypeAdapter<Person> {
    override fun encode(encoder: PackEncoder, target: Person) {
        encoder
            .putString(0, target.name)
            .putInt(1, target.age)
    }

    override fun decode(decoder: PackDecoder): Person {
        return Person(
            name = decoder.getString(0),
            age = decoder.getInt(1)
        )
    }
}

Serialization / Deserialization:

private fun testEncodeObject() {
    val person = Person("Tom", 20)

    val encoded = PersonAdapter.encode(person)
    val decoded = PersonAdapter.decode(encoded)

    println("Person: ${person == decoded}")
}

private fun testEncodeObjectList() {
    val list = listOf(
        Person("Tom", 20),
        Person("Mary", 18)
    )

    val encoded = PersonAdapter.encodeObjectList(list)
    val decoded = PersonAdapter.decodeObjectList(encoded)

    println("Person list: ${list == decoded}")
}

3. Benchmark

Testing object:

  • Packable
  • Protobuf
  • Gson

Testing Device: Macbook Pro

Testing Code:Main

Testing result:

数据大小(byte)序列化(ms)反序列化(ms)
packable2564756 (56%)88
protobuf2627081 (59%)1617
gson4427344 (100%)5850

4. Format

See: Packable Format

License

See the LICENSE file for license rights and limitations.