diff --git a/mapping/JSONDateTimeMapping.cs b/mapping/JSONDateTimeMapping.cs new file mode 100644 index 0000000..81531e2 --- /dev/null +++ b/mapping/JSONDateTimeMapping.cs @@ -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()); + } +} diff --git a/mapping/JSONDateTimeOffsetMapping.cs b/mapping/JSONDateTimeOffsetMapping.cs new file mode 100644 index 0000000..af7f37c --- /dev/null +++ b/mapping/JSONDateTimeOffsetMapping.cs @@ -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()); + } +} diff --git a/mapping/JSONEnumerableMapping.cs b/mapping/JSONEnumerableMapping.cs index e03b4ad..1ac2800 100644 --- a/mapping/JSONEnumerableMapping.cs +++ b/mapping/JSONEnumerableMapping.cs @@ -8,7 +8,6 @@ // * // **/ using System; -using System.Collections; using System.Collections.Generic; namespace ln.json.mapping { diff --git a/mapping/JSONGuidMapping.cs b/mapping/JSONGuidMapping.cs new file mode 100644 index 0000000..eaf2167 --- /dev/null +++ b/mapping/JSONGuidMapping.cs @@ -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()); + } +} diff --git a/mapping/JSONIPv4Mapping.cs b/mapping/JSONIPv4Mapping.cs new file mode 100644 index 0000000..d620b85 --- /dev/null +++ b/mapping/JSONIPv4Mapping.cs @@ -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()); + } +} diff --git a/mapping/JSONMapper.cs b/mapping/JSONMapper.cs index 2fad2a9..e384932 100644 --- a/mapping/JSONMapper.cs +++ b/mapping/JSONMapper.cs @@ -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(string jsonString) => FromJson((JSONObject)JSONParser.Parse(jsonString)); public T FromJson(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); + } + + } } diff --git a/mapping/JSONNetwork4Mapping.cs b/mapping/JSONNetwork4Mapping.cs new file mode 100644 index 0000000..70bbe34 --- /dev/null +++ b/mapping/JSONNetwork4Mapping.cs @@ -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()); + } +} diff --git a/mapping/JSONObjectMapping.cs b/mapping/JSONObjectMapping.cs index 47539a9..f02ac46 100644 --- a/mapping/JSONObjectMapping.cs +++ b/mapping/JSONObjectMapping.cs @@ -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> setters = new Dictionary>(); @@ -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])); + } + } + } } } diff --git a/mapping/JSONRPCCallMapping.cs b/mapping/JSONRPCCallMapping.cs new file mode 100644 index 0000000..9d7055d --- /dev/null +++ b/mapping/JSONRPCCallMapping.cs @@ -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(json["module"]); + + if (json.ContainsKey("method")) + call.MethodName = JSONMapper.DefaultMapper.FromJson(json["method"]); + + if (json.ContainsKey("parameters")) + call.Parameters = JSONMapper.DefaultMapper.FromJson(json["parameters"]); + else + call.Parameters = new object[0]; + + return call; + } + } +} diff --git a/mapping/JSONRPCResultMapping.cs b/mapping/JSONRPCResultMapping.cs new file mode 100644 index 0000000..733082e --- /dev/null +++ b/mapping/JSONRPCResultMapping.cs @@ -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; + } + } +} diff --git a/mapping/JSONTimeSpanMapping.cs b/mapping/JSONTimeSpanMapping.cs new file mode 100644 index 0000000..c37c7b1 --- /dev/null +++ b/mapping/JSONTimeSpanMapping.cs @@ -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); + } +}