Serialization and Type Conversion

Vogen supports a range of serialization and type conversion frameworks. These frameworks are handled by the Conversions enum.

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:

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.