SteveDunn / Vogen

A semi-opinionated library which is a source generator and a code analyser. It Source generates Value Objects
Apache License 2.0
853 stars 45 forks source link

Improve documentation around using with System.Text.Json's source generation #614

Closed SteveDunn closed 3 months ago

SteveDunn commented 4 months ago

Describe the feature

It isn't obvious from the documentation how this should be done. An example similar to the following in the documentation (docs\site\Writerside) will make things clearer - with a link to the sample code at \samples\AotTrimmedSample

using System.Text.Json;
using System.Text.Json.Serialization;
using Vogen;

var options = new JsonSerializerOptions
{
    WriteIndented = true,
    PropertyNamingPolicy = JsonNamingPolicy.KebabCaseLower,
    Converters =
    {
        new VogenTypesFactory()
    }
};

var foo = new Foo(Test.From("abc"));

var ctx = new JsonContext(options);
Console.WriteLine(JsonSerializer.Serialize(foo, ctx.Foo));

public record Foo(Test TestValueObject);

[ValueObject<string>]
public readonly partial struct Test;

[JsonSerializable(typeof(Foo))]
public partial class JsonContext : JsonSerializerContext;

... produces

{
  "test-value-object": "abc"
}
SteveDunn commented 3 months ago

Done