Skip to content

Commit 3c06947

Browse files
Converted code to support system.text.json
Updated the code to support system.text.json, and also updated the tests to run using system.text.json. Issues that needs fixing: Could not make the jsonconstructor work properly, so i updated the properties to be INIT. This does not solve that the classes can be instantiated invalidly, so more work might have to go into solving the jsonconstructors.
1 parent a086e93 commit 3c06947

70 files changed

Lines changed: 1416 additions & 874 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/GeoJSON.Text.Tests/CoordinateReferenceSystem/DefaultCrsTests.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
using GeoJSON.Net.CoordinateReferenceSystem;
2-
using GeoJSON.Net.Feature;
3-
using GeoJSON.Net.Geometry;
4-
using Newtonsoft.Json;
1+
using GeoJSON.Text.CoordinateReferenceSystem;
2+
using GeoJSON.Text.Feature;
3+
using GeoJSON.Text.Geometry;
54
using NUnit.Framework;
5+
using System.Text.Json;
66

7-
namespace GeoJSON.Net.Tests.CoordinateReferenceSystem
7+
namespace GeoJSON.Text.Tests.CoordinateReferenceSystem
88
{
99
[TestFixture]
1010
public class DefaultCrsTests : TestBase
@@ -14,7 +14,7 @@ public void Can_Serialize_Does_Not_Output_Crs_Property()
1414
{
1515
var collection = new FeatureCollection();
1616

17-
var json = JsonConvert.SerializeObject(collection);
17+
var json = JsonSerializer.Serialize(collection);
1818

1919
Assert.IsTrue(!json.Contains("\"crs\""));
2020
}
@@ -23,8 +23,8 @@ public void Can_Serialize_Does_Not_Output_Crs_Property()
2323
public void Can_Deserialize_When_Json_Does_Not_Contain_Crs_Property()
2424
{
2525
var json = "{\"coordinates\":[90.65464646,53.2455662,200.4567],\"type\":\"Point\"}";
26-
27-
var point = JsonConvert.DeserializeObject<Point>(json);
26+
27+
var point = JsonSerializer.Deserialize<Point>(json);
2828

2929
Assert.IsNull(point.CRS);
3030
}
@@ -34,7 +34,7 @@ public void Can_Deserialize_CRS_issue_89()
3434
{
3535
var json = "{\"coordinates\": [ 90.65464646, 53.2455662, 200.4567 ], \"type\": \"Point\", \"crs\": { \"type\": \"name\", \"properties\": { \"name\": \"urn:ogc:def:crs:OGC:1.3:CRS84\" }}}";
3636

37-
var point = JsonConvert.DeserializeObject<Point>(json);
37+
var point = JsonSerializer.Deserialize<Point>(json);
3838

3939
Assert.IsNotNull(point.CRS);
4040
Assert.AreEqual(CRSType.Name, point.CRS.Type);
@@ -47,7 +47,7 @@ public void Can_Serialize_CRS_issue_89()
4747
"{\"type\":\"Point\",\"coordinates\":[34.56,12.34],\"crs\":{\"properties\":{\"name\":\"TEST NAME\"},\"type\":\"name\"}}";
4848
var point = new Point(new Position(12.34, 34.56)) { CRS = new NamedCRS("TEST NAME") };
4949

50-
var json = JsonConvert.SerializeObject(point);
50+
var json = JsonSerializer.Serialize(point);
5151

5252
Assert.IsNotNull(json);
5353
Assert.AreEqual(expected, json);
@@ -60,7 +60,7 @@ public void Can_Serialize_DefaultCRS_issue_89()
6060
"{\"type\":\"Point\",\"coordinates\":[34.56,12.34],\"crs\":{\"properties\":{\"name\":\"urn:ogc:def:crs:OGC::CRS84\"},\"type\":\"name\"}}";
6161
var point = new Point(new Position(12.34, 34.56)) { CRS = new NamedCRS("urn:ogc:def:crs:OGC::CRS84") };
6262

63-
var json = JsonConvert.SerializeObject(point);
63+
var json = JsonSerializer.Serialize(point);
6464

6565
Assert.IsNotNull(json);
6666
Assert.AreEqual(expected, json);

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
using System;
2-
using GeoJSON.Net.CoordinateReferenceSystem;
3-
using GeoJSON.Net.Geometry;
4-
using Newtonsoft.Json;
2+
using System.Text.Json;
3+
using GeoJSON.Text.CoordinateReferenceSystem;
4+
using GeoJSON.Text.Geometry;
55
using NUnit.Framework;
66

7-
namespace GeoJSON.Net.Tests.CoordinateReferenceSystem
7+
namespace GeoJSON.Text.Tests.CoordinateReferenceSystem
88
{
99
[TestFixture]
1010
public class LinkedCRSTests : TestBase
@@ -41,7 +41,7 @@ public void Has_Type_Property()
4141
public void Can_Serialize()
4242
{
4343
var collection = new Point(new Position(1, 2, 3)) { CRS = new LinkedCRS(Href) };
44-
var actualJson = JsonConvert.SerializeObject(collection);
44+
var actualJson = JsonSerializer.Serialize(collection);
4545

4646
JsonAssert.Contains("{\"properties\":{\"href\":\"http://localhost\"},\"type\":\"link\"}", actualJson);
4747
}
@@ -50,7 +50,7 @@ public void Can_Serialize()
5050
public void Can_Deserialize_CRS_issue_101()
5151
{
5252
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);
53+
var pointWithCRS = JsonSerializer.Deserialize<Point>(pointJson);
5454
var linkCRS = pointWithCRS.CRS as LinkedCRS;
5555

5656
Assert.IsNotNull(linkCRS);
@@ -73,14 +73,11 @@ public void Ctor_Throws_ArgumentNullExpection_When_Href_Uri_Is_Null()
7373
[Test]
7474
public void Ctor_Throws_ArgumentExpection_When_Href_Is_Not_Dereferencable_Uri()
7575
{
76-
#if NETCOREAPP1_1
77-
System.Globalization.CultureInfo.CurrentUICulture = new System.Globalization.CultureInfo("en-US");
78-
#else
7976
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-US");
80-
#endif
8177

78+
// Assert that a argument exception is thrown, and that it is for href.
8279
var argumentExpection = Assert.Throws<ArgumentException>(() => { var crs = new LinkedCRS("http://not-a-valid-<>-url"); });
83-
Assert.AreEqual($"must be a dereferenceable URI{Environment.NewLine}Parameter name: href", argumentExpection.Message);
80+
Assert.True(argumentExpection.Message.ToLower().Contains("href"));
8481
}
8582

8683
[Test]

src/GeoJSON.Text.Tests/CoordinateReferenceSystem/NamedCrsTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
using System;
2-
using GeoJSON.Net.CoordinateReferenceSystem;
3-
using GeoJSON.Net.Feature;
4-
using Newtonsoft.Json;
2+
using System.Text.Json;
3+
using GeoJSON.Text.CoordinateReferenceSystem;
4+
using GeoJSON.Text.Feature;
55
using NUnit.Framework;
66

7-
namespace GeoJSON.Net.Tests.CoordinateReferenceSystem
7+
namespace GeoJSON.Text.Tests.CoordinateReferenceSystem
88
{
99
[TestFixture]
1010
public class NamedCRSTests : TestBase
@@ -32,7 +32,7 @@ public void Has_Name_Property_With_Name()
3232
public void Can_Serialize()
3333
{
3434
var collection = new FeatureCollection() { CRS = new NamedCRS("EPSG:31370") };
35-
var actualJson = JsonConvert.SerializeObject(collection);
35+
var actualJson = JsonSerializer.Serialize(collection);
3636

3737
JsonAssert.Contains("{\"properties\":{\"name\":\"EPSG:31370\"},\"type\":\"name\"}", actualJson);
3838
}

src/GeoJSON.Text.Tests/CoordinateReferenceSystem/UnspecifiedCRSTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
using GeoJSON.Net.CoordinateReferenceSystem;
2-
using GeoJSON.Net.Feature;
3-
using Newtonsoft.Json;
1+
using GeoJSON.Text.CoordinateReferenceSystem;
2+
using GeoJSON.Text.Feature;
43
using NUnit.Framework;
4+
using System.Text.Json;
55

6-
namespace GeoJSON.Net.Tests.CoordinateReferenceSystem
6+
namespace GeoJSON.Text.Tests.CoordinateReferenceSystem
77
{
88
[TestFixture]
99
public class UnspecifiedCRSTests : TestBase
@@ -21,7 +21,7 @@ public void Can_Serialize_To_Null()
2121
{
2222
var collection = new FeatureCollection { CRS = new UnspecifiedCRS() };
2323
var expectedJson = "{\"type\":\"FeatureCollection\",\"crs\":null,\"features\":[] }";
24-
var actualJson = JsonConvert.SerializeObject(collection);
24+
var actualJson = JsonSerializer.Serialize(collection);
2525

2626
JsonAssert.AreEqual(expectedJson, actualJson);
2727
}
@@ -30,7 +30,7 @@ public void Can_Serialize_To_Null()
3030
public void Can_Deserialize_From_Null()
3131
{
3232
var json = "{\"type\":\"FeatureCollection\",\"crs\":null,\"features\":[] }";
33-
var featureCollection = JsonConvert.DeserializeObject<FeatureCollection>(json);
33+
var featureCollection = JsonSerializer.Deserialize<FeatureCollection>(json);
3434

3535
Assert.IsInstanceOf<UnspecifiedCRS>(featureCollection.CRS);
3636
}

src/GeoJSON.Text.Tests/Feature/FeatureCollectionTests.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using GeoJSON.Net.Feature;
5-
using GeoJSON.Net.Geometry;
6-
using Newtonsoft.Json;
4+
using System.Text.Json;
5+
using GeoJSON.Text.Feature;
6+
using GeoJSON.Text.Geometry;
77
using NUnit.Framework;
88

9-
namespace GeoJSON.Net.Tests.Feature
9+
namespace GeoJSON.Text.Tests.Feature
1010
{
1111
[TestFixture]
1212
public class FeatureCollectionTests : TestBase
@@ -25,7 +25,7 @@ public void Can_Deserialize()
2525
{
2626
string json = GetExpectedJson();
2727

28-
var featureCollection = JsonConvert.DeserializeObject<FeatureCollection>(json);
28+
var featureCollection = JsonSerializer.Deserialize<FeatureCollection>(json);
2929

3030
Assert.IsNotNull(featureCollection.Features);
3131
Assert.AreEqual(featureCollection.Features.Count, 3);
@@ -52,17 +52,17 @@ public void FeatureCollectionSerialization()
5252
{ "test2", 2 }
5353
};
5454

55-
var feature = new Net.Feature.Feature(geom, props);
55+
var feature = new Text.Feature.Feature(geom, props);
5656
model.Features.Add(feature);
5757
}
5858

59-
var actualJson = JsonConvert.SerializeObject(model);
59+
var actualJson = JsonSerializer.Serialize(model);
6060

6161
Assert.IsNotNull(actualJson);
6262

6363
Assert.IsFalse(string.IsNullOrEmpty(actualJson));
6464
}
65-
65+
6666
[Test]
6767
public void FeatureCollection_Equals_GetHashCode_Contract()
6868
{
@@ -76,12 +76,12 @@ public void FeatureCollection_Equals_GetHashCode_Contract()
7676
public void Serialized_And_Deserialized_FeatureCollection_Equals_And_Share_HashCode()
7777
{
7878
var leftFc = GetFeatureCollection();
79-
var leftJson = JsonConvert.SerializeObject(leftFc);
80-
var left = JsonConvert.DeserializeObject<FeatureCollection>(leftJson);
79+
var leftJson = JsonSerializer.Serialize(leftFc);
80+
var left = JsonSerializer.Deserialize<FeatureCollection>(leftJson);
8181

8282
var rightFc = GetFeatureCollection();
83-
var rightJson = JsonConvert.SerializeObject(rightFc);
84-
var right = JsonConvert.DeserializeObject<FeatureCollection>(rightJson);
83+
var rightJson = JsonSerializer.Serialize(rightFc);
84+
var right = JsonSerializer.Deserialize<FeatureCollection>(rightJson);
8585

8686
Assert_Are_Equal(left, right);
8787
}
@@ -108,7 +108,7 @@ public void FeatureCollection_Test_IndexOf()
108108

109109
var props = FeatureTests.GetPropertiesInRandomOrder();
110110

111-
var feature = new Net.Feature.Feature(geom, props, id);
111+
var feature = new Text.Feature.Feature(geom, props, id);
112112
model.Features.Add(feature);
113113
}
114114

@@ -124,7 +124,7 @@ public void FeatureCollection_Test_IndexOf()
124124
Assert.AreEqual(expectedId, actualId);
125125
Assert.AreEqual(expectedIndex, actualIndex);
126126

127-
Assert.Inconclusive("not supported. the Feature.Id is optional. " +
127+
Assert.Inconclusive("not supported. the Feature.Id is optional. " +
128128
" create a new class that inherits from" +
129129
" Feature and then override Equals and GetHashCode");
130130

@@ -146,7 +146,7 @@ private FeatureCollection GetFeatureCollection()
146146

147147
var props = FeatureTests.GetPropertiesInRandomOrder();
148148

149-
var feature = new Net.Feature.Feature(geom, props);
149+
var feature = new Text.Feature.Feature(geom, props);
150150
model.Features.Add(feature);
151151
}
152152
return model;

0 commit comments

Comments
 (0)