-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathNewtonsoftJsonSerializer.cs
More file actions
114 lines (98 loc) · 4.98 KB
/
NewtonsoftJsonSerializer.cs
File metadata and controls
114 lines (98 loc) · 4.98 KB
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
106
107
108
109
110
111
112
113
114
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/UnitTestEx
#nullable enable
using System;
using System.Collections.Generic;
using System.IO;
using UnitTestEx.Json;
using Nsj = Newtonsoft.Json;
namespace UnitTestEx.MSTest.Test
{
/// <summary>
/// Provides the <see cref="Nsj.JsonSerializer"/> encapsulated implementation.
/// </summary>
public class NewtonsoftJsonSerializer : IJsonSerializer
{
/// <summary>
/// Gets or sets the default <see cref="Nsj.JsonSerializerSettings"/>.
/// </summary>
public static Nsj.JsonSerializerSettings DefaultSettings { get; set; } = new Nsj.JsonSerializerSettings
{
DefaultValueHandling = Nsj.DefaultValueHandling.Ignore,
NullValueHandling = Nsj.NullValueHandling.Ignore,
Formatting = Nsj.Formatting.None,
Converters = { new Nsj.Converters.StringEnumConverter() },
ContractResolver = new Nsj.Serialization.CamelCasePropertyNamesContractResolver(),
};
/// <summary>
/// Initializes a new instance of the <see cref="NewtonsoftJsonSerializer"/> class.
/// </summary>
public NewtonsoftJsonSerializer() => Settings = DefaultSettings;
/// <summary>
/// Initializes a new instance of the <see cref="NewtonsoftJsonSerializer"/> class.
/// </summary>
/// <param name="settings">The <see cref="Nsj.JsonSerializerSettings"/>. Defaults to <see cref="DefaultSettings"/>.</param>
public NewtonsoftJsonSerializer(Nsj.JsonSerializerSettings? settings = null) => Settings = settings ?? DefaultSettings;
/// <inheritdoc/>
object IJsonSerializer.Options => Settings;
/// <summary>
/// Gets the <see cref="Nsj.JsonSerializerSettings"/>.
/// </summary>
public Nsj.JsonSerializerSettings Settings { get; }
/// <inheritdoc/>
public object? Deserialize(string json) => Deserialize<dynamic>(json);
/// <inheritdoc/>
public object? Deserialize(string json, Type type)
{
using var sr = new StringReader(json);
using var jtr = new Nsj.JsonTextReader(sr);
return Nsj.JsonSerializer.Create(Settings).Deserialize(jtr, type);
}
/// <inheritdoc/>
public T? Deserialize<T>(string json) => (T?)Deserialize(json, typeof(T));
/// <inheritdoc/>
public string Serialize<T>(T value, JsonWriteFormat? format = null) => Nsj.JsonConvert.SerializeObject(value, format is null ? Settings : CopySettings(format.Value));
/// <summary>
/// Copies the settings.
/// </summary>
private Nsj.JsonSerializerSettings CopySettings(JsonWriteFormat? format)
{
var s = new Nsj.JsonSerializerSettings
{
ReferenceLoopHandling = Settings.ReferenceLoopHandling,
MissingMemberHandling = Settings.MissingMemberHandling,
ObjectCreationHandling = Settings.ObjectCreationHandling,
NullValueHandling = Settings.NullValueHandling,
DefaultValueHandling = Settings.DefaultValueHandling,
PreserveReferencesHandling = Settings.PreserveReferencesHandling,
TypeNameHandling = Settings.TypeNameHandling,
MetadataPropertyHandling = Settings.MetadataPropertyHandling,
TypeNameAssemblyFormatHandling = Settings.TypeNameAssemblyFormatHandling,
ConstructorHandling = Settings.ConstructorHandling,
ContractResolver = Settings.ContractResolver,
EqualityComparer = Settings.EqualityComparer,
ReferenceResolverProvider = Settings.ReferenceResolverProvider,
TraceWriter = Settings.TraceWriter,
SerializationBinder = Settings.SerializationBinder,
Error = Settings.Error,
Context = Settings.Context,
DateFormatString = Settings.DateFormatString,
MaxDepth = Settings.MaxDepth,
Formatting = format is null ? Settings.Formatting : format == JsonWriteFormat.None ? Nsj.Formatting.None : Nsj.Formatting.Indented,
DateFormatHandling = Settings.DateFormatHandling,
DateTimeZoneHandling = Settings.DateTimeZoneHandling,
DateParseHandling = Settings.DateParseHandling,
FloatFormatHandling = Settings.FloatFormatHandling,
FloatParseHandling = Settings.FloatParseHandling,
StringEscapeHandling = Settings.StringEscapeHandling,
Culture = Settings.Culture,
CheckAdditionalContent = Settings.CheckAdditionalContent,
};
if (Settings.Converters != null)
s.Converters = new List<Nsj.JsonConverter>(Settings.Converters);
return s;
}
/// <inheritdoc/>
public IJsonSerializer Clone() => new NewtonsoftJsonSerializer(CopySettings(null));
}
}
#nullable restore