Packable
July 20, 2024 · View on GitHub
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:
- Fast encoding/decoding.
- Encoded message is compact (with small size).
- Easy to use.
- The code is light.
- Support multiple types and compression strategies.
- 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) | |
|---|---|---|---|
| packable | 2564756 (56%) | 8 | 8 |
| protobuf | 2627081 (59%) | 16 | 17 |
| gson | 4427344 (100%) | 58 | 50 |
4. Format
See: Packable Format
License
See the LICENSE file for license rights and limitations.