Skip to content

Commit 249cfde

Browse files
JoeRobichmatt-lethargic
authored andcommitted
Deserialize LinkedCRS with the href from its properties (#102)
* Deserialize LinkedCRS with the href from its properties Fixes #101 * Added regression test for deserializing LinkedCRS
1 parent 4d13175 commit 249cfde

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

src/GeoJSON.Net.Tests/CoordinateReferenceSystem/LinkedCRSTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ public void Can_Serialize()
4646
JsonAssert.Contains("{\"properties\":{\"href\":\"http://localhost\"},\"type\":\"link\"}", actualJson);
4747
}
4848

49+
[Test]
50+
public void Can_Deserialize_CRS_issue_101()
51+
{
52+
const string pointJson = "{\"type\":\"Point\",\"coordinates\":[2.0,1.0,3.0],\"crs\":{\"properties\":{\"href\":\"http://localhost\"},\"type\":\"link\"}}";
53+
var pointWithCRS = JsonConvert.DeserializeObject<Point>(pointJson);
54+
var linkCRS = pointWithCRS.CRS as LinkedCRS;
55+
56+
Assert.IsNotNull(linkCRS);
57+
Assert.AreEqual(CRSType.Link, linkCRS.Type);
58+
Assert.AreEqual(Href, linkCRS.Properties["href"]);
59+
}
60+
4961
[Test]
5062
public void Ctor_Throws_ArgumentNullExpection_When_Href_String_Is_Null()
5163
{

src/GeoJSON.Net/Converters/CrsConverter.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,18 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
7777
}
7878
else if (string.Equals("link", crsType, StringComparison.OrdinalIgnoreCase))
7979
{
80-
var linked = new LinkedCRS(string.Empty);
81-
serializer.Populate(jObject.CreateReader(), linked);
82-
return linked;
80+
JObject properties = null;
81+
if (jObject.TryGetValue("properties", out token))
82+
{
83+
properties = token as JObject;
84+
}
85+
86+
if (properties != null)
87+
{
88+
var linked = new LinkedCRS(properties["href"].ToString());
89+
serializer.Populate(jObject.CreateReader(), linked);
90+
return linked;
91+
}
8392
}
8493

8594
return new NotSupportedException(string.Format("Type {0} unexpected.", crsType));
@@ -112,4 +121,4 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
112121
}
113122
}
114123
}
115-
}
124+
}

0 commit comments

Comments
 (0)