mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-15 20:54:58 +00:00
feat: Standardizing JSON Type conversion
This commit is contained in:
@@ -96,11 +96,15 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
/// <param name="extends"></param>
|
/// <param name="extends"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
|
[CanBeNull]
|
||||||
public static Type GetValueAsType([NotNull] this JsonReader extends)
|
public static Type GetValueAsType([NotNull] this JsonReader extends)
|
||||||
{
|
{
|
||||||
if (extends == null)
|
if (extends == null)
|
||||||
throw new ArgumentNullException("extends");
|
throw new ArgumentNullException("extends");
|
||||||
|
|
||||||
|
if (extends.TokenType == JsonToken.Null)
|
||||||
|
return null;
|
||||||
|
|
||||||
string value = extends.GetValueAsString();
|
string value = extends.GetValueAsString();
|
||||||
return Type.GetType(value);
|
return Type.GetType(value);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -132,6 +132,7 @@
|
|||||||
<Compile Include="IO\eSeekOrigin.cs" />
|
<Compile Include="IO\eSeekOrigin.cs" />
|
||||||
<Compile Include="IO\IcdStreamWriter.cs" />
|
<Compile Include="IO\IcdStreamWriter.cs" />
|
||||||
<Compile Include="Json\DateTimeIsoConverter.cs" />
|
<Compile Include="Json\DateTimeIsoConverter.cs" />
|
||||||
|
<Compile Include="Json\MinimalTypeConverter.cs" />
|
||||||
<Compile Include="Json\ToStringJsonConverter.cs" />
|
<Compile Include="Json\ToStringJsonConverter.cs" />
|
||||||
<Compile Include="NullObject.cs" />
|
<Compile Include="NullObject.cs" />
|
||||||
<Compile Include="ProcessorUtils.SimplSharp.cs" />
|
<Compile Include="ProcessorUtils.SimplSharp.cs" />
|
||||||
|
|||||||
@@ -40,6 +40,9 @@ namespace ICD.Common.Utils.Json
|
|||||||
// Serialize DateTimes to ISO
|
// Serialize DateTimes to ISO
|
||||||
s_CommonSettings.Converters.Add(new DateTimeIsoConverter());
|
s_CommonSettings.Converters.Add(new DateTimeIsoConverter());
|
||||||
|
|
||||||
|
// Minify Type serialization
|
||||||
|
s_CommonSettings.Converters.Add(new MinimalTypeConverter());
|
||||||
|
|
||||||
return s_CommonSettings;
|
return s_CommonSettings;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
34
ICD.Common.Utils/Json/MinimalTypeConverter.cs
Normal file
34
ICD.Common.Utils/Json/MinimalTypeConverter.cs
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
using System;
|
||||||
|
using ICD.Common.Utils.Extensions;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
|
namespace ICD.Common.Utils.Json
|
||||||
|
{
|
||||||
|
public sealed class MinimalTypeConverter : AbstractGenericJsonConverter<Type>
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Writes the JSON representation of the object.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="writer">The <see cref="T:Newtonsoft.Json.JsonWriter"/> to write to.</param>
|
||||||
|
/// <param name="value">The value.</param>
|
||||||
|
/// <param name="serializer">The calling serializer.</param>
|
||||||
|
public override void WriteJson(JsonWriter writer, Type value, JsonSerializer serializer)
|
||||||
|
{
|
||||||
|
writer.WriteType(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Reads the JSON representation of the object.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="reader">The <see cref="T:Newtonsoft.Json.JsonReader"/> to read from.</param>
|
||||||
|
/// <param name="existingValue">The existing value of object being read.</param>
|
||||||
|
/// <param name="serializer">The calling serializer.</param>
|
||||||
|
/// <returns>
|
||||||
|
/// The object value.
|
||||||
|
/// </returns>
|
||||||
|
public override Type ReadJson(JsonReader reader, Type existingValue, JsonSerializer serializer)
|
||||||
|
{
|
||||||
|
return reader.GetValueAsType();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user