Skip to content

Commit 211a692

Browse files
committed
[skip ci] Add README.md for ByteBuf Serialization library with usage examples and installation instructions
1 parent befb20e commit 211a692

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed

README.md

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# ByteBuf Serialization for kotlinx.serialization
2+
3+
A Kotlin Serialization format for encoding and decoding data into Netty's `ByteBuf`. This library
4+
provides seamless integration with `kotlinx.serialization`, allowing efficient binary serialization
5+
for network communication and other performance-critical applications.
6+
7+
## 🚀 Features
8+
- Serialize and deserialize objects into `ByteBuf`
9+
- Supports primitive types, collections, and custom objects
10+
- Extensible API for adding custom serializers (for example, VarInt, UUID)
11+
- Type-safe error handling for invalid `ByteBuf` types
12+
13+
---
14+
15+
## 📦 Installation (Gradle Kotlin DSL)
16+
17+
Add the dependency to your `build.gradle.kts`:
18+
19+
```kotlin
20+
repositories {
21+
maven("https://repo.slne.dev/repository/maven-public/") { name = "slne-maven-public" }
22+
}
23+
24+
dependencies {
25+
implementation("dev.slne.surf:kotlin-byte-buf-serializer:1.0.0") // ships netty-all by default – you may want to exclude it
26+
}
27+
```
28+
29+
Enable Kotlin Serialization in your project:
30+
31+
```kotlin
32+
plugins {
33+
kotlin("plugin.serialization") version "2.1.10"
34+
}
35+
```
36+
37+
---
38+
39+
## 🔧 Usage
40+
41+
### 1️⃣ Basic Serialization
42+
43+
```kotlin
44+
@Serializable
45+
data class Player(val name: String, val score: Int)
46+
47+
val player = Player("Alice", 42)
48+
val buf: ByteBuf = Unpooled.buffer()
49+
50+
// Encode to ByteBuf
51+
Buf.encodeToBuf(buf, player)
52+
53+
// Decode from ByteBuf
54+
val decodedPlayer = Buf.decodeFromBuf<Player>(buf)
55+
println(decodedPlayer) // Player(name="Alice", score=42)
56+
```
57+
58+
### 2️⃣ Using Byte Arrays
59+
60+
```kotlin
61+
val bytes = Buf.encodeToByteArray(player)
62+
val decodedFromBytes = Buf.decodeFromByteArray<Player>(bytes)
63+
```
64+
65+
### 3️⃣ Custom Serializer Example (VarInt)
66+
67+
```kotlin
68+
class MyCustomByteBuf(private val buf: ByteBuf) : ByteBuf() {
69+
fun writeVarInt(value: Int) {
70+
// Custom implementation
71+
}
72+
73+
fun readVarInt(): Int {
74+
// Custom implementation
75+
}
76+
77+
// other ByteBuf methods
78+
}
79+
80+
object VarIntSerializer : KBufSerializer<Int, MyCustomByteBuf> {
81+
override val descriptor = PrimitiveSerialDescriptor("VarInt", PrimitiveKind.INT)
82+
override val bufClass = MyCustomByteBuf::class
83+
84+
override fun serialize0(buf: MyCustomByteBuf, value: Int) {
85+
buf.writeVarInt(value)
86+
}
87+
88+
override fun deserialize0(buf: MyCustomByteBuf): Int {
89+
return buf.readVarInt()
90+
}
91+
}
92+
```
93+
94+
Register the custom serializer:
95+
96+
```kotlin
97+
val customModule = SerializersModule {
98+
contextual(VarIntSerializer)
99+
}
100+
```
101+
102+
---
103+
104+
## 🛠 Advanced Configuration
105+
106+
### Customizing `Buf`
107+
108+
You can create a custom instance of `Buf` with a specific `SerializersModule`:
109+
110+
```kotlin
111+
val bufFormat = Buf(customModule)
112+
```
113+
114+
### Type-Safe Serialization with Error Handling
115+
116+
This library provides **explicit type validation** for `ByteBuf`. If an incorrect type is used, a
117+
clear error message is thrown.
118+
119+
```kotlin
120+
val wrongBuf: ByteBuf = Unpooled.directBuffer()
121+
val encoder = BufEncoder(wrongBuf, customModule, true)
122+
123+
assertFailsWith<IllegalArgumentException> {
124+
VarIntSerializer.serialize(encoder, 42)
125+
}
126+
```
127+
128+
---
129+
130+
## 📝 License
131+
132+
This project is licensed under the MIT License.
133+
134+
---
135+
136+
## 👨‍💻 Contributing
137+
138+
Pull requests are welcome! Feel free to open an issue if you find a bug or have a feature request.

0 commit comments

Comments
 (0)