mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-11 18:54:55 +00:00
feat: Adding ToStringJsonConverter
This commit is contained in:
@@ -118,6 +118,7 @@
|
|||||||
<Compile Include="IO\IcdBinaryReader.cs" />
|
<Compile Include="IO\IcdBinaryReader.cs" />
|
||||||
<Compile Include="IO\eSeekOrigin.cs" />
|
<Compile Include="IO\eSeekOrigin.cs" />
|
||||||
<Compile Include="IO\IcdStreamWriter.cs" />
|
<Compile Include="IO\IcdStreamWriter.cs" />
|
||||||
|
<Compile Include="Json\ToStringJsonConverter.cs" />
|
||||||
<Compile Include="ProcessorUtils.SimplSharp.cs" />
|
<Compile Include="ProcessorUtils.SimplSharp.cs" />
|
||||||
<Compile Include="ProcessorUtils.Standard.cs" />
|
<Compile Include="ProcessorUtils.Standard.cs" />
|
||||||
<Compile Include="ProgramUtils.SimplSharp.cs" />
|
<Compile Include="ProgramUtils.SimplSharp.cs" />
|
||||||
|
|||||||
89
ICD.Common.Utils/Json/ToStringJsonConverter.cs
Normal file
89
ICD.Common.Utils/Json/ToStringJsonConverter.cs
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
#if SIMPLSHARP
|
||||||
|
using Crestron.SimplSharp.Reflection;
|
||||||
|
#else
|
||||||
|
using System.Reflection;
|
||||||
|
#endif
|
||||||
|
using ICD.Common.Properties;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
|
namespace ICD.Common.Utils.Json
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Simply reads/writes values from/to JSON as their string representation.
|
||||||
|
/// </summary>
|
||||||
|
[PublicAPI]
|
||||||
|
public sealed class ToStringJsonConverter : JsonConverter
|
||||||
|
{
|
||||||
|
private static readonly Dictionary<Type, MethodInfo> s_ParseMethods;
|
||||||
|
|
||||||
|
public override bool CanRead { get { return true; } }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Static constructor.
|
||||||
|
/// </summary>
|
||||||
|
static ToStringJsonConverter()
|
||||||
|
{
|
||||||
|
s_ParseMethods = new Dictionary<Type, MethodInfo>();
|
||||||
|
}
|
||||||
|
|
||||||
|
#region Methods
|
||||||
|
|
||||||
|
public override bool CanConvert(Type objectType)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||||
|
{
|
||||||
|
writer.WriteValue(value.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||||
|
{
|
||||||
|
MethodInfo parse = GetParseMethod(objectType);
|
||||||
|
if (parse != null)
|
||||||
|
return parse.Invoke(null, new[] {reader.Value});
|
||||||
|
|
||||||
|
throw new ArgumentException(
|
||||||
|
string.Format("{0} does not have a 'static {0} Parse(string)' method.", objectType.Name),
|
||||||
|
"objectType");
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
[CanBeNull]
|
||||||
|
private static MethodInfo GetParseMethod(Type type)
|
||||||
|
{
|
||||||
|
if (type == null)
|
||||||
|
throw new ArgumentNullException("type");
|
||||||
|
|
||||||
|
MethodInfo output;
|
||||||
|
if (!s_ParseMethods.TryGetValue(type, out output))
|
||||||
|
{
|
||||||
|
|
||||||
|
output = type
|
||||||
|
#if SIMPLSHARP
|
||||||
|
.GetCType()
|
||||||
|
.GetMethod("Parse",
|
||||||
|
BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic,
|
||||||
|
CType.DefaultBinder,
|
||||||
|
new CType[] {typeof(string)},
|
||||||
|
new ParameterModifier[] {});
|
||||||
|
#else
|
||||||
|
.GetTypeInfo()
|
||||||
|
.GetMethod("Parse",
|
||||||
|
BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic,
|
||||||
|
Type.DefaultBinder,
|
||||||
|
new[] {typeof(string)},
|
||||||
|
new ParameterModifier[] {});
|
||||||
|
#endif
|
||||||
|
|
||||||
|
s_ParseMethods.Add(type, output);
|
||||||
|
}
|
||||||
|
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user