diff --git a/src/GeoJSON.Net.Tests/Serialization/JsonTests.cs b/src/GeoJSON.Net.Tests/Serialization/JsonTests.cs new file mode 100644 index 00000000..53ffa161 --- /dev/null +++ b/src/GeoJSON.Net.Tests/Serialization/JsonTests.cs @@ -0,0 +1,34 @@ +using GeoJSON.Net.Converters; +using GeoJSON.Net.Geometry; +using Newtonsoft.Json; +using NUnit.Framework; + +namespace GeoJSON.Net.Tests.Serialization +{ + public class JsonTests + { + [Test] + public void Can_Serialize_as_GeoJSONObject() + { + GeoJSONObject source = new Point(new Position(10, 20)); + + var json = JsonConvert.SerializeObject(source, new GeoJsonConverter()); + + var deserialized = JsonConvert.DeserializeObject(json, new GeoJsonConverter()); + + Assert.AreEqual(source, deserialized); + } + + [Test] + public void Can_Serialize_as_IGeoJSONObject() + { + IGeoJSONObject source = new Point(new Position(10, 20)); + + var json = JsonConvert.SerializeObject(source, new GeoJsonConverter()); + + var deserialized = JsonConvert.DeserializeObject(json, new GeoJsonConverter()); + + Assert.AreEqual(source, deserialized); + } + } +} diff --git a/src/GeoJSON.Net/Converters/GeoJsonConverter.cs b/src/GeoJSON.Net/Converters/GeoJsonConverter.cs index 1777e7da..dfa4d3f6 100644 --- a/src/GeoJSON.Net/Converters/GeoJsonConverter.cs +++ b/src/GeoJSON.Net/Converters/GeoJsonConverter.cs @@ -15,14 +15,17 @@ namespace GeoJSON.Net.Converters /// public class GeoJsonConverter : JsonConverter { - /// - /// Determines whether this instance can convert the specified object type. - /// - /// Type of the object. - /// - /// true if this instance can convert the specified object type; otherwise, false. - /// - public override bool CanConvert(Type objectType) + /// + public override bool CanWrite => false; + + /// + /// Determines whether this instance can convert the specified object type. + /// + /// Type of the object. + /// + /// true if this instance can convert the specified object type; otherwise, false. + /// + public override bool CanConvert(Type objectType) { return typeof(IGeoJSONObject).IsAssignableFromType(objectType); } @@ -64,7 +67,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist /// The calling serializer. public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { - serializer.Serialize(writer, value); + throw new NotSupportedException(); } ///