Serialization and Type Conversion
Vogen supports a range of serialization and type conversion frameworks. These frameworks are handled by the Conversions
enum.
Conversions.Default
: This is the default setting and provides type converters and System.Text.Json serialization.Conversions.TypeConverter
: Generates a type converter for converting a value object from a string and back. docs/site/Writerside/topics/reference/Integration.mdConversions.NewtonsoftJson
: Generates a Newtonsoft.Json.JsonConverter for serializing the value object to its primitive value. docs/site/Writerside/topics/reference/Integration.mdConversions.SystemTextJson
: Generates a System.Text.Json.Serialization.JsonConverter for serializing the value object to its primitive value. docs/site/Writerside/topics/reference/Integration.mdConversions.Dapper
: Generates a Dapper type handler for the value object. docs/site/Writerside/topics/reference/Integration.mdConversions.EfCoreValueConverter
: Generates an EF Core value converter for the value object. samples/Vogen.Examples/SerializationAndConversion/EFCore/Types.csConversions.LinqToDb
: Generates a LINQ to DB mapping for the value object. README.mdConversions.MongoDB
: Generates a MongoDB BSON serializer for the value object. docs/site/Writerside/topics/reference/MongoIntegrationHowTo.mdConversions.MessagePack
: Generates an implementation of IMessagePackFormatter. docs/site/Writerside/topics/reference/Integration.mdConversions.Orleans
: Generates an Orleans serializer for the value object. README.mdConversions.ServiceStack.Text
: Generates a ServiceStack.Text serializer for the value object. README.mdConversions.None
: Disables all default conversion and serialization.Conversions.Protobuf
: Generates a Protobuf-net surrogate for the value object. README.md
Example
[ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson)]
public readonly partial struct Celsius;
This code snippet generates serializers for both Newtonsoft.Json and System.Text.Json.
Custom Converters and Serializers
You can also implement your own custom type converters and serializers. To do this, specify Conversions.None
in the [ValueObject]
attribute and implement the necessary interfaces yourself.
Supported Types
Vogen supports the following primitive types:
- string
- int
- long
- short
- byte
- float (Single)
- decimal
- double
- DateTime
- DateOnly
- TimeOnly
- DateTimeOffset
- Guid
- bool
For any other type, a generic type conversion and serializer is applied.
Example
[ValueObject(conversions: Conversions.None)]
[System.Text.Json.Serialization.JsonConverter(typeof(SpecialPrimitiveJsonConverter))]
[System.ComponentModel.TypeConverter(typeof(SpecialPrimitiveTypeConverter))]
public partial struct SpecialMeasurement;
In this example, we specify Conversions.None
to disable default converters and serializers. We then use custom attributes to specify our own JsonConverter
and TypeConverter
.
Testing
Vogen provides comprehensive unit tests for its serialization and type conversion features. See the following files for examples:
- tests/ConsumerTests/SerializationAndConversionTests/StructVos/IntVoTests.cs
- tests/ConsumerTests/GenericDeserializationValidationTests/DeserializationValidationTests.cs
- tests/ConsumerTests/SerializationAndConversionTests/ClassVos/IntVoTests.cs
Code Generation
Vogen generates the necessary code for type converters and serializers. You can see this in the following file:
## Top-Level Directory Explanations
<a class='local-link directory-link' data-ref="samples/" href="#samples/">samples/</a> - This directory contains example projects that demonstrate the usage of the Vogen library. Each subdirectory represents a different example, and contains the necessary files and configurations for that example.
<a class='local-link directory-link' data-ref="src/" href="#src/">src/</a> - This directory contains the source code for the project. It includes the Vogen library itself, as well as any shared types and code fixers.
<a class='local-link directory-link' data-ref="src/Vogen/" href="#src/Vogen/">src/Vogen/</a> - This subdirectory contains the core Vogen library code. It includes subdirectories for diagnostics, extensions, generators, properties, rules, suppressors, templates, and binaries and object files.
<a class='local-link directory-link' data-ref="tests/" href="#tests/">tests/</a> - This directory contains unit tests and benchmarks for the project. It includes subdirectories for analyzer tests, consumer tests, snapshot tests, and Vogen benchmarks.
<a class='local-link directory-link' data-ref="tests/ConsumerTests/" href="#tests/ConsumerTests/">tests/ConsumerTests/</a> - This subdirectory contains unit tests for the consumer-side components of the Vogen library.
<a class='local-link directory-link' data-ref="tests/SnapshotTests/" href="#tests/SnapshotTests/">tests/SnapshotTests/</a> - This subdirectory contains snapshot tests, which test the output of the code generation and serialization components of the Vogen library.