-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
JsonSerializerWrapper.cs
105 lines (80 loc) · 4.17 KB
/
JsonSerializerWrapper.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Text.Json.Serialization.Metadata;
using System.Threading.Tasks;
namespace System.Text.Json.Serialization.Tests
{
/// <summary>
/// Base class for abstracting JsonSerializer method families behind an async/string serialization facade.
/// </summary>
public abstract partial class JsonSerializerWrapper
{
/// <summary>
/// Either JsonSerializerOptions.Default for reflection or the JsonSerializerContext.Options for source gen.
/// </summary>
public abstract JsonSerializerOptions DefaultOptions { get; }
public bool IsSourceGeneratedSerializer => DefaultOptions.TypeInfoResolver is JsonSerializerContext;
/// <summary>
/// Do the deserialize methods allow a value of 'null'.
/// For example, deserializing JSON to a String supports null by returning a 'null' String reference from a literal value of "null".
/// </summary>
public virtual bool SupportsNullValueOnDeserialize => false;
public abstract Task<string> SerializeWrapper(object value, Type inputType, JsonSerializerOptions options = null);
public abstract Task<string> SerializeWrapper<T>(T value, JsonSerializerOptions options = null);
public abstract Task<string> SerializeWrapper(object value, Type inputType, JsonSerializerContext context);
public abstract Task<string> SerializeWrapper<T>(T value, JsonTypeInfo<T> jsonTypeInfo);
public abstract Task<string> SerializeWrapper(object value, JsonTypeInfo jsonTypeInfo);
public abstract Task<T> DeserializeWrapper<T>(string json, JsonSerializerOptions options = null);
public abstract Task<object> DeserializeWrapper(string json, Type type, JsonSerializerOptions options = null);
public abstract Task<T> DeserializeWrapper<T>(string json, JsonTypeInfo<T> jsonTypeInfo);
public abstract Task<object> DeserializeWrapper(string value, JsonTypeInfo jsonTypeInfo);
public abstract Task<object> DeserializeWrapper(string json, Type type, JsonSerializerContext context);
public JsonTypeInfo GetTypeInfo(Type type, bool mutable = false)
{
JsonSerializerOptions defaultOptions = DefaultOptions;
// return a fresh mutable instance or the cached readonly metadata
return mutable ? defaultOptions.TypeInfoResolver.GetTypeInfo(type, defaultOptions) : defaultOptions.GetTypeInfo(type);
}
public JsonTypeInfo<T> GetTypeInfo<T>(bool mutable = false)
=> (JsonTypeInfo<T>)GetTypeInfo(typeof(T), mutable);
public JsonSerializerOptions GetDefaultOptionsWithMetadataModifier(Action<JsonTypeInfo> modifier)
{
JsonSerializerOptions defaultOptions = DefaultOptions;
return new JsonSerializerOptions(defaultOptions)
{
TypeInfoResolver = defaultOptions.TypeInfoResolver.WithAddedModifier(modifier)
};
}
public JsonSerializerOptions CreateOptions(
Action<JsonSerializerOptions> configure = null,
bool? includeFields = false,
List<JsonConverter>? customConverters = null,
Action<JsonTypeInfo>? modifier = null,
bool makeReadOnly = true)
{
var options = new JsonSerializerOptions(DefaultOptions);
if (includeFields != null)
{
options.IncludeFields = includeFields.Value;
}
if (modifier != null && options.TypeInfoResolver != null)
{
options.TypeInfoResolver = DefaultOptions.TypeInfoResolver.WithAddedModifier(modifier);
}
if (customConverters != null)
{
foreach (JsonConverter converter in customConverters)
{
options.Converters.Add(converter);
}
}
configure?.Invoke(options);
if (makeReadOnly)
{
options.MakeReadOnly();
}
return options;
}
}
}