C# Client Library
A C# Client Library for the AnalyzeRe REST API
Loading...
Searching...
No Matches
UriConverter.cs
Go to the documentation of this file.
1using System;
2using Newtonsoft.Json;
3
5{
9 public class UriConverter : JsonConverter
10 {
17 public override bool CanConvert(Type objectType)
18 {
19 return objectType == typeof(Uri);
20 }
21
28 public override object ReadJson(
29 JsonReader reader,
30 Type objectType,
31 object existingValue,
32 JsonSerializer serializer)
33 {
34 if (reader.TokenType == JsonToken.String)
35 return new Uri((string)reader.Value);
36 if (reader.TokenType == JsonToken.Null)
37 return null;
38 throw new InvalidOperationException(
39 "Unhandled case for UriConverter. Unexpected TokenType: " + reader.TokenType);
40 }
41
48 public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
49 {
50 if (value == null)
51 writer.WriteNull();
52 else
53 {
54 if (value is Uri uri)
55 writer.WriteValue(uri.OriginalString);
56 else
57 throw new InvalidOperationException(
58 "Unhandled case for UriConverter. Unexpected type: " + value.GetType());
59 }
60 }
61 }
62}
Converts JSON string representations of URLs to and from System.Uri instances.
override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
Serializes to the specified type.
override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
Parses the json to the specified type.
override bool CanConvert(Type objectType)
Determines if this converter is designed for deserialization to objects of the specified type.