The easy to use and full featured Irc Bot everyone is talking about!
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

JsonHelper.cs 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Net;
  7. using Newtonsoft.Json;
  8. using Newtonsoft.Json.Linq;
  9. namespace Combot.Configurations
  10. {
  11. public class IPAddressConverter : JsonConverter
  12. {
  13. public override bool CanConvert(Type objectType)
  14. {
  15. return (objectType == typeof(IPAddress));
  16. }
  17. public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
  18. {
  19. IPAddress ip = (IPAddress)value;
  20. writer.WriteValue(ip.ToString());
  21. }
  22. public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
  23. {
  24. JToken token = JToken.Load(reader);
  25. return IPAddress.Parse(token.Value<string>());
  26. }
  27. }
  28. public class IPEndPointConverter : JsonConverter
  29. {
  30. public override bool CanConvert(Type objectType)
  31. {
  32. return (objectType == typeof(IPEndPoint));
  33. }
  34. public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
  35. {
  36. IPEndPoint ep = (IPEndPoint)value;
  37. writer.WriteStartObject();
  38. writer.WritePropertyName("Address");
  39. serializer.Serialize(writer, ep.Address);
  40. writer.WritePropertyName("Port");
  41. writer.WriteValue(ep.Port);
  42. writer.WriteEndObject();
  43. }
  44. public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
  45. {
  46. JObject jo = JObject.Load(reader);
  47. IPAddress address = jo["Address"].ToObject<IPAddress>(serializer);
  48. int port = jo["Port"].Value<int>();
  49. return new IPEndPoint(address, port);
  50. }
  51. }
  52. }