CustomStructSchema
A schema you define by hand. You name the components and supply a function that produces them.
Nothing is reflected. Use it for a class you can't modify, or to write fewer bytes than reflection would. You can drop fields you never plot, round a double, convert units, or emit a computed value that isn't a field.
How it works
encoder turns one object into a list of components. Each component has a name from componentNames and a schema from componentSchemas at the same index. They are written in that order after a leading type string.
Binary format
The schema:
[4 bytes: tag] [4 bytes: component count]
[4 bytes: ".type" length] [".type" bytes] [StringSchema]
[4 bytes: name1 length] [name1 bytes] [schema1]
[4 bytes: name2 length] [name2 bytes] [schema2]
...An encoded object:
[4 bytes: type length] [type string bytes]
[component1 data] [component2 data] ...Examples
Basic usage
data class Person(val name: String, val age: Int, val email: String)
val personSchema = CustomStructSchema<Person>(
type = "Person",
componentNames = listOf("name", "age", "email"),
componentSchemas = listOf(StringSchema, IntSchema, StringSchema),
encoder = { person -> listOf(person.name, person.age, person.email) }
)Transforming values
val transformedSchema = CustomStructSchema<Person>(
type = "NormalizedPerson",
componentNames = listOf("displayName", "ageCategory"),
componentSchemas = listOf(StringSchema, StringSchema),
encoder = { person ->
listOf(
person.name.uppercase(),
when {
person.age < 18 -> "MINOR"
person.age < 65 -> "ADULT"
else -> "SENIOR"
}
)
}
)Logging a subset of fields
val minimalSchema = CustomStructSchema<ComplexObject>(
type = "Essential",
componentNames = listOf("id", "status"),
componentSchemas = listOf(LongSchema, StringSchema),
encoder = { obj -> listOf(obj.id, obj.currentStatus.name) }
)Parameters
The type of objects this schema serializes
Identifier for this object type, written to the log as the first value
Names of the components, in the order they are written
Schemas for each component, in the same order as componentNames
Converts an object into its list of components
Throws
if componentNames and componentSchemas have different sizes
Types
Properties
Names of the components, in the order they are written. They go into the schema entry.
Schemas for each component, in the same order as componentNames.
Number of bytes in this schema's definition.
Functions
Writes the given object into the buffer.
Writes the whole schema definition, starting with the tag, into the buffer.
Returns the number of bytes encodeObject writes for the given object.