-
Notifications
You must be signed in to change notification settings - Fork 20
/
DefaultConverter.cs
49 lines (43 loc) · 1.85 KB
/
DefaultConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;
using FileFlowsScriptRepo.Generators;
public class DataConverter : JsonConverter<RepositoryObject>
{
public override RepositoryObject? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
/// <summary>
/// Write the value as JSON.
/// </summary>
/// <remarks>
/// A converter may throw any Exception, but should throw <cref>JsonException</cref> when the JSON
/// cannot be created.
/// </remarks>
/// <param name="writer">The <see cref="Utf8JsonWriter"/> to write to.</param>
/// <param name="value">The value to convert. Note that the value of determines if the converter handles values.</param>
/// <param name="options">The <see cref="JsonSerializerOptions"/> being used.</param>
public override void Write(Utf8JsonWriter writer, RepositoryObject value, JsonSerializerOptions options)
{
var properties = value.GetType().GetProperties();
writer.WriteStartObject();
foreach (var prop in properties)
{
var propValue = prop.GetValue(value);
if (propValue == null)
continue; // dont write nulls
if (prop.PropertyType.IsPrimitive && propValue == Activator.CreateInstance(prop.PropertyType))
continue; // dont write defaults
if (propValue as bool? == false)
continue; // don't write default false booleans
if(propValue is int iValue && iValue == 0)
continue;
if(prop.Name == "Group")
continue;
writer.WritePropertyName(prop.Name);
JsonSerializer.Serialize(writer, propValue, prop.PropertyType, options);
}
writer.WriteEndObject();
}
}