forked from angularsen/UnitsNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnitsNetBaseJsonConverter.cs
More file actions
237 lines (204 loc) · 10.1 KB
/
UnitsNetBaseJsonConverter.cs
File metadata and controls
237 lines (204 loc) · 10.1 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Licensed under MIT No Attribution, see LICENSE file at the root.
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace UnitsNet.Serialization.JsonNet
{
/// <summary>
/// Base converter for serializing and deserializing UnitsNet types to and from JSON.
/// Contains shared functionality used by <see cref="UnitsNetIQuantityJsonConverter"/> and <see cref="UnitsNetIComparableJsonConverter"/>
/// </summary>
/// <typeparam name="T">The type being converted. Should either be <see cref="IQuantity"/> or <see cref="IComparable"/></typeparam>
#if NET
[RequiresDynamicCode("The native code for this instantiation might not be available at runtime.")]
[RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")]
#endif
public abstract class UnitsNetBaseJsonConverter<T> : NullableQuantityConverter<T>
{
private readonly ConcurrentDictionary<string, (Type Quantity, Type Unit)> _registeredTypes = new();
/// <summary>
/// Register custom types so that the converter can instantiate these quantities.
/// Instead of calling <see cref="Quantity.From(double,UnitKey)"/>, the <see cref="Activator"/> will be used to instantiate the object.
/// It is therefore assumed that the constructor of <paramref name="quantity"/> is specified with <c>new T(double value, typeof(<paramref name="unit"/>) unit)</c>.
/// Registering the same <paramref name="unit"/> multiple times, it will overwrite the one registered.
/// </summary>
public void RegisterCustomType(Type quantity, Type unit)
{
if (!typeof(T).IsAssignableFrom(quantity))
{
throw new ArgumentException($"The type {quantity} is not a {typeof(T)}");
}
if (!typeof(Enum).IsAssignableFrom(unit))
{
throw new ArgumentException($"The type {unit} is not a {nameof(Enum)}");
}
_registeredTypes[unit.Name] = (quantity, unit);
}
/// <summary>
/// Reads the "Unit" and "Value" properties from a JSON string
/// </summary>
/// <param name="jsonToken">The JSON data to read from</param>
/// <returns>A <see cref="ValueUnit"/></returns>
protected ValueUnit? ReadValueUnit(JToken jsonToken)
{
// Empty JSON "{}"
if (!jsonToken.HasValues)
{
return null;
}
var jsonObject = (JObject) jsonToken;
var unit = jsonObject.GetValue(nameof(ValueUnit.Unit), StringComparison.OrdinalIgnoreCase);
var value = jsonObject.GetValue(nameof(ValueUnit.Value), StringComparison.OrdinalIgnoreCase);
if (unit == null || value == null)
{
return null;
}
if (value.Type != JTokenType.Float && value.Type != JTokenType.Integer)
{
return null;
}
return new ValueUnit {
Unit = unit.Value<string>() ?? throw new InvalidOperationException("Unit was not a string."),
Value = value.Value<double>()
};
}
/// <summary>
/// Convert a <see cref="ValueUnit"/> to an <see cref="IQuantity"/>
/// </summary>
/// <param name="valueUnit">The value unit to convert</param>
/// <exception cref="UnitsNetException">Thrown when an invalid Unit has been provided</exception>
/// <returns>An IQuantity</returns>
protected IQuantity ConvertValueUnit(ValueUnit valueUnit)
{
if (string.IsNullOrWhiteSpace(valueUnit.Unit))
{
throw new NotSupportedException("Unit must be specified.");
}
var unit = GetUnit(valueUnit.Unit);
var registeredQuantity = GetRegisteredType(valueUnit.Unit).Quantity;
if (registeredQuantity is not null)
{
return (IQuantity)Activator.CreateInstance(registeredQuantity, valueUnit.Value, unit)!;
}
return Quantity.From(valueUnit.Value, unit);
}
private (Type? Quantity, Type? Unit) GetRegisteredType(string unit)
{
var (unitEnumTypeName, _) = SplitUnitString(unit);
if (_registeredTypes.TryGetValue(unitEnumTypeName, out var registeredType))
{
return registeredType;
}
return (null, null);
}
private Enum GetUnit(string unit)
{
var (unitEnumTypeName, unitEnumValue) = SplitUnitString(unit);
// First try to find the name in the list of registered types.
var unitEnumType = GetRegisteredType(unit).Unit;
if (unitEnumType is null)
{
// "UnitsNet.Units.MassUnit,UnitsNet"
var unitEnumTypeAssemblyQualifiedName = "UnitsNet.Units." + unitEnumTypeName + ",UnitsNet";
// -- see http://stackoverflow.com/a/6465096/1256096 for details
unitEnumType = Type.GetType(unitEnumTypeAssemblyQualifiedName);
if (unitEnumType is null)
{
var ex = new UnitsNetException("Unable to find enum type.");
ex.Data["type"] = unitEnumTypeAssemblyQualifiedName;
throw ex;
}
}
var unitValue = (Enum) Enum.Parse(unitEnumType, unitEnumValue); // Ex: MassUnit.Kilogram
return unitValue;
}
private static (string EnumName, string EnumValue) SplitUnitString(string unit)
{
var unitParts = unit.Split('.');
if (unitParts.Length != 2)
{
var ex = new UnitsNetException($"\"{unit}\" is not a valid unit.");
ex.Data["type"] = unit;
throw ex;
}
// "MassUnit.Kilogram" => "MassUnit" and "Kilogram"
return (unitParts[0], unitParts[1]);
}
/// <summary>
/// Convert an <see cref="IQuantity"/> to a <see cref="ValueUnit"/>
/// </summary>
/// <param name="quantity">The quantity to convert</param>
/// <returns>A serializable object.</returns>
protected ValueUnit ConvertIQuantity(IQuantity quantity)
{
quantity = quantity ?? throw new ArgumentNullException(nameof(quantity));
return new ValueUnit {Value = (double)quantity.Value, Unit = $"{quantity.QuantityInfo.UnitType.Name}.{quantity.Unit}"};
}
/// <summary>
/// Create a copy of a serializer, retaining any settings but leaving out a converter to prevent loops
/// </summary>
/// <param name="serializer">The serializer to copy</param>
/// <param name="currentConverter">The converter to leave out</param>
/// <returns>A serializer with the same settings and all converters except the current one.</returns>
protected JsonSerializer CreateLocalSerializer(JsonSerializer serializer, JsonConverter currentConverter)
{
var localSerializer = new JsonSerializer()
{
Culture = serializer.Culture,
CheckAdditionalContent = serializer.CheckAdditionalContent,
Context = serializer.Context,
ContractResolver = serializer.ContractResolver,
TypeNameHandling = serializer.TypeNameHandling,
TypeNameAssemblyFormatHandling = serializer.TypeNameAssemblyFormatHandling,
Formatting = serializer.Formatting,
ConstructorHandling = serializer.ConstructorHandling,
DateFormatHandling = serializer.DateFormatHandling,
DateFormatString = serializer.DateFormatString,
DateParseHandling = serializer.DateParseHandling,
DateTimeZoneHandling = serializer.DateTimeZoneHandling,
DefaultValueHandling = serializer.DefaultValueHandling,
EqualityComparer = serializer.EqualityComparer,
FloatFormatHandling = serializer.FloatFormatHandling,
FloatParseHandling = serializer.FloatParseHandling,
MaxDepth = serializer.MaxDepth,
MetadataPropertyHandling = serializer.MetadataPropertyHandling,
MissingMemberHandling = serializer.MissingMemberHandling,
NullValueHandling = serializer.NullValueHandling,
ObjectCreationHandling = serializer.ObjectCreationHandling,
PreserveReferencesHandling = serializer.PreserveReferencesHandling,
ReferenceLoopHandling = serializer.ReferenceLoopHandling,
ReferenceResolver = serializer.ReferenceResolver,
SerializationBinder = serializer.SerializationBinder,
StringEscapeHandling = serializer.StringEscapeHandling,
TraceWriter = serializer.TraceWriter
};
foreach (var converter in serializer.Converters.Where(x => x != currentConverter))
{
localSerializer.Converters.Add(converter);
}
return localSerializer;
}
/// <summary>
/// A structure used to serialize/deserialize Units.NET unit instances.
/// </summary>
protected class ValueUnit
{
/// <summary>
/// The unit of the value.
/// </summary>
/// <example>MassUnit.Pound</example>
/// <example>InformationUnit.Kilobyte</example>
[JsonProperty(Order = 1)]
public string Unit { get; set; } = null!;
/// <summary>
/// The value.
/// </summary>
[JsonProperty(Order = 2)]
public double Value { get; set; }
}
}
}