FateSchema
Describes how one type becomes bytes.
A schema is generic over the type it encodes, so the compiler rejects a value that doesn't belong on a channel. Every schema can also write a description of itself. The writer puts that description in the log ahead of the data, so a reader can decode a file without the classes that wrote it.
The schemas
Primitives
IntSchema: 32-bit signed integersLongSchema: 64-bit signed integersDoubleSchema: 64-bit floating point numbersStringSchema: UTF-8 encoded strings with length prefixBooleanSchema: Single-byte boolean values
There is no float type. Float widens to DoubleSchema, which is exact. Narrower primitives (Short, Byte, Char) have no encoding and are rejected when the schema is built.
Collections
ArraySchema<T>: arrays, and the wire formatList/Setuse tooDoubleArraySchemaand friends: Kotlin's primitive arrays, same bytes without boxingMapSchema<K, V>: maps with independently typed keys and valuesEnumSchema<T>: enums, as a 4-byte ordinal
Optionals
OptionalSchema<T>: a nullableT?, as a presence byte then the valueJavaOptionalSchema<T>:java.util.Optional<T>, same tag and same bytesOptionalIntSchemaand friends:OptionalInt,OptionalLong,OptionalDouble
Classes
ReflectedClassSchema<T>: built by reflecting over public propertiesTypedClassSchema<T>: the same, plus the class'sAS_TYPEname in the logCustomStructSchema<T>: components and an encoder you write yourself
Binary format
The schema, written once per channel:
[4 bytes: tag] [schema-specific metadata...]An object, written once per logged value:
[variable: object data as defined by schema]Example
// Automatic schema inference
val userSchema = FateSchema.schemaOfClass(User::class.java)
// Manual schema creation
val customSchema = CustomStructSchema<Person>(
type = "Person",
componentNames = listOf("name", "age"),
componentSchemas = listOf(StringSchema, IntSchema),
encoder = { person -> listOf(person.name, person.age) }
)
// Size calculation and encoding
val person = Person("Alice", 30)
val objSize = customSchema.objSize(person)
val buffer = ByteBuffer.allocate(objSize)
customSchema.encodeObject(buffer, person)Parameters
The type of objects this schema can serialize and deserialize
Inheritors
Types
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.