add Uri converter

This commit is contained in:
2013-11-25 22:12:34 +01:00
parent 127da64ec5
commit 498765d1b0
2 changed files with 36 additions and 28 deletions
+4 -28
View File
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json;
using PropertyChanged;
using System;
using System.Collections.Generic;
namespace UptimeSharp.Models
{
@@ -30,15 +30,6 @@ namespace UptimeSharp.Models
[JsonProperty("friendlyname")]
public string Name { get; set; }
/// <summary>
/// Gets or sets the URI.
/// </summary>
/// <value>
/// The URI.
/// </value>
[JsonProperty("url")]
public string UriString { get; set; }
/// <summary>
/// Gets the URI.
/// </summary>
@@ -46,22 +37,7 @@ namespace UptimeSharp.Models
/// The URI.
/// </value>
[JsonIgnore]
public Uri Uri
{
get
{
Uri uri;
try
{
uri = new Uri(UriString);
}
catch
{
uri = null;
}
return uri;
}
}
public Uri Uri { get; set; }
/// <summary>
/// Gets or sets the port.
+32
View File
@@ -45,4 +45,36 @@ namespace UptimeSharp
return new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(Convert.ToDouble(reader.Value)).ToLocalTime();
}
}
public class UriConverter : JsonConverter
{
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.String && Uri.IsWellFormedUriString(reader.Value.ToString(), UriKind.Absolute))
{
return new Uri(reader.Value.ToString());
}
return null;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (value == null)
{
writer.WriteNull();
}
else if (value is Uri)
{
writer.WriteValue(((Uri)value).OriginalString);
}
}
public override bool CanConvert(Type objectType)
{
return objectType.Equals(typeof(Uri));
}
}
}