Skip to content

Commit 8487224

Browse files
author
Matt Hunt
committed
Merge remote-tracking branch 'origin/master'
2 parents a7a828d + 84ed5d8 commit 8487224

2 files changed

Lines changed: 46 additions & 9 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using GeoJSON.Net.Converters;
2+
using GeoJSON.Net.Geometry;
3+
using Newtonsoft.Json;
4+
using NUnit.Framework;
5+
6+
namespace GeoJSON.Net.Tests.Serialization
7+
{
8+
public class JsonTests
9+
{
10+
[Test]
11+
public void Can_Serialize_as_GeoJSONObject()
12+
{
13+
GeoJSONObject source = new Point(new Position(10, 20));
14+
15+
var json = JsonConvert.SerializeObject(source, new GeoJsonConverter());
16+
17+
var deserialized = JsonConvert.DeserializeObject<GeoJSONObject>(json, new GeoJsonConverter());
18+
19+
Assert.AreEqual(source, deserialized);
20+
}
21+
22+
[Test]
23+
public void Can_Serialize_as_IGeoJSONObject()
24+
{
25+
IGeoJSONObject source = new Point(new Position(10, 20));
26+
27+
var json = JsonConvert.SerializeObject(source, new GeoJsonConverter());
28+
29+
var deserialized = JsonConvert.DeserializeObject<IGeoJSONObject>(json, new GeoJsonConverter());
30+
31+
Assert.AreEqual(source, deserialized);
32+
}
33+
}
34+
}

src/GeoJSON.Net/Converters/GeoJsonConverter.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@ namespace GeoJSON.Net.Converters
1515
/// </summary>
1616
public class GeoJsonConverter : JsonConverter
1717
{
18-
/// <summary>
19-
/// Determines whether this instance can convert the specified object type.
20-
/// </summary>
21-
/// <param name="objectType">Type of the object.</param>
22-
/// <returns>
23-
/// <c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.
24-
/// </returns>
25-
public override bool CanConvert(Type objectType)
18+
/// <inheritdoc/>
19+
public override bool CanWrite => false;
20+
21+
/// <summary>
22+
/// Determines whether this instance can convert the specified object type.
23+
/// </summary>
24+
/// <param name="objectType">Type of the object.</param>
25+
/// <returns>
26+
/// <c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.
27+
/// </returns>
28+
public override bool CanConvert(Type objectType)
2629
{
2730
return typeof(IGeoJSONObject).IsAssignableFromType(objectType);
2831
}
@@ -64,7 +67,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
6467
/// <param name="serializer">The calling serializer.</param>
6568
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
6669
{
67-
serializer.Serialize(writer, value);
70+
throw new NotSupportedException();
6871
}
6972

7073
/// <summary>

0 commit comments

Comments
 (0)