forked from ln-dotnet/ln.json
WIP
parent
42e32919e3
commit
6add70bde4
|
@ -1,6 +1,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using ln.json.mapping;
|
||||||
|
|
||||||
namespace ln.json
|
namespace ln.json
|
||||||
{
|
{
|
||||||
|
@ -11,6 +12,11 @@ namespace ln.json
|
||||||
|
|
||||||
public int Count => values.Count;
|
public int Count => values.Count;
|
||||||
|
|
||||||
|
public override object ToNative()
|
||||||
|
{
|
||||||
|
return JSONMapper.DefaultMapper.FromJson(this, typeof(object[]));
|
||||||
|
}
|
||||||
|
|
||||||
JSONValue[] Values
|
JSONValue[] Values
|
||||||
{
|
{
|
||||||
get => values.ToArray();
|
get => values.ToArray();
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
|
using ln.types;
|
||||||
namespace ln.json
|
namespace ln.json
|
||||||
{
|
{
|
||||||
public class JSONNumber : JSONValue
|
public class JSONNumber : JSONValue
|
||||||
|
@ -7,6 +8,10 @@ namespace ln.json
|
||||||
public Decimal Decimal => decValue;
|
public Decimal Decimal => decValue;
|
||||||
readonly decimal decValue;
|
readonly decimal decValue;
|
||||||
|
|
||||||
|
public double AsDouble => (double)decValue;
|
||||||
|
public long AsLong => (long)decValue;
|
||||||
|
public int AsInt => (int)decValue;
|
||||||
|
|
||||||
public override object ToNative()
|
public override object ToNative()
|
||||||
{
|
{
|
||||||
if (Decimal.Ceiling(decValue).Equals(Decimal.Floor(decValue)))
|
if (Decimal.Ceiling(decValue).Equals(Decimal.Floor(decValue)))
|
||||||
|
@ -35,7 +40,7 @@ namespace ln.json
|
||||||
public JSONNumber(double doubleValue)
|
public JSONNumber(double doubleValue)
|
||||||
: base(JSONValueType.NUMBER)
|
: base(JSONValueType.NUMBER)
|
||||||
{
|
{
|
||||||
decValue = new Decimal(doubleValue);
|
decValue = doubleValue.ToDecimal();
|
||||||
}
|
}
|
||||||
public JSONNumber(decimal decValue)
|
public JSONNumber(decimal decValue)
|
||||||
: base(JSONValueType.NUMBER)
|
: base(JSONValueType.NUMBER)
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
using System;
|
||||||
|
namespace ln.json.attributes
|
||||||
|
{
|
||||||
|
public class JSONMappingAttribute : Attribute
|
||||||
|
{
|
||||||
|
public bool Private { get; set; } = false;
|
||||||
|
|
||||||
|
public JSONMappingAttribute()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -51,9 +51,12 @@
|
||||||
<Compile Include="mapping\JSONTimeSpanMapping.cs" />
|
<Compile Include="mapping\JSONTimeSpanMapping.cs" />
|
||||||
<Compile Include="mapping\JSONDateTimeOffsetMapping.cs" />
|
<Compile Include="mapping\JSONDateTimeOffsetMapping.cs" />
|
||||||
<Compile Include="mapping\JSONRPCResultMapping.cs" />
|
<Compile Include="mapping\JSONRPCResultMapping.cs" />
|
||||||
|
<Compile Include="attributes\JSONMappingAttribute.cs" />
|
||||||
|
<Compile Include="mapping\JSONEnumMapping.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Folder Include="mapping\" />
|
<Folder Include="mapping\" />
|
||||||
|
<Folder Include="attributes\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\ln.types\ln.types.csproj">
|
<ProjectReference Include="..\ln.types\ln.types.csproj">
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
// **/
|
// **/
|
||||||
using System;
|
using System;
|
||||||
using ln.json;
|
using ln.json;
|
||||||
|
using ln.types;
|
||||||
|
|
||||||
namespace ln.json.mapping
|
namespace ln.json.mapping
|
||||||
{
|
{
|
||||||
|
@ -19,7 +20,7 @@ namespace ln.json.mapping
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public override object FromJson(JSONMapper mapper, JSONValue json) => DateTimeOffset.FromUnixTimeSeconds((long)((JSONNumber)json).Decimal).DateTime;
|
public override object FromJson(JSONMapper mapper, JSONValue json) => DateTimeExtensions.FromUnixTimeSeconds(((JSONNumber)json).AsDouble);
|
||||||
public override JSONValue ToJson(JSONMapper mapper, object value) => new JSONNumber(new DateTimeOffset((DateTime)value).ToUnixTimeSeconds());
|
public override JSONValue ToJson(JSONMapper mapper, object value) => new JSONNumber(((DateTime)value).ToUnixTimeSeconds());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,21 @@
|
||||||
|
using System;
|
||||||
|
namespace ln.json.mapping
|
||||||
|
{
|
||||||
|
public class JSONEnumMapping : JSONMapping
|
||||||
|
{
|
||||||
|
public JSONEnumMapping(Type targetType)
|
||||||
|
:base(targetType)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public override JSONValue ToJson(JSONMapper mapper, object value)
|
||||||
|
{
|
||||||
|
return new JSONString(value.ToString());
|
||||||
|
}
|
||||||
|
public override object FromJson(JSONMapper mapper, JSONValue json)
|
||||||
|
{
|
||||||
|
return Enum.Parse(TargetType, json.ToNative().ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -122,11 +122,14 @@ namespace ln.json.mapping
|
||||||
if (this != DefaultMapper)
|
if (this != DefaultMapper)
|
||||||
return DefaultMapper.FindMapping(targetType);
|
return DefaultMapper.FindMapping(targetType);
|
||||||
|
|
||||||
if (!targetType.IsPrimitive && !targetType.IsValueType)
|
if (!targetType.IsPrimitive)
|
||||||
{
|
{
|
||||||
if (targetType.IsArray)
|
if (targetType.IsArray)
|
||||||
{
|
{
|
||||||
Add(new JSONArrayMapping(targetType));
|
Add(new JSONArrayMapping(targetType));
|
||||||
|
} else if (targetType.IsEnum)
|
||||||
|
{
|
||||||
|
Add(new JSONEnumMapping(targetType));
|
||||||
} else if (targetType.IsGenericType && targetType.GetGenericTypeDefinition().Equals(typeof(IEnumerable<>)))
|
} else if (targetType.IsGenericType && targetType.GetGenericTypeDefinition().Equals(typeof(IEnumerable<>)))
|
||||||
{
|
{
|
||||||
Add((JSONMapping)Activator.CreateInstance(typeof(JSONEnumerableMapping<>).MakeGenericType(targetType.GetGenericArguments()[0])));
|
Add((JSONMapping)Activator.CreateInstance(typeof(JSONEnumerableMapping<>).MakeGenericType(targetType.GetGenericArguments()[0])));
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using ln.json.attributes;
|
||||||
namespace ln.json.mapping
|
namespace ln.json.mapping
|
||||||
{
|
{
|
||||||
public interface IJSONApply
|
public interface IJSONApply
|
||||||
|
@ -24,30 +25,45 @@ namespace ln.json.mapping
|
||||||
Dictionary<string, Type> types = new Dictionary<string, Type>();
|
Dictionary<string, Type> types = new Dictionary<string, Type>();
|
||||||
|
|
||||||
public JSONObjectMapping(Type type)
|
public JSONObjectMapping(Type type)
|
||||||
:base(type)
|
: base(type)
|
||||||
{
|
{
|
||||||
foreach (FieldInfo fieldInfo in type.GetFields(BindingFlags.Instance | BindingFlags.Public))
|
foreach (FieldInfo fieldInfo in type.GetFields(BindingFlags.Instance | BindingFlags.Public))
|
||||||
{
|
{
|
||||||
setters.Add(fieldInfo.Name, (object arg1, object arg2) => fieldInfo.SetValue(arg1, arg2));
|
JSONMappingAttribute mappingAttribute = fieldInfo.GetCustomAttribute<JSONMappingAttribute>();
|
||||||
getters.Add(fieldInfo.Name, (object arg) => fieldInfo.GetValue(arg));
|
if ((mappingAttribute == null) || !mappingAttribute.Private)
|
||||||
types.Add(fieldInfo.Name, fieldInfo.FieldType);
|
{
|
||||||
|
setters.Add(fieldInfo.Name, (object arg1, object arg2) => fieldInfo.SetValue(arg1, arg2));
|
||||||
|
getters.Add(fieldInfo.Name, (object arg) => fieldInfo.GetValue(arg));
|
||||||
|
types.Add(fieldInfo.Name, fieldInfo.FieldType);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
foreach (PropertyInfo propertyInfo in type.GetProperties(BindingFlags.Instance | BindingFlags.Public))
|
foreach (PropertyInfo propertyInfo in type.GetProperties(BindingFlags.Instance | BindingFlags.Public))
|
||||||
{
|
{
|
||||||
setters.Add(propertyInfo.Name, (object arg1, object arg2) => propertyInfo.SetValue(arg1, arg2));
|
JSONMappingAttribute mappingAttribute = propertyInfo.GetCustomAttribute<JSONMappingAttribute>();
|
||||||
getters.Add(propertyInfo.Name, (object arg) => propertyInfo.GetValue(arg));
|
if ((mappingAttribute == null) || !mappingAttribute.Private)
|
||||||
types.Add(propertyInfo.Name, propertyInfo.PropertyType);
|
{
|
||||||
|
setters.Add(propertyInfo.Name, (object arg1, object arg2) => propertyInfo.SetValue(arg1, arg2));
|
||||||
|
getters.Add(propertyInfo.Name, (object arg) => propertyInfo.GetValue(arg));
|
||||||
|
types.Add(propertyInfo.Name, propertyInfo.PropertyType);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override object FromJson(JSONMapper mapper, JSONValue json)
|
public override object FromJson(JSONMapper mapper, JSONValue json)
|
||||||
{
|
{
|
||||||
JSONObject jObject = (JSONObject)json;
|
if (json is JSONObject)
|
||||||
object o = Activator.CreateInstance(TargetType);
|
{
|
||||||
|
JSONObject jObject = (JSONObject)json;
|
||||||
|
object o = Activator.CreateInstance(TargetType);
|
||||||
|
|
||||||
Apply(mapper, jObject, o);
|
Apply(mapper, jObject, o);
|
||||||
|
|
||||||
return o;
|
return o;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return json.ToNative();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override JSONValue ToJson(JSONMapper mapper, object value)
|
public override JSONValue ToJson(JSONMapper mapper, object value)
|
||||||
|
|
Loading…
Reference in New Issue