master
Harald Wolff 2019-08-29 14:16:55 +02:00
parent 42e32919e3
commit 6add70bde4
8 changed files with 82 additions and 15 deletions

View File

@ -1,6 +1,7 @@
using System;
using System.Text;
using System.Collections.Generic;
using ln.json.mapping;
namespace ln.json
{
@ -11,6 +12,11 @@ namespace ln.json
public int Count => values.Count;
public override object ToNative()
{
return JSONMapper.DefaultMapper.FromJson(this, typeof(object[]));
}
JSONValue[] Values
{
get => values.ToArray();

View File

@ -1,5 +1,6 @@
using System;
using System.Globalization;
using ln.types;
namespace ln.json
{
public class JSONNumber : JSONValue
@ -7,6 +8,10 @@ namespace ln.json
public Decimal Decimal => decValue;
readonly decimal decValue;
public double AsDouble => (double)decValue;
public long AsLong => (long)decValue;
public int AsInt => (int)decValue;
public override object ToNative()
{
if (Decimal.Ceiling(decValue).Equals(Decimal.Floor(decValue)))
@ -35,7 +40,7 @@ namespace ln.json
public JSONNumber(double doubleValue)
: base(JSONValueType.NUMBER)
{
decValue = new Decimal(doubleValue);
decValue = doubleValue.ToDecimal();
}
public JSONNumber(decimal decValue)
: base(JSONValueType.NUMBER)

View File

@ -0,0 +1,12 @@
using System;
namespace ln.json.attributes
{
public class JSONMappingAttribute : Attribute
{
public bool Private { get; set; } = false;
public JSONMappingAttribute()
{
}
}
}

View File

@ -51,9 +51,12 @@
<Compile Include="mapping\JSONTimeSpanMapping.cs" />
<Compile Include="mapping\JSONDateTimeOffsetMapping.cs" />
<Compile Include="mapping\JSONRPCResultMapping.cs" />
<Compile Include="attributes\JSONMappingAttribute.cs" />
<Compile Include="mapping\JSONEnumMapping.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="mapping\" />
<Folder Include="attributes\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ln.types\ln.types.csproj">

View File

@ -9,6 +9,7 @@
// **/
using System;
using ln.json;
using ln.types;
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 JSONValue ToJson(JSONMapper mapper, object value) => new JSONNumber(new DateTimeOffset((DateTime)value).ToUnixTimeSeconds());
public override object FromJson(JSONMapper mapper, JSONValue json) => DateTimeExtensions.FromUnixTimeSeconds(((JSONNumber)json).AsDouble);
public override JSONValue ToJson(JSONMapper mapper, object value) => new JSONNumber(((DateTime)value).ToUnixTimeSeconds());
}
}

View File

@ -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());
}
}
}

View File

@ -122,11 +122,14 @@ namespace ln.json.mapping
if (this != DefaultMapper)
return DefaultMapper.FindMapping(targetType);
if (!targetType.IsPrimitive && !targetType.IsValueType)
if (!targetType.IsPrimitive)
{
if (targetType.IsArray)
{
Add(new JSONArrayMapping(targetType));
} else if (targetType.IsEnum)
{
Add(new JSONEnumMapping(targetType));
} else if (targetType.IsGenericType && targetType.GetGenericTypeDefinition().Equals(typeof(IEnumerable<>)))
{
Add((JSONMapping)Activator.CreateInstance(typeof(JSONEnumerableMapping<>).MakeGenericType(targetType.GetGenericArguments()[0])));

View File

@ -10,6 +10,7 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using ln.json.attributes;
namespace ln.json.mapping
{
public interface IJSONApply
@ -24,30 +25,45 @@ namespace ln.json.mapping
Dictionary<string, Type> types = new Dictionary<string, Type>();
public JSONObjectMapping(Type type)
:base(type)
: base(type)
{
foreach (FieldInfo fieldInfo in type.GetFields(BindingFlags.Instance | BindingFlags.Public))
{
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);
JSONMappingAttribute mappingAttribute = fieldInfo.GetCustomAttribute<JSONMappingAttribute>();
if ((mappingAttribute == null) || !mappingAttribute.Private)
{
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))
{
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);
JSONMappingAttribute mappingAttribute = propertyInfo.GetCustomAttribute<JSONMappingAttribute>();
if ((mappingAttribute == null) || !mappingAttribute.Private)
{
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)
{
JSONObject jObject = (JSONObject)json;
object o = Activator.CreateInstance(TargetType);
if (json is JSONObject)
{
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)