This source generator takes interfaces decorated with the [JsonWrapper]
attribute and generates an extension method
and wrapper class to allow accesing items in a strongly typed fashion directly out of the JsonElement without
deserializing.
Given an interface like:
[JsonWrapper]
interface IPerson
{
string FirstName { get; }
string LastName { get; }
int Age { get; }
This source generator would generate an AsIPerson()
extension method on System.Text.Json.JsonElement
and an implementation of IPerson
that wraps the JsonElement
and extracts each property on access.
This explores an idea of reducing the conversions and DTO types necessary for implementing Json HTTP APIs. Also, I haven't ever really looked at implementing a source generator, so I thought I'd give it a go.
- Handle unwrapping array properties
- Handle
Nullable<T>
properties. - Handle a few more basic values
System.Text.Json.JsonElement
has accessors for (egGuid
,DateTimeOffset
,Base64Bytes
) - Support properties that are themselves interfaces with
[JsonWrapper]
so that you can have a hierarchy.