-
Notifications
You must be signed in to change notification settings - Fork 533
Expand file tree
/
Copy pathCrsJsonConverter.cs
More file actions
144 lines (128 loc) · 5.57 KB
/
CrsJsonConverter.cs
File metadata and controls
144 lines (128 loc) · 5.57 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace Microsoft.Azure.Cosmos.Spatial.Converters
{
using System;
using Microsoft.Azure.Documents;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
/// <summary>
/// <see cref="JsonConverter"/> for <see cref="Crs" /> class and all its implementations.
/// </summary>
internal sealed class CrsJsonConverter : JsonConverter
{
/// <summary>
/// Writes the JSON representation of the object.
/// </summary>
/// <param name="writer">The <see cref="T:Newtonsoft.Json.JsonWriter" /> to write to.</param>
/// <param name="value">The value.</param>
/// <param name="serializer">The calling serializer.</param>
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
Crs crs = (Crs)value;
switch (crs.Type)
{
case CrsType.Linked:
LinkedCrs linkedCrs = (LinkedCrs)crs;
writer.WriteStartObject();
writer.WritePropertyName("type");
writer.WriteValue("link");
writer.WritePropertyName("properties");
writer.WriteStartObject();
writer.WritePropertyName("href");
writer.WriteValue(linkedCrs.Href);
if (linkedCrs.HrefType != null)
{
writer.WritePropertyName("type");
writer.WriteValue(linkedCrs.HrefType);
}
writer.WriteEndObject();
writer.WriteEndObject();
break;
case CrsType.Named:
NamedCrs namedCrs = (NamedCrs)crs;
writer.WriteStartObject();
writer.WritePropertyName("type");
writer.WriteValue("name");
writer.WritePropertyName("properties");
writer.WriteStartObject();
writer.WritePropertyName("name");
writer.WriteValue(namedCrs.Name);
writer.WriteEndObject();
writer.WriteEndObject();
break;
case CrsType.Unspecified:
writer.WriteNull();
break;
}
}
/// <summary>
/// Reads the JSON representation of the object.
/// </summary>
/// <param name="reader">The <see cref="T:Newtonsoft.Json.JsonReader" /> to read from.</param>
/// <param name="objectType">Type of the object.</param>
/// <param name="existingValue">The existing value of object being read.</param>
/// <param name="serializer">The calling serializer.</param>
/// <returns>
/// The object value.
/// </returns>
public override object ReadJson(
JsonReader reader,
Type objectType,
object existingValue,
JsonSerializer serializer)
{
JToken token = JToken.Load(reader);
if (token.Type == JTokenType.Null)
{
return Crs.Unspecified;
}
if (token.Type != JTokenType.Object)
{
throw new JsonSerializationException(RMResources.SpatialFailedToDeserializeCrs);
}
JToken properties = token["properties"];
if (properties == null || properties.Type != JTokenType.Object)
{
throw new JsonSerializationException(RMResources.SpatialFailedToDeserializeCrs);
}
JToken crsType = token["type"];
if (crsType == null || crsType.Type != JTokenType.String)
{
throw new JsonSerializationException(RMResources.SpatialFailedToDeserializeCrs);
}
switch (crsType.Value<string>())
{
case "name":
JToken crsName = properties["name"];
if (crsName == null || crsName.Type != JTokenType.String)
{
throw new JsonSerializationException(RMResources.SpatialFailedToDeserializeCrs);
}
return new NamedCrs(crsName.Value<string>());
case "link":
JToken crsHref = properties["href"];
JToken crsHrefType = properties["type"];
if (crsHref == null || crsHref.Type != JTokenType.String || (crsHrefType != null && crsHref.Type != JTokenType.String))
{
throw new JsonSerializationException(RMResources.SpatialFailedToDeserializeCrs);
}
return new LinkedCrs(crsHref.Value<string>(), crsHrefType?.Value<string>());
default:
throw new JsonSerializationException(RMResources.SpatialFailedToDeserializeCrs);
}
}
/// <summary>
/// Determines whether this instance can convert the specified object type.
/// </summary>
/// <param name="objectType">Type of the object.</param>
/// <returns>
/// <c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.
/// </returns>
public override bool CanConvert(Type objectType)
{
return typeof(Crs).IsAssignableFrom(objectType);
}
}
}