ln.types/json/converter/JSONGuidConverter.cs

41 lines
1000 B
C#

using System;
using Newtonsoft.Json.Linq;
namespace ln.types.json.converter
{
public class JSONGuidConverter : IJsonConvert
{
public JSONGuidConverter()
{
}
public string GetSkyType(Type nativeType)
{
if (typeof(Guid).Equals(nativeType))
return "guid";
return null;
}
public bool JSON2Value(Type targetType, JToken jToken, ref object value)
{
if (typeof(Guid).Equals(targetType))
{
String src = jToken.ToObject<string>();
value = Guid.Parse(src);
return true;
}
return false;
}
public bool Value2JSON(object value, ref JToken jToken)
{
if (typeof(Guid).Equals(value.GetType()))
{
jToken = JToken.FromObject(((Guid)value).ToString());
return true;
}
return false;
}
}
}