forked from CrustyJew/RedditSharp-DEPRECATED-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUrlParser.cs
More file actions
35 lines (31 loc) · 1.12 KB
/
UrlParser.cs
File metadata and controls
35 lines (31 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
namespace RedditSharp
{
class UrlParser : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(string) || objectType == typeof(Uri);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var token = JToken.Load(reader);
if (token.Type == JTokenType.String)
{
if (Type.GetType("Mono.Runtime") == null)
return new Uri(token.Value<string>(), UriKind.RelativeOrAbsolute);
if (token.Value<string>().StartsWith("/"))
return new Uri(token.Value<string>(), UriKind.Relative);
return new Uri(token.Value<string>(), UriKind.RelativeOrAbsolute);
}
else
return token.Value<Uri>();
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteValue(value);
}
}
}