Serialization

The StringlyTyped library supports serialization and deserialization of Value Objects using System.Text.Json. This is achieved through the ValueObjectConverterFactory.

Source:

Example:

JsonSerializerOptions options = new JsonSerializerOptions
          {
              Converters = { new ValueObjectConverterFactory() }
          };
          
          // Serialization
          string jsonString = JsonSerializer.Serialize(valueObject, options);
          
          // Deserialization
          ValueObject deserializedObject = JsonSerializer.Deserialize<ValueObject>(jsonString, options);
          

Note: The ValueObjectConverterFactory automatically handles serialization and deserialization of Value Objects.

Security Concerns:

The library does not address security concerns related to serialization and deserialization. This is because it assumes that the application handling serialization and deserialization is responsible for ensuring the security of the process.

Example:

[Fact]
          public void serialising_from_same_json()
          {
              JsonSerializerOptions options = new JsonSerializerOptions
              {
                  Converters = { new ValueObjectConverterFactory() }
              };
          
              const string json = "18";
          
              Age d_age = JsonSerializer.Deserialize(json, options);
              Score d_score = JsonSerializer.Deserialize(json, options);
          
              d_age.Value.Should().Be(18);
              d_score.Value.Should().Be(18);
              
              object.ReferenceEquals(d_age, d_score).Should().BeFalse();
          }
          

In this example, the ValueObjectConverterFactory ensures that the Age and Score Value Objects are correctly deserialized from the JSON string. However, it is up to the application to handle the security of the deserialization process, such as validating the input data and preventing malicious code injection.


          ## Top-Level Directory Explanations
          
          <a class='local-link directory-link' data-ref="samples/" href="#samples/">samples/</a> - This directory contains example projects demonstrating the usage of StringlyTyped library.
          
          <a class='local-link directory-link' data-ref="src/" href="#src/">src/</a> - This directory contains the source code of the StringlyTyped library.
          
          <a class='local-link directory-link' data-ref="tests/" href="#tests/">tests/</a> - This directory contains unit tests for the StringlyTyped library. It includes benchmark tests and small tests.