Using the source generator
Serialization in serde is driven by implementions of the ISerde<T>
interface through a "serde object". Serde ships with a source generator that can automatically implement these interfaces for any type.
To use the source generator:
- Make the type
partial
. - Add the
GenerateSerde
,GenerateSerialize
, orGenerateDeserialize
attribute
For example,
class SampleClass
{
...
}
would become
[GenerateSerde]
partial class SampleClass
{
...
}
By default, the source generator will include all the public properties and public fields. The field or property types must either,
- Directly implement the serde interfaces using
[GenerateSerde]
- Be a serde-dn built-in type, like
int
orstring
. - Specify a proxy using
[SerdeMemberOptions(Proxy = typeof(Proxy))]
If you don't control any of the types you need to serialize or deserialize (e.g., they are defined in another assembly that you can't modify) you'll need to specify a proxy. See External types for more info.
Additional IDeserialize requirements
Deserialization needs a way create and initialize a given type. There are two recognized patterns:
- A parameterless constructor, where all the fields/properties are writable.
- A primary constructor, where all the fields/properties are either writable, or the same name as a one of the constructor parameters.
If this isn't possible, the constructor used can be configured through the SerdeTypeOptions
attribute.