diff --git a/CIDR.cs b/CIDR.cs index beaa81a..2f757e3 100644 --- a/CIDR.cs +++ b/CIDR.cs @@ -12,6 +12,7 @@ using System.Net; using System.Linq; using System.Collections.Generic; using System.Collections; +using Newtonsoft.Json; namespace ln.types { @@ -34,7 +35,7 @@ namespace ln.types return new CIDR(ip, w); } } - return new CIDR(ip); + return new CIDR(ip,32); } private readonly uint _ip; @@ -42,6 +43,7 @@ namespace ln.types public int MaskWidth => (int)getNetWidth(_netmask); public CIDR Network => new CIDR(_ip & _netmask, _netmask); + public CIDR Host => new CIDR(_ip, 0xffffffff); public byte[] IPBytes { @@ -77,11 +79,11 @@ namespace ln.types this._netmask = maskFromWidth(maskwidth); } - public CIDR(IPAddress ip, uint maskwidth) - { - _ip = BitConverter.ToUInt32(ip.GetAddressBytes().Reverse().ToArray(), 0); - _netmask = maskFromWidth(maskwidth); - } + //public CIDR(IPAddress ip, uint maskwidth) + //{ + // _ip = BitConverter.ToUInt32(ip.GetAddressBytes().Reverse().ToArray(), 0); + // _netmask = maskFromWidth(maskwidth); + //} public CIDR(IPAddress ip, IPAddress mask) { @@ -134,11 +136,11 @@ namespace ln.types public static implicit operator IPAddress(CIDR cidr) { - return new IPAddress(cidr._ip); + return new IPAddress( cidr.IPBytes ); } public static implicit operator CIDR(IPAddress iPAddress) { - return new CIDR(iPAddress); + return new CIDR(BitConverter.ToUInt32(iPAddress.GetAddressBytes().Reverse().ToArray(),0),0xFFFFFFFF); } public bool Contains(CIDR you) @@ -203,5 +205,34 @@ namespace ln.types } } + + static bool ___init = ____init(); + static bool ____init() + { + List converters = new List(); + converters.Add(new CIDRJsonConverter()); + JsonConvert.DefaultSettings = () => new JsonSerializerSettings { Converters = converters }; + return true; + } + } + + public class CIDRJsonConverter : JsonConverter + { + public override bool CanConvert(Type objectType) + { + return (objectType == typeof(CIDR)); + } + + public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) + { + return CIDR.Parse(reader.ReadAsString()); + } + + public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) + { + writer.WriteValue((value as CIDR).ToString()); + } + } + } diff --git a/ln.types.csproj b/ln.types.csproj index c322e9a..672f856 100644 --- a/ln.types.csproj +++ b/ln.types.csproj @@ -31,6 +31,9 @@ + + ..\packages\Newtonsoft.Json.12.0.1\lib\net45\Newtonsoft.Json.dll + @@ -58,5 +61,8 @@ ln.logging + + + \ No newline at end of file diff --git a/packages.config b/packages.config new file mode 100644 index 0000000..8ebaf08 --- /dev/null +++ b/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/serialize/ObjectReader.cs b/serialize/ObjectReader.cs index 2ba9041..2dab33d 100644 --- a/serialize/ObjectReader.cs +++ b/serialize/ObjectReader.cs @@ -50,8 +50,12 @@ namespace ln.types.serialize { ListReplacement listReplacement = (ListReplacement)replacement; - object list = Activator.CreateInstance(typeof(List<>).MakeGenericType(new Type[] { listReplacement.elementType }), new object[] { listReplacement.elements }); - return list; + IList ilist = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(new Type[] { listReplacement.elementType })); + + foreach (object e in listReplacement.elements) + ilist.Add(e); + + return ilist; } else if (replacement is DictionaryReplacement) {