forked from ln-dotnet/ln.json
Extend mapping implementations
parent
9b2d2fe6b5
commit
a0a475c743
|
@ -0,0 +1,25 @@
|
|||
// /**
|
||||
// * File: JSONDateTimeConverter.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 ln.json;
|
||||
|
||||
namespace ln.json.mapping
|
||||
{
|
||||
public class JSONDateTimeMapping : JSONMapping
|
||||
{
|
||||
public JSONDateTimeMapping()
|
||||
:base(typeof(DateTime))
|
||||
{
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
// /**
|
||||
// * File: JSONDateTimeConverter.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;
|
||||
|
||||
namespace ln.json.mapping
|
||||
{
|
||||
public class JSONDateTimeOffsetMapping : JSONMapping
|
||||
{
|
||||
public JSONDateTimeOffsetMapping()
|
||||
:base(typeof(DateTimeOffset))
|
||||
{
|
||||
}
|
||||
|
||||
public override object FromJson(JSONMapper mapper, JSONValue json) => DateTimeOffset.FromUnixTimeSeconds((long)((JSONNumber)json).Decimal);
|
||||
public override JSONValue ToJson(JSONMapper mapper, object value) => new JSONNumber(((DateTimeOffset)value).ToUnixTimeSeconds());
|
||||
}
|
||||
}
|
|
@ -8,7 +8,6 @@
|
|||
// *
|
||||
// **/
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
namespace ln.json.mapping
|
||||
{
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
using System;
|
||||
|
||||
namespace ln.json.mapping
|
||||
{
|
||||
public class JSONGuidMapping : JSONMapping
|
||||
{
|
||||
public JSONGuidMapping()
|
||||
:base(typeof(Guid))
|
||||
{
|
||||
}
|
||||
|
||||
public override object FromJson(JSONMapper mapper, JSONValue json) => Guid.Parse(((JSONString)json).Value);
|
||||
public override JSONValue ToJson(JSONMapper mapper, object value) => new JSONString(((Guid)value).ToString());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
using System;
|
||||
using ln.types.net;
|
||||
|
||||
namespace ln.json.mapping
|
||||
{
|
||||
public class JSONIPv4Mapping : JSONMapping
|
||||
{
|
||||
public JSONIPv4Mapping() : base(typeof(IPv4))
|
||||
{
|
||||
}
|
||||
|
||||
public override object FromJson(JSONMapper mapper, JSONValue json)
|
||||
{
|
||||
if (json is JSONNumber)
|
||||
{
|
||||
return new IPv4((uint)(((JSONNumber)json).Decimal));
|
||||
}
|
||||
else
|
||||
{
|
||||
return IPv4.Parse(((JSONString)json).Value);
|
||||
}
|
||||
}
|
||||
public override JSONValue ToJson(JSONMapper mapper, object value) => new JSONString(value.ToString());
|
||||
}
|
||||
}
|
|
@ -98,6 +98,15 @@ namespace ln.json.mapping
|
|||
(JSONMapper arg1, JSONValue arg2) => (arg2.ValueType == JSONValueType.TRUE) || (arg2.ValueType == JSONValueType.FALSE) ? false : throw new NotSupportedException()
|
||||
));
|
||||
|
||||
Add(new JSONDateTimeMapping());
|
||||
Add(new JSONDateTimeOffsetMapping());
|
||||
Add(new JSONGuidMapping());
|
||||
Add(new JSONIPv4Mapping());
|
||||
Add(new JSONNetwork4Mapping());
|
||||
Add(new JSONTimeSpanMapping());
|
||||
|
||||
Add(new JSONRPCCallMapping());
|
||||
|
||||
}
|
||||
|
||||
public void Add(JSONMapping mapping)
|
||||
|
@ -127,10 +136,12 @@ namespace ln.json.mapping
|
|||
return mappings[targetType];
|
||||
}
|
||||
|
||||
throw new NotSupportedException();
|
||||
throw new NotSupportedException(targetType.ToString());
|
||||
}
|
||||
|
||||
public T FromJson<T>(string jsonString) => FromJson<T>((JSONObject)JSONParser.Parse(jsonString));
|
||||
public T FromJson<T>(JSONValue json) => (T)FromJson(json, typeof(T));
|
||||
public object FromJson(string jsonString, Type targetType) => FromJson((JSONObject)JSONParser.Parse(jsonString), targetType);
|
||||
public object FromJson(JSONValue json, Type targetType)
|
||||
{
|
||||
if (json.ValueType == JSONValueType.NULL)
|
||||
|
@ -147,5 +158,17 @@ namespace ln.json.mapping
|
|||
return FindMapping(sourceType).ToJson(this, value);
|
||||
}
|
||||
|
||||
public void Apply(string jsonString, object o) => Apply((JSONObject)JSONParser.Parse(jsonString), o);
|
||||
public void Apply(JSONObject json,object o)
|
||||
{
|
||||
JSONMapping mapping = FindMapping(o.GetType());
|
||||
if (!(mapping is JSONObjectMapping))
|
||||
throw new NotSupportedException();
|
||||
|
||||
JSONObjectMapping objectMapping = mapping as JSONObjectMapping;
|
||||
objectMapping.Apply(this, json, o);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
using System;
|
||||
using ln.types.net;
|
||||
|
||||
namespace ln.json.mapping
|
||||
{
|
||||
public class JSONNetwork4Mapping : JSONMapping
|
||||
{
|
||||
public JSONNetwork4Mapping() : base(typeof(Network4))
|
||||
{
|
||||
}
|
||||
|
||||
public override object FromJson(JSONMapper mapper, JSONValue json) => Network4.Parse(((JSONString) json).Value);
|
||||
public override JSONValue ToJson(JSONMapper mapper, object value) => new JSONString(((Network4)value).ToString());
|
||||
}
|
||||
}
|
|
@ -12,6 +12,11 @@ using System.Collections.Generic;
|
|||
using System.Reflection;
|
||||
namespace ln.json.mapping
|
||||
{
|
||||
public interface IJSONApply
|
||||
{
|
||||
void Apply(JSONObject json);
|
||||
}
|
||||
|
||||
public class JSONObjectMapping : JSONMapping
|
||||
{
|
||||
Dictionary<string, Action<object,object>> setters = new Dictionary<string, Action<object,object>>();
|
||||
|
@ -40,11 +45,7 @@ namespace ln.json.mapping
|
|||
JSONObject jObject = (JSONObject)json;
|
||||
object o = Activator.CreateInstance(TargetType);
|
||||
|
||||
foreach (string name in setters.Keys)
|
||||
{
|
||||
if (jObject.ContainsKey(name))
|
||||
setters[name](o, mapper.FromJson(jObject[name], types[name]));
|
||||
}
|
||||
Apply(mapper, jObject, o);
|
||||
|
||||
return o;
|
||||
}
|
||||
|
@ -58,5 +59,22 @@ namespace ln.json.mapping
|
|||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
public void Apply(JSONObject json, object o) => Apply(JSONMapper.DefaultMapper, json, o);
|
||||
public void Apply(JSONMapper mapper,JSONObject json,object o)
|
||||
{
|
||||
if (o is IJSONApply)
|
||||
{
|
||||
(o as IJSONApply).Apply(json);
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (string name in setters.Keys)
|
||||
{
|
||||
if (json.ContainsKey(name))
|
||||
setters[name](o, mapper.FromJson(json[name], types[name]));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
using System;
|
||||
using ln.types.rpc;
|
||||
|
||||
namespace ln.json.mapping
|
||||
{
|
||||
public class JSONRPCCallMapping : JSONMapping
|
||||
{
|
||||
public JSONRPCCallMapping()
|
||||
:base(typeof(RPCCall))
|
||||
{
|
||||
}
|
||||
|
||||
public override object FromJson(JSONMapper mapper, JSONValue jsonValue)
|
||||
{
|
||||
JSONObject json = jsonValue as JSONObject;
|
||||
RPCCall call = new RPCCall();
|
||||
|
||||
if (json.ContainsKey("id"))
|
||||
{
|
||||
call.Identifier = json["id"].ToNative();
|
||||
}
|
||||
else
|
||||
{
|
||||
call.Identifier = Guid.NewGuid();
|
||||
}
|
||||
|
||||
if (json.ContainsKey("module"))
|
||||
call.ModuleName = JSONMapper.DefaultMapper.FromJson<string>(json["module"]);
|
||||
|
||||
if (json.ContainsKey("method"))
|
||||
call.MethodName = JSONMapper.DefaultMapper.FromJson<string>(json["method"]);
|
||||
|
||||
if (json.ContainsKey("parameters"))
|
||||
call.Parameters = JSONMapper.DefaultMapper.FromJson<object[]>(json["parameters"]);
|
||||
else
|
||||
call.Parameters = new object[0];
|
||||
|
||||
return call;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
using System;
|
||||
using ln.types.rpc;
|
||||
namespace ln.json.mapping
|
||||
{
|
||||
public class JSONRPCResultMapping : JSONMapping
|
||||
{
|
||||
public JSONRPCResultMapping() : base(typeof(RPCResult))
|
||||
{
|
||||
}
|
||||
|
||||
public override JSONValue ToJson(JSONMapper mapper, object value)
|
||||
{
|
||||
RPCResult rpcresult = value as RPCResult;
|
||||
JSONObject result = new JSONObject();
|
||||
result["id"] = JSONMapper.DefaultMapper.ToJson(rpcresult.Identifier);
|
||||
|
||||
if (rpcresult.ErrorText != null)
|
||||
{
|
||||
result["error"] = new JSONObject();
|
||||
result["error"]["message"] = JSONMapper.DefaultMapper.ToJson(rpcresult.ErrorText);
|
||||
result["error"]["exception"] = JSONMapper.DefaultMapper.ToJson(rpcresult.Exception);
|
||||
}
|
||||
else
|
||||
{
|
||||
result["result"] = JSONMapper.DefaultMapper.ToJson(rpcresult.Result);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
// /**
|
||||
// * File: JSONTimeSpanConverter.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;
|
||||
|
||||
namespace ln.json.mapping
|
||||
{
|
||||
public class JSONTimeSpanMapping : JSONMapping
|
||||
{
|
||||
public JSONTimeSpanMapping()
|
||||
:base(typeof(TimeSpan))
|
||||
{
|
||||
}
|
||||
|
||||
public override object FromJson(JSONMapper mapper, JSONValue json) => TimeSpan.FromSeconds((long)(((JSONNumber)json).Decimal));
|
||||
public override JSONValue ToJson(JSONMapper mapper, object value) => new JSONNumber(((TimeSpan)value).TotalSeconds);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue