124 lines
3.5 KiB
C#
124 lines
3.5 KiB
C#
// /**
|
|
// * File: JSONNumberConverter.cs
|
|
// * Author: haraldwolff
|
|
// *
|
|
// * This file and it's content is copyrighted by the Author and / or copyright holder.
|
|
// * Any use wihtout proper permission is illegal and may lead to legal actions.
|
|
// *
|
|
// *
|
|
// **/
|
|
using System;
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
namespace ln.types.json.converter
|
|
{
|
|
public class JSONNumberConverter : IJsonConvert
|
|
{
|
|
public JSONNumberConverter()
|
|
{
|
|
}
|
|
|
|
public string GetSkyType(Type nativeType)
|
|
{
|
|
if (typeof(short).Equals(nativeType))
|
|
{
|
|
return "int";
|
|
}
|
|
else if (typeof(ushort).Equals(nativeType))
|
|
{
|
|
return "int";
|
|
}
|
|
else if (typeof(int).Equals(nativeType))
|
|
{
|
|
return "int";
|
|
}
|
|
else if (typeof(uint).Equals(nativeType))
|
|
{
|
|
return "int";
|
|
}
|
|
else if (typeof(long).Equals(nativeType))
|
|
{
|
|
return "int";
|
|
}
|
|
else if (typeof(ulong).Equals(nativeType))
|
|
{
|
|
return "int";
|
|
}
|
|
else if (typeof(float).Equals(nativeType))
|
|
{
|
|
return "float";
|
|
}
|
|
else if (typeof(double).Equals(nativeType))
|
|
{
|
|
return "float";
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public bool JSON2Value(Type targetType, JToken jToken, ref object value)
|
|
{
|
|
if (typeof(short).Equals(targetType))
|
|
{
|
|
value = jToken.Value<short>();
|
|
return true;
|
|
}
|
|
else if (typeof(ushort).Equals(targetType))
|
|
{
|
|
value = jToken.Value<ushort>();
|
|
return true;
|
|
}
|
|
else if (typeof(int).Equals(targetType))
|
|
{
|
|
value = jToken.Value<int>();
|
|
return true;
|
|
}
|
|
else if (typeof(uint).Equals(targetType))
|
|
{
|
|
value = jToken.Value<uint>();
|
|
return true;
|
|
}
|
|
else if (typeof(long).Equals(targetType))
|
|
{
|
|
value = jToken.Value<long>();
|
|
return true;
|
|
}
|
|
else if (typeof(ulong).Equals(targetType))
|
|
{
|
|
value = jToken.Value<ulong>();
|
|
return true;
|
|
}
|
|
else if (typeof(float).Equals(targetType))
|
|
{
|
|
value = jToken.Value<float>();
|
|
return true;
|
|
}
|
|
else if (typeof(double).Equals(targetType))
|
|
{
|
|
value = jToken.Value<double>();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public bool Value2JSON(object value, ref JToken jToken)
|
|
{
|
|
Type sourceType = value.GetType();
|
|
if (typeof(short).Equals(sourceType)
|
|
|| typeof(ushort).Equals(sourceType)
|
|
|| typeof(int).Equals(sourceType)
|
|
|| typeof(uint).Equals(sourceType)
|
|
|| typeof(long).Equals(sourceType)
|
|
|| typeof(ulong).Equals(sourceType)
|
|
|| typeof(float).Equals(sourceType)
|
|
|| typeof(double).Equals(sourceType)
|
|
)
|
|
{
|
|
jToken = JToken.FromObject(value);
|
|
return true;
|
|
}
|
|
return false;
|
|
|
|
}
|
|
}
|
|
}
|