
Codec
- Namespace
- Rowles.LeanCorpus.Codecs.CodecKit
- Assembly
- Rowles.LeanCorpus.dll
Primary entry point for all codec operations. Provides factory methods for primitive and composite codecs, as well as top-level encode/decode entry points.
public static class Codec
- Inheritance
-
Codec
Bool
Float32BE
Float32LE
Float64BE
Float64LE
GuidDotNet
GuidRfc4122
Int16BE
Int16LE
Int32BE
Int32LE
Int64BE
Int64LE
Int8
UInt16BE
UInt16LE
UInt32BE
UInt32LE
UInt64BE
UInt64LE
UInt8
VarInt32
VarInt64
VarUInt32
VarUInt64
BytesBorrowed(int)
BytesBorrowedRemaining()
BytesOwned(int)
BytesOwnedRemaining()
Case<TBase, TCase>(object, string, ICodec<TCase>)
Create a case definition for use in Choice<TBase, TTag>(ICodec<TTag>, params CaseDefinition<TBase>[]).
Choice<TBase, TTag>(ICodec<TTag>, params CaseDefinition<TBase>[])
Tag-based discriminated union codec. Decode reads the tag, dispatches to the matching case codec; encode pattern-matches the value type.
Decode<T>(ICodec<T>, ReadOnlySequence<byte>, CodecOptions?, CodecRegistry?)
Decodes a value of type T from the given bytes.
Throws a CodecException on failure.
Decode<T>(ICodec<T>, byte[], CodecOptions?, CodecRegistry?)
Decodes a value from a byte array.
Decode<T>(ICodec<T>, ReadOnlySpan<byte>, CodecOptions?, CodecRegistry?)
Decodes a value from a ReadOnlySpan (copies to array first).
EncodeAtomic<T>(ICodec<T>, T, IBufferWriter<byte>, CodecOptions?, CodecRegistry?)
Encodes a value atomically — stages to a scratch buffer first, only commits to the writer on success. If encoding fails, the writer remains untouched.
EncodeSpan(ICodec<byte[]>, ReadOnlySpan<byte>, IBufferWriter<byte>, CodecOptions?, CodecRegistry?)
Encodes a ReadOnlySpan<T> body through a version-envelope codec
without allocating a byte[]. If the codec is a
Rowles.LeanCorpus.Codecs.CodecKit.Internal.VersionEnvelopeCodec<TBase, TVersion> the fast span path is used;
otherwise falls back to the normal Encode<T>(ICodec<T>, T, IBufferWriter<byte>, CodecOptions?, CodecRegistry?) path.
EncodeToArray<T>(ICodec<T>, T, CodecOptions?, CodecRegistry?)
Encodes a value and returns the bytes as a new array.
Encode<T>(ICodec<T>, T, IBufferWriter<byte>, CodecOptions, CodecRegistry)
Encodes a value to the given writer.
FixedFrame<T>(int, ICodec<T>, FramePadding?, TrailingDataPolicy)
Fixed-frame framing: [body][padding] with exactly size total bytes.
From<TDep, TOut>(string, Func<TDep, ICodec<TOut>>)
Creates a dependent field factory that produces a codec from a previously decoded field value.
LengthPrefixed<T>(ICodec<int>, ICodec<T>, TrailingDataPolicy)
Length-prefixed framing: [length][body].
The length codec writes/reads the body byte count.
LengthPrefixed<T>(ICodec<long>, ICodec<T>, TrailingDataPolicy)
Length-prefixed framing with a long length codec.
LengthPrefixed<T>(ICodec<uint>, ICodec<T>, TrailingDataPolicy)
Length-prefixed framing with a uint length codec.
Magic(params byte[])
Magic(uint)
Map<TIn, TOut>(ICodec<TIn>, Func<TIn, TOut>, Func<TOut, TIn>)
Creates a codec that transforms values using the given encode/decode delegates. User delegate exceptions are wrapped in UserCodeException.
Optional<T>(ICodec<T>, ICodec<bool>)
Flag-based optional: a bool discriminator precedes the body. true → body present, false → absent (returns default).
Optional<T, TSentinel>(ICodec<T>, ICodec<TSentinel>, TSentinel, TSentinel)
Sentinel-based optional: if the decoded sentinel equals absentValue,
the result is default/null. Otherwise the full body is decoded.
Padding(int, byte)
Record<T>()
Creates a new record builder for type T.
RepeatFrom<T>(ICodec<T>, string)
Creates a DependentFieldFactory<TDep, TOut> that produces a
Repeat(count) codec where the count comes from a parent field.
Use with Field<TDep, TField>(string, Func<T, TField>, DependentFieldFactory<TDep, TField>).
RepeatPrefixed<T>(ICodec<T>, ICodec<int>)
Decode/encode a count-prefixed list with an int count codec. The count is read/written first, then that many elements follow.
RepeatPrefixed<T>(ICodec<T>, ICodec<long>)
Decode/encode a count-prefixed list with a long count codec.
RepeatPrefixed<T>(ICodec<T>, ICodec<uint>)
Decode/encode a count-prefixed list with a uint count codec.
Repeat<T>(ICodec<T>, int)
Decode/encode a fixed number of T items.
Path segment: [index] for each element.
Skip(int)
Then<TFirst, TSecond>(ICodec<TFirst>, ICodec<TSecond>)
Sequences two codecs: first is decoded/encoded but its result is discarded. The returned codec operates on the second codec's type.
TryDecode<T>(ICodec<T>, ReadOnlySequence<byte>, CodecOptions?, CodecRegistry?)
Tries to decode a value. Returns a CodecResult<T> instead of throwing. On failure, the reader position is restored.
TryDecode<T>(ICodec<T>, byte[], CodecOptions?, CodecRegistry?)
Tries to decode a value from a byte array.
TryEncode<T>(ICodec<T>, T, IBufferWriter<byte>, CodecOptions?, CodecRegistry?)
Tries to encode a value. Returns a CodecResult<T> instead of throwing.
Utf8BytesBorrowed(int)
Utf8BytesOwned(int)
Utf8String(int)
Utf8StringRemaining()
Validate<T>(ICodec<T>, Func<T, bool>, Func<T, string>)
Wraps a codec with a validation predicate. On decode or encode, if the predicate returns false, a CodecValidationException is thrown.
Validate<T>(ICodec<T>, Func<T, bool>, string)
Wraps a codec with a validation predicate and a fixed error message.
VersionCase<TBase, TCase>(object, string, ICodec<TCase>)
Creates a version case definition for use in Versioned<TBase, TVersion>(ICodec<TVersion>, params VersionCaseDefinition<TBase>[]) or VersionEnvelope<TBase, TVersion>(ICodec<TVersion>, ICodec<long>, Func<TVersion, byte[], TBase>, params VersionCaseDefinition<TBase>[]).
VersionEnvelope<TBase, TVersion>(ICodec<TVersion>, ICodec<long>, Func<TVersion, byte[], TBase>, params VersionCaseDefinition<TBase>[])
Version-envelope codec: [version][body-length][body].
Known versions decode via their case codec; unknown versions are preserved
via the unknown delegate.
Versioned<TBase, TVersion>(ICodec<TVersion>, params VersionCaseDefinition<TBase>[])
Version-based discriminated union codec. Decode reads the version, dispatches to the matching case codec; encode pattern-matches the value type.
WithChecksum<T>(ICodec<T>, ChecksumAlgorithmId, ChecksumPlacement)
Wraps a codec with checksum computation and verification.
WithCompression<T>(ICodec<T>, CodecCompressionLevel, ICodec<uint>?)
Wraps a codec with Deflate compression/decompression. Wire format: [compressed-length][compressed-payload].