Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Pilchie/JsonSourceGenerator
Source generators to create strongly typed wrappers around JSonElements instead of serializing
https://github.com/Pilchie/JsonSourceGenerator
Last synced: about 1 month ago
JSON representation
Source generators to create strongly typed wrappers around JSonElements instead of serializing
- Host: GitHub
- URL: https://github.com/Pilchie/JsonSourceGenerator
- Owner: Pilchie
- License: mit
- Created: 2022-03-18T18:11:45.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-04-13T17:29:22.000Z (over 2 years ago)
- Last Synced: 2024-11-02T07:42:23.436Z (about 1 month ago)
- Language: C#
- Size: 52.7 KB
- Stars: 38
- Watchers: 4
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- RSCG_Examples - JsonSourceGenerator
README
# JSon Wrapper Source Generator
## What
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.## Example
Given an interface like:
``` C#
[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.
## Why?
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.
## Future ideas
1. Handle unwrapping array properties
2. Handle `Nullable` properties.
3. Handle a few more basic values `System.Text.Json.JsonElement` has accessors for (eg `Guid`, `DateTimeOffset`, `Base64Bytes`)
4. Support properties that are themselves interfaces with `[JsonWrapper]` so that you can have a hierarchy.