kencode

Top-level

Entry pointUse it to
EncodedFormatEncode a @Serializable value as a short string and back.
PackedFormatGet the compact ByteArray without the text layer.
ByteEncodingEncode raw bytes with Base62, Base36, Base64, or Base85.
val encoded = EncodedFormat.encodeToString(payload)
val decoded = EncodedFormat.decodeFromString<Payload>(encoded)

Configure formats with the builder lambda: set the codec, add a checksum, or chain PayloadTransforms. Annotate Int/Long fields with PackedType to choose an IntPacking.

Types

Link copied to clipboard
interface Alphabet

Maps digit values to characters and back.

Link copied to clipboard
object Base36 : BaseRadix

Base36 codec: digits and lowercase letters.

Link copied to clipboard
object Base62 : BaseRadix

Base62 codec over BASE_62.

Link copied to clipboard
open class Base64(val alphabet: CharArray) : ByteEncoding

Padded RFC 4648 Base64 codec.

Link copied to clipboard
object Base64Url : Base64

Base64 with the BASE_64_URL alphabet.

Link copied to clipboard
open class Base85(alphabet: CharArray) : ByteEncoding

Base85 codec: 4 bytes to 5 characters, and a final 1–3 bytes to 2–4 characters.

Link copied to clipboard
open class BaseRadix(alphabet: Alphabet, val blockSize: Int = 32) : ByteEncoding

Base-N codec over any Alphabet, converting blockSize bytes at a time. With more than 256 characters, partial blocks get a sentinel byte that preserves their length and leading zeros.

Link copied to clipboard
interface ByteEncoding

Bidirectional byte–text codec such as Base62 or Base64.

Link copied to clipboard
class CharAlphabet(chars: String) : Alphabet

Alphabet of the characters in chars.

Link copied to clipboard
interface Checksum

Checksum with a fixed-size digest.

Link copied to clipboard

Replaces leading zero bytes with a 0x00 sentinel and a varint count, which pays off from three zeros. Payloads starting with a non-zero byte pass through unchanged. At most 65,536 zeros are stripped.

Link copied to clipboard
abstract class Crc : Checksum

Bitwise CRC up to 32 bits, parameterized per the Rocksoft model.

Link copied to clipboard
open class Crc16(poly: Int = 4129, init: Int = 65535, refin: Boolean = true, refout: Boolean = true, xorOut: Int = 65535) : Crc

16-bit CRC; CRC-16/X-25 by default.

Link copied to clipboard
open class Crc32(poly: Int = 79764919, init: Int = 0xFFFFFFFF.toInt(), refin: Boolean = true, refout: Boolean = true, xorOut: Int = 0xFFFFFFFF.toInt()) : Crc

32-bit CRC; CRC-32/ISO-HDLC by default.

Link copied to clipboard
open class Crc8(poly: Int = 7, init: Int = 0, refin: Boolean = false, refout: Boolean = false, xorOut: Int = 0) : Crc

8-bit CRC; CRC-8/SMBUS by default.

Link copied to clipboard
data class EncodedConfiguration(val codec: ByteEncoding = Base62, val transform: PayloadTransform? = null, val binaryFormat: BinaryFormat = PackedFormat.Default)
Link copied to clipboard
open class EncodedFormat(val configuration: EncodedConfiguration) : StringFormat

StringFormat that serializes with a BinaryFormat, applies an optional PayloadTransform, and encodes the bytes with a ByteEncoding. Defaults to PackedFormat and Base62.

Link copied to clipboard
Link copied to clipboard

Integer encoding for Int and Long fields in PackedFormat.

Link copied to clipboard
data class PackedConfiguration(val defaultEncoding: IntPacking = IntPacking.DEFAULT)
Link copied to clipboard
class PackedDecoder : Decoder, CompositeDecoder

PackedFormat decoder.

Link copied to clipboard
class PackedEncoder : Encoder, CompositeEncoder

PackedFormat encoder.

Link copied to clipboard
open class PackedFormat(val configuration: PackedConfiguration = PackedConfiguration(), val serializersModule: SerializersModule = EmptySerializersModule()) : BinaryFormat

Compact BinaryFormat for Kotlin classes.

Link copied to clipboard
Link copied to clipboard
@SerialInfo
@Target(allowedTargets = [AnnotationTarget.PROPERTY])
annotation class PackedType(val type: IntPacking)

Sets the IntPacking of an Int or Long field. Without it, kotlinx.serialization.protobuf.ProtoType applies if present, then PackedConfiguration.defaultEncoding.

Link copied to clipboard

Byte transform between the binary format and the text codec, such as a checksum, encryption, or error correction. decode(encode(data)) must equal data.

Link copied to clipboard
class UnicodeRangeAlphabet(start: Int = 32, val size: Int = 0xD800 - 0x0020) : Alphabet

size consecutive BMP characters from start. Defaults to U+0020–U+D7FF, the largest range below the surrogate block.

Properties

Link copied to clipboard
const val ASCII85: String

ASCII85 alphabet, ! through u.

Link copied to clipboard
const val BASE_62: String

Base62 alphabet: digits, lowercase letters, then uppercase letters.

Link copied to clipboard
const val BASE_64: String

RFC 4648 Base64 alphabet.

Link copied to clipboard
const val BASE_64_URL: String

RFC 4648 §5 URL-safe Base64 alphabet.

Functions

Link copied to clipboard

Appends the digest on encode; verifies and strips it on decode.

Link copied to clipboard
fun EncodedFormat(from: EncodedFormat = EncodedFormat.Default, builderAction: EncodedFormatBuilder.() -> Unit): EncodedFormat

Creates an EncodedFormat.

Link copied to clipboard
fun PackedFormat(from: PackedFormat = PackedFormat.Default, builderAction: PackedFormatBuilder.() -> Unit): PackedFormat

Creates a PackedFormat.

Link copied to clipboard

Runs this then next on encode, and the reverse on decode.