Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/trampster/JsonSrcGen
Json library that uses .NET 5 Source Generators
https://github.com/trampster/JsonSrcGen
csharp-sourcegenerator
Last synced: 3 months ago
JSON representation
Json library that uses .NET 5 Source Generators
- Host: GitHub
- URL: https://github.com/trampster/JsonSrcGen
- Owner: trampster
- License: mit
- Created: 2020-08-22T06:50:51.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2021-07-31T09:23:17.000Z (over 3 years ago)
- Last Synced: 2024-06-29T21:06:27.906Z (4 months ago)
- Topics: csharp-sourcegenerator
- Language: C#
- Homepage:
- Size: 709 KB
- Stars: 148
- Watchers: 4
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
- RSCG_Examples - JsonSrcGen
- csharp-source-generators - JsonSrcGen - ![stars](https://img.shields.io/github/stars/trampster/JsonSrcGen?style=flat-square&cacheSeconds=604800) ![last commit](https://img.shields.io/github/last-commit/trampster/JsonSrcGen?style=flat-square&cacheSeconds=86400) - compile time JSON serializer generation. (Source Generators / Serialization)
- awesome-roslyn - JsonSrcGen - Reflection-free JSON serializer. Allows extremely fast JSON processing by generating reflection-free serializers at the compile time. (Source Generators)
README
# ![alt text](https://github.com/trampster/JsonSrcGen/blob/master/JsonSrcGen/icon.png "JsonSrcGen Logo") JsonSrcGen
Json library that uses .NET 5 c# Source GeneratorsNote: Requires the .NET 5 to run.
# Supported Types
**Classes**
Class serializers can be generated by defining a Json attribute on the class
```csharp
[Json]
public class MyType
{
[JsonName("my_name")]
public string MyProperty {get;set}[JsonIgnore]
public string IgnoredProperty {get;set;}
}var converter = new JsonConverter();
ReadOnlySpan json = convert.ToJson(new MyType(){MyProperty = "Some value"});
var myType = new MyType();
convert.FromJson("{\"MyProperty\:\"Some value\"}", myType);
```
Properties with the following types are supported:Integer Types | Others
------|--------
int | float?
int? | double
uint | double?
uint? | boolean
ushort | boolean?
ushort? | string
short | DateTime
short? | DateTime?
byte | DateTimeOffset
byte? | DateTimeOffset?
long | Guid
long? | Arrays
float | List
**Arrays**Arrays are generated by defining a JsonArray attribute at the assembly level.
```csharp
[assembly: JsonArray(typeof(bool))]
```**List**
Lists are generated by defining a JsonList attribute at the assembly level.
```csharp
[assembly: JsonList(typeof(bool))]
```**Dictionary**
Dictionaries are generated by defining a JsonList attribute at the assembly level. Only Dictionaries with string keys are supported currently.
```csharp
[assembly: JsonDictionary(typeof(string), typeof(int))]
```**Values**
Simple json values are generated by defining a JsonValue attribute at the assembly level.
```csharp
[assembly: JsonValue(typeof(int))]
```**Custom Converters**
You can customize the conversion for specific types by providing your own converter. You must implement ICustomConverter and
put the CustomConverter attribute on your class.```csharp
[CustomConverter(typeof(int))]
public class CustomCaseStringConverter : ICustomConverter
{
public void ToJson(IJsonBuilder builder, int value)
{
// Write your json to the builder here
}public ReadOnlySpan FromJson(ReadOnlySpan json, ref int value)
{
// Read the Json from the json span here
}
}
```**Leave out null properties**
You can instruct JsonSrcGen to skip serializing null property values by adding the following attribute:
```csharp
[JsonIgnoreNull]
public class MyJsonType
{
int? MyProperty {get;set;}
}
```**Set Property to default if missing**
By default JsonSrcGen doesn't set properties to there default value if they are missing in the JSON. If you always give FromJson a new instance this isn't a problem. However if you reused objects (which is a big performance boost) then the property wont get set unless present in the Json. If you want JsonSrcGen to set missing properties to default then you can specify this using the JsonOptionalAttribute
```csharp
public class MyJsonType
{
[JsonOptional]
string MyProperty{get;set;}
}
```**UTF8 Support**
JsonSrcGen supports UTF8 via ReadOnlySpan.
To Json looks is the same as string ToJson byte with Utf8 at the end of the name
```csharp
ReadOnlySpan json = convert.ToJsonUtf8(new MyType(){MyProperty = "Some value"});
```From Json is even easier as the method name is the same as string FromJson but takes a ReadOnlySpan instead of ReadOnlySpan
```
ReadOnlySpan utf8Json = Encoding.Utf8.GetBytes("{\"MyProperty\:\"Some value\"}");
convert.FromJson(utf8Json, myType);
```**Nuget Packages**
JsonSrcGen is available as a nuget package:
https://www.nuget.org/packages/JsonSrcGen/