Skip to content

Commit b984c5a

Browse files
author
rstam
committed
CSHARP-808: GeoJson serializers should coerce numeric values to double during deserialization.
1 parent 20e289d commit b984c5a

13 files changed

+297
-15
lines changed

MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJson2DCoordinatesSerializer.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ namespace MongoDB.Driver.GeoJsonObjectModel.Serializers
2626
/// </summary>
2727
public class GeoJson2DCoordinatesSerializer : BsonBaseSerializer
2828
{
29+
// private static fields
30+
private static readonly IBsonSerializer __doubleSerializer = new DoubleSerializer();
31+
2932
// public methods
3033
/// <summary>
3134
/// Deserializes an object from a BsonReader.
@@ -47,8 +50,8 @@ public override object Deserialize(BsonReader bsonReader, Type nominalType, Type
4750
else
4851
{
4952
bsonReader.ReadStartArray();
50-
var x = bsonReader.ReadDouble();
51-
var y = bsonReader.ReadDouble();
53+
var x = (double)__doubleSerializer.Deserialize(bsonReader, typeof(double), null);
54+
var y = (double)__doubleSerializer.Deserialize(bsonReader, typeof(double), null);
5255
bsonReader.ReadEndArray();
5356

5457
return new GeoJson2DCoordinates(x, y);

MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJson2DGeographicCoordinatesSerializer.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ namespace MongoDB.Driver.GeoJsonObjectModel.Serializers
2626
/// </summary>
2727
public class GeoJson2DGeographicCoordinatesSerializer : BsonBaseSerializer
2828
{
29+
// private static fields
30+
private static readonly IBsonSerializer __doubleSerializer = new DoubleSerializer();
31+
2932
// public methods
3033
/// <summary>
3134
/// Deserializes an object from a BsonReader.
@@ -47,8 +50,8 @@ public override object Deserialize(BsonReader bsonReader, Type nominalType, Type
4750
else
4851
{
4952
bsonReader.ReadStartArray();
50-
var longitude = bsonReader.ReadDouble();
51-
var latitude = bsonReader.ReadDouble();
53+
var longitude = (double)__doubleSerializer.Deserialize(bsonReader, typeof(double), null);
54+
var latitude = (double)__doubleSerializer.Deserialize(bsonReader, typeof(double), null);
5255
bsonReader.ReadEndArray();
5356

5457
return new GeoJson2DGeographicCoordinates(longitude, latitude);

MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJson2DProjectedCoordinatesSerializer.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ namespace MongoDB.Driver.GeoJsonObjectModel.Serializers
2626
/// </summary>
2727
public class GeoJson2DProjectedCoordinatesSerializer : BsonBaseSerializer
2828
{
29+
// private static fields
30+
private static readonly IBsonSerializer __doubleSerializer = new DoubleSerializer();
31+
2932
// public methods
3033
/// <summary>
3134
/// Deserializes an object from a BsonReader.
@@ -47,8 +50,8 @@ public override object Deserialize(BsonReader bsonReader, Type nominalType, Type
4750
else
4851
{
4952
bsonReader.ReadStartArray();
50-
var easting = bsonReader.ReadDouble();
51-
var northing = bsonReader.ReadDouble();
53+
var easting = (double)__doubleSerializer.Deserialize(bsonReader, typeof(double), null);
54+
var northing = (double)__doubleSerializer.Deserialize(bsonReader, typeof(double), null);
5255
bsonReader.ReadEndArray();
5356

5457
return new GeoJson2DProjectedCoordinates(easting, northing);

MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJson3DCoordinatesSerializer.cs

+6-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ namespace MongoDB.Driver.GeoJsonObjectModel.Serializers
2626
/// </summary>
2727
public class GeoJson3DCoordinatesSerializer : BsonBaseSerializer
2828
{
29+
// private static fields
30+
private static readonly IBsonSerializer __doubleSerializer = new DoubleSerializer();
31+
2932
// public methods
3033
/// <summary>
3134
/// Deserializes an object from a BsonReader.
@@ -47,9 +50,9 @@ public override object Deserialize(BsonReader bsonReader, Type nominalType, Type
4750
else
4851
{
4952
bsonReader.ReadStartArray();
50-
var x = bsonReader.ReadDouble();
51-
var y = bsonReader.ReadDouble();
52-
var z = bsonReader.ReadDouble();
53+
var x = (double)__doubleSerializer.Deserialize(bsonReader, typeof(double), null);
54+
var y = (double)__doubleSerializer.Deserialize(bsonReader, typeof(double), null);
55+
var z = (double)__doubleSerializer.Deserialize(bsonReader, typeof(double), null);
5356
bsonReader.ReadEndArray();
5457

5558
return new GeoJson3DCoordinates(x, y, z);

MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJson3DGeographicCoordinatesSerializer.cs

+6-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ namespace MongoDB.Driver.GeoJsonObjectModel.Serializers
2626
/// </summary>
2727
public class GeoJson3DGeographicCoordinatesSerializer : BsonBaseSerializer
2828
{
29+
// private static fields
30+
private static readonly IBsonSerializer __doubleSerializer = new DoubleSerializer();
31+
2932
// public methods
3033
/// <summary>
3134
/// Deserializes an object from a BsonReader.
@@ -47,9 +50,9 @@ public override object Deserialize(BsonReader bsonReader, Type nominalType, Type
4750
else
4851
{
4952
bsonReader.ReadStartArray();
50-
var longitude = bsonReader.ReadDouble();
51-
var latitude = bsonReader.ReadDouble();
52-
var altitude = bsonReader.ReadDouble();
53+
var longitude = (double)__doubleSerializer.Deserialize(bsonReader, typeof(double), null);
54+
var latitude = (double)__doubleSerializer.Deserialize(bsonReader, typeof(double), null);
55+
var altitude = (double)__doubleSerializer.Deserialize(bsonReader, typeof(double), null);
5356
bsonReader.ReadEndArray();
5457

5558
return new GeoJson3DGeographicCoordinates(longitude, latitude, altitude);

MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJson3DProjectedCoordinatesSerializer.cs

+6-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ namespace MongoDB.Driver.GeoJsonObjectModel.Serializers
2626
/// </summary>
2727
public class GeoJson3DProjectedCoordinatesSerializer : BsonBaseSerializer
2828
{
29+
// private static fields
30+
private static readonly IBsonSerializer __doubleSerializer = new DoubleSerializer();
31+
2932
// public methods
3033
/// <summary>
3134
/// Deserializes an object from a BsonReader.
@@ -47,9 +50,9 @@ public override object Deserialize(BsonReader bsonReader, Type nominalType, Type
4750
else
4851
{
4952
bsonReader.ReadStartArray();
50-
var easting = bsonReader.ReadDouble();
51-
var northing = bsonReader.ReadDouble();
52-
var altitude = bsonReader.ReadDouble();
53+
var easting = (double)__doubleSerializer.Deserialize(bsonReader, typeof(double), null);
54+
var northing = (double)__doubleSerializer.Deserialize(bsonReader, typeof(double), null);
55+
var altitude = (double)__doubleSerializer.Deserialize(bsonReader, typeof(double), null);
5356
bsonReader.ReadEndArray();
5457

5558
return new GeoJson3DProjectedCoordinates(easting, northing, altitude);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* Copyright 2010-2013 10gen Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using MongoDB.Bson.Serialization;
17+
using MongoDB.Driver.GeoJsonObjectModel;
18+
using NUnit.Framework;
19+
20+
namespace MongoDB.DriverUnitTests.GeoJsonObjectModel
21+
{
22+
public class GeoJson2DCoordinatesTests
23+
{
24+
[Test]
25+
public void TestDeserializeDoubles()
26+
{
27+
var json = "[1.0, 2.0]";
28+
var coordinates = BsonSerializer.Deserialize<GeoJson2DCoordinates>(json);
29+
Assert.AreEqual(1.0, coordinates.X);
30+
Assert.AreEqual(2.0, coordinates.Y);
31+
}
32+
33+
[Test]
34+
public void TestDeserializeInts()
35+
{
36+
var json = "[1, 2]";
37+
var coordinates = BsonSerializer.Deserialize<GeoJson2DCoordinates>(json);
38+
Assert.AreEqual(1.0, coordinates.X);
39+
Assert.AreEqual(2.0, coordinates.Y);
40+
}
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* Copyright 2010-2013 10gen Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using MongoDB.Bson.Serialization;
17+
using MongoDB.Driver.GeoJsonObjectModel;
18+
using NUnit.Framework;
19+
20+
namespace MongoDB.DriverUnitTests.GeoJsonObjectModel
21+
{
22+
public class GeoJson2DGeographicCoordinatesTests
23+
{
24+
[Test]
25+
public void TestDeserializeDoubles()
26+
{
27+
var json = "[1.0, 2.0]";
28+
var coordinates = BsonSerializer.Deserialize<GeoJson2DGeographicCoordinates>(json);
29+
Assert.AreEqual(1.0, coordinates.Longitude);
30+
Assert.AreEqual(2.0, coordinates.Latitude);
31+
}
32+
33+
[Test]
34+
public void TestDeserializeInts()
35+
{
36+
var json = "[1, 2]";
37+
var coordinates = BsonSerializer.Deserialize<GeoJson2DGeographicCoordinates>(json);
38+
Assert.AreEqual(1.0, coordinates.Longitude);
39+
Assert.AreEqual(2.0, coordinates.Latitude);
40+
}
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* Copyright 2010-2013 10gen Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using MongoDB.Bson.Serialization;
17+
using MongoDB.Driver.GeoJsonObjectModel;
18+
using NUnit.Framework;
19+
20+
namespace MongoDB.DriverUnitTests.GeoJsonObjectModel
21+
{
22+
public class GeoJson2DProjectedCoordinatesTests
23+
{
24+
[Test]
25+
public void TestDeserializeDoubles()
26+
{
27+
var json = "[1.0, 2.0]";
28+
var coordinates = BsonSerializer.Deserialize<GeoJson2DProjectedCoordinates>(json);
29+
Assert.AreEqual(1.0, coordinates.Easting);
30+
Assert.AreEqual(2.0, coordinates.Northing);
31+
}
32+
33+
[Test]
34+
public void TestDeserializeInts()
35+
{
36+
var json = "[1, 2]";
37+
var coordinates = BsonSerializer.Deserialize<GeoJson2DProjectedCoordinates>(json);
38+
Assert.AreEqual(1.0, coordinates.Easting);
39+
Assert.AreEqual(2.0, coordinates.Northing);
40+
}
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/* Copyright 2010-2013 10gen Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using MongoDB.Bson.Serialization;
17+
using MongoDB.Driver.GeoJsonObjectModel;
18+
using NUnit.Framework;
19+
20+
namespace MongoDB.DriverUnitTests.GeoJsonObjectModel
21+
{
22+
public class GeoJson3DCoordinatesTests
23+
{
24+
[Test]
25+
public void TestDeserializeDoubles()
26+
{
27+
var json = "[1.0, 2.0, 3.0]";
28+
var coordinates = BsonSerializer.Deserialize<GeoJson3DCoordinates>(json);
29+
Assert.AreEqual(1.0, coordinates.X);
30+
Assert.AreEqual(2.0, coordinates.Y);
31+
Assert.AreEqual(3.0, coordinates.Z);
32+
}
33+
34+
[Test]
35+
public void TestDeserializeInts()
36+
{
37+
var json = "[1, 2, 3]";
38+
var coordinates = BsonSerializer.Deserialize<GeoJson3DCoordinates>(json);
39+
Assert.AreEqual(1.0, coordinates.X);
40+
Assert.AreEqual(2.0, coordinates.Y);
41+
Assert.AreEqual(3.0, coordinates.Z);
42+
}
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/* Copyright 2010-2013 10gen Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using MongoDB.Bson.Serialization;
17+
using MongoDB.Driver.GeoJsonObjectModel;
18+
using NUnit.Framework;
19+
20+
namespace MongoDB.DriverUnitTests.GeoJsonObjectModel
21+
{
22+
public class GeoJson3DGeographicCoordinatesTests
23+
{
24+
[Test]
25+
public void TestDeserializeDoubles()
26+
{
27+
var json = "[1.0, 2.0, 3.0]";
28+
var coordinates = BsonSerializer.Deserialize<GeoJson3DGeographicCoordinates>(json);
29+
Assert.AreEqual(1.0, coordinates.Longitude);
30+
Assert.AreEqual(2.0, coordinates.Latitude);
31+
Assert.AreEqual(3.0, coordinates.Altitude);
32+
}
33+
34+
[Test]
35+
public void TestDeserializeInts()
36+
{
37+
var json = "[1, 2, 3]";
38+
var coordinates = BsonSerializer.Deserialize<GeoJson3DGeographicCoordinates>(json);
39+
Assert.AreEqual(1.0, coordinates.Longitude);
40+
Assert.AreEqual(2.0, coordinates.Latitude);
41+
Assert.AreEqual(3.0, coordinates.Altitude);
42+
}
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/* Copyright 2010-2013 10gen Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using MongoDB.Bson.Serialization;
17+
using MongoDB.Driver.GeoJsonObjectModel;
18+
using NUnit.Framework;
19+
20+
namespace MongoDB.DriverUnitTests.GeoJsonObjectModel
21+
{
22+
public class GeoJson3DProjectedCoordinatesTests
23+
{
24+
[Test]
25+
public void TestDeserializeDoubles()
26+
{
27+
var json = "[1.0, 2.0, 3.0]";
28+
var coordinates = BsonSerializer.Deserialize<GeoJson3DProjectedCoordinates>(json);
29+
Assert.AreEqual(1.0, coordinates.Easting);
30+
Assert.AreEqual(2.0, coordinates.Northing);
31+
Assert.AreEqual(3.0, coordinates.Altitude);
32+
}
33+
34+
[Test]
35+
public void TestDeserializeInts()
36+
{
37+
var json = "[1, 2, 3]";
38+
var coordinates = BsonSerializer.Deserialize<GeoJson3DProjectedCoordinates>(json);
39+
Assert.AreEqual(1.0, coordinates.Easting);
40+
Assert.AreEqual(2.0, coordinates.Northing);
41+
Assert.AreEqual(3.0, coordinates.Altitude);
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)