-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathCrsConverter.cs
More file actions
151 lines (133 loc) · 5.53 KB
/
CrsConverter.cs
File metadata and controls
151 lines (133 loc) · 5.53 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
145
146
147
148
149
150
151
// Copyright © Joerg Battermann 2014, Matt Hunt 2017
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using GeoJSON.Text.CoordinateReferenceSystem;
namespace GeoJSON.Text.Converters
{
/// <summary>
/// Converts <see cref="ICRSObject"/> types to and from JSON.
/// </summary>
public class CrsConverter : JsonConverter<ICRSObject>
{
public override bool HandleNull => true;
/// <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(ICRSObject).IsAssignableFromType(objectType);
}
/// <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>
/// <exception cref="Newtonsoft.Json.JsonReaderException">
/// CRS must be null or a json object
/// or
/// CRS must have a "type" property
/// </exception>
public override ICRSObject Read(
ref Utf8JsonReader reader,
Type type,
JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
{
return new UnspecifiedCRS();
}
if (reader.TokenType != JsonTokenType.StartObject)
{
throw new JsonException("CRS must be null or a json object");
}
var jObject = JsonDocument.ParseValue(ref reader).RootElement;
JsonElement token;
if (!jObject.TryGetProperty("type", out token))
{
throw new JsonException("CRS must have a \"type\" property");
}
var crsType = token.GetString();
if (string.Equals("name", crsType, StringComparison.OrdinalIgnoreCase))
{
if (jObject.TryGetProperty("properties", out var properties))
{
var name = properties.GetProperty("name").GetString();
var target = new NamedCRS(name);
var converted = jObject.Deserialize<NamedCRS>();
if (converted.Properties != null)
{
foreach (var item in converted?.Properties)
{
target.Properties[item.Key] = item.Value;
}
}
return target;
}
}
else if (string.Equals("link", crsType, StringComparison.OrdinalIgnoreCase))
{
if (jObject.TryGetProperty("properties", out var properties))
{
var href = properties.GetProperty("href").GetString();
var target = new LinkedCRS(href);
var converted = jObject.Deserialize<LinkedCRS>();
if (converted.Properties != null)
{
foreach (var item in converted?.Properties)
{
target.Properties[item.Key] = item.Value;
}
}
return target;
}
}
//return new NotSupportedException(string.Format("Type {0} unexpected.", crsType));
return null;
}
/// <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>
/// <exception cref="System.ArgumentOutOfRangeException"></exception>
public override void Write(
Utf8JsonWriter writer,
ICRSObject crsValue,
JsonSerializerOptions options)
{
var value = (ICRSObject)crsValue;
if(value == null)
return;
switch (value.Type)
{
case CRSType.Name:
//var nameObject = (NamedCRS)value;
//var serializedName = JsonSerializer.Serialize(nameObject, options);
JsonSerializer.Serialize(writer, value, typeof(NamedCRS), options);
break;
case CRSType.Link:
//var linkedObject = (LinkedCRS)value;
//var serializedLink = JsonSerializer.Serialize(linkedObject, options);
//writer.WriteRawValue(serializedLink);
JsonSerializer.Serialize(writer, value, typeof(LinkedCRS), options);
break;
case CRSType.Unspecified:
writer.WriteNullValue();
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
}