Skip to content

Commit 427e417

Browse files
committed
CSHARP-1064: Modified serializers (and several other places where BsonValues are created) to use implicit conversions so that they can take advantage of pre-created instances.
1 parent 01c34eb commit 427e417

File tree

13 files changed

+73
-77
lines changed

13 files changed

+73
-77
lines changed

src/MongoDB.Bson/IO/JsonReader.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2010-2014 MongoDB Inc.
1+
/* Copyright 2010-2015 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -1379,7 +1379,7 @@ private BsonValue ParseNumberConstructor()
13791379
throw new FormatException(message);
13801380
}
13811381
VerifyToken(")");
1382-
return new BsonInt32(value);
1382+
return (BsonInt32)value;
13831383
}
13841384

13851385
private BsonValue ParseNumberLongConstructor()
@@ -1401,7 +1401,7 @@ private BsonValue ParseNumberLongConstructor()
14011401
throw new FormatException(message);
14021402
}
14031403
VerifyToken(")");
1404-
return new BsonInt64(value);
1404+
return (BsonInt64)value;
14051405
}
14061406

14071407
private BsonValue ParseNumberLongExtendedJson()
@@ -1414,7 +1414,7 @@ private BsonValue ParseNumberLongExtendedJson()
14141414
throw new FormatException(message);
14151415
}
14161416
VerifyToken("}");
1417-
return new BsonInt64(long.Parse(valueToken.StringValue));
1417+
return (BsonInt64)long.Parse(valueToken.StringValue);
14181418
}
14191419

14201420
private BsonValue ParseObjectIdConstructor()
@@ -1513,7 +1513,7 @@ private BsonValue ParseSymbolExtendedJson()
15131513
throw new FormatException(message);
15141514
}
15151515
VerifyToken("}");
1516-
return new BsonString(nameToken.StringValue); // will be converted to a BsonSymbol at a higher level
1516+
return (BsonString)nameToken.StringValue; // will be converted to a BsonSymbol at a higher level
15171517
}
15181518

15191519
private BsonValue ParseTimestampConstructor()

src/MongoDB.Bson/ObjectModel/BsonArray.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2010-2014 MongoDB Inc.
1+
/* Copyright 2010-2015 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -18,8 +18,6 @@
1818
using System.Collections.Generic;
1919
using System.Linq;
2020
using System.Text;
21-
using MongoDB.Bson.IO;
22-
using MongoDB.Bson.Serialization;
2321
using MongoDB.Shared;
2422

2523
namespace MongoDB.Bson
@@ -341,7 +339,7 @@ public virtual BsonArray AddRange(IEnumerable<double> values)
341339

342340
foreach (var value in values)
343341
{
344-
Add(new BsonDouble(value));
342+
Add((BsonDouble)value);
345343
}
346344

347345
return this;
@@ -361,7 +359,7 @@ public virtual BsonArray AddRange(IEnumerable<int> values)
361359

362360
foreach (var value in values)
363361
{
364-
Add(new BsonInt32(value));
362+
Add((BsonInt32)value);
365363
}
366364

367365
return this;
@@ -381,7 +379,7 @@ public virtual BsonArray AddRange(IEnumerable<long> values)
381379

382380
foreach (var value in values)
383381
{
384-
Add(new BsonInt64(value));
382+
Add((BsonInt64)value);
385383
}
386384

387385
return this;
@@ -421,7 +419,7 @@ public virtual BsonArray AddRange(IEnumerable<string> values)
421419

422420
foreach (var value in values)
423421
{
424-
_values.Add((value == null) ? (BsonValue)BsonNull.Value : new BsonString(value));
422+
_values.Add((value == null) ? (BsonValue)BsonNull.Value : (BsonString)value);
425423
}
426424

427425
return this;

src/MongoDB.Bson/ObjectModel/BsonInt32.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,46 +57,46 @@ public BsonInt32(int value)
5757
/// <summary>
5858
/// Gets an instance of BsonInt32 that represents -1.
5959
/// </summary>
60-
[Obsolete("Use new BsonInt32(-1) instead.")]
60+
[Obsolete("Use (BsonInt32)(-1) instead.")]
6161
public static BsonInt32 MinusOne
6262
{
63-
get { return new BsonInt32(-1); }
63+
get { return (BsonInt32)(-1); }
6464
}
6565

6666
/// <summary>
6767
/// Gets an instance of BsonInt32 that represents -0.
6868
/// </summary>
69-
[Obsolete("Use new BsonInt32(0) instead.")]
69+
[Obsolete("Use (BsonInt32)0 instead.")]
7070
public static BsonInt32 Zero
7171
{
72-
get { return new BsonInt32(0); }
72+
get { return (BsonInt32)0; }
7373
}
7474

7575
/// <summary>
7676
/// Gets an instance of BsonInt32 that represents 1.
7777
/// </summary>
78-
[Obsolete("Use new BsonInt32(1) instead.")]
78+
[Obsolete("Use (BsonInt32)1 instead.")]
7979
public static BsonInt32 One
8080
{
81-
get { return new BsonInt32(1); }
81+
get { return (BsonInt32)1; }
8282
}
8383

8484
/// <summary>
8585
/// Gets an instance of BsonInt32 that represents 2.
8686
/// </summary>
87-
[Obsolete("Use new BsonInt32(2) instead.")]
87+
[Obsolete("Use (BsonInt32)2 instead.")]
8888
public static BsonInt32 Two
8989
{
90-
get { return new BsonInt32(2); }
90+
get { return (BsonInt32)2; }
9191
}
9292

9393
/// <summary>
9494
/// Gets an instance of BsonInt32 that represents 3.
9595
/// </summary>
96-
[Obsolete("Use new BsonInt32(3) instead.")]
96+
[Obsolete("Use (BsonInt32)3 instead.")]
9797
public static BsonInt32 Three
9898
{
99-
get { return new BsonInt32(3); }
99+
get { return (BsonInt32)3; }
100100
}
101101

102102
// public properties

src/MongoDB.Bson/ObjectModel/BsonTypeMapper.cs

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2010-2014 MongoDB Inc.
1+
/* Copyright 2010-2015 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -509,65 +509,65 @@ private static BsonValue Convert(object value, Conversion conversion)
509509
case Conversion.ByteArrayToBsonBinary: return new BsonBinaryData((byte[])value);
510510
case Conversion.ByteArrayToBsonObjectId: return new BsonObjectId(new ObjectId((byte[])value));
511511
case Conversion.ByteToBsonBoolean: return (BsonBoolean)((byte)value != 0);
512-
case Conversion.ByteToBsonDouble: return new BsonDouble((double)(byte)value);
513-
case Conversion.ByteToBsonInt32: return new BsonInt32((int)(byte)value);
514-
case Conversion.ByteToBsonInt64: return new BsonInt64((long)(byte)value);
512+
case Conversion.ByteToBsonDouble: return (BsonDouble)(double)(byte)value;
513+
case Conversion.ByteToBsonInt32: return (BsonInt32)(int)(byte)value;
514+
case Conversion.ByteToBsonInt64: return (BsonInt64)(long)(byte)value;
515515
case Conversion.CharToBsonBoolean: return (BsonBoolean)((char)value != 0);
516-
case Conversion.CharToBsonDouble: return new BsonDouble((double)(char)value);
517-
case Conversion.CharToBsonInt32: return new BsonInt32((int)(char)value);
518-
case Conversion.CharToBsonInt64: return new BsonInt64((long)(char)value);
516+
case Conversion.CharToBsonDouble: return (BsonDouble)(double)(char)value;
517+
case Conversion.CharToBsonInt32: return (BsonInt32)(int)(char)value;
518+
case Conversion.CharToBsonInt64: return (BsonInt64)(long)(char)value;
519519
case Conversion.DateTimeOffsetToBsonDateTime: return new BsonDateTime(((DateTimeOffset)value).UtcDateTime);
520520
case Conversion.DateTimeToBsonDateTime: return new BsonDateTime((DateTime)value);
521521
case Conversion.DoubleToBsonBoolean: var d = (double)value; return (BsonBoolean)(!(double.IsNaN(d) || d == 0.0));
522522
case Conversion.GuidToBsonBinary: return new BsonBinaryData((Guid)value);
523523
case Conversion.Int16ToBsonBoolean: return (BsonBoolean)((short)value != 0);
524-
case Conversion.Int16ToBsonDouble: return new BsonDouble((double)(short)value);
525-
case Conversion.Int16ToBsonInt32: return new BsonInt32((int)(short)value);
526-
case Conversion.Int16ToBsonInt64: return new BsonInt64((long)(short)value);
524+
case Conversion.Int16ToBsonDouble: return (BsonDouble)(double)(short)value;
525+
case Conversion.Int16ToBsonInt32: return (BsonInt32)(int)(short)value;
526+
case Conversion.Int16ToBsonInt64: return (BsonInt64)(long)(short)value;
527527
case Conversion.Int32ToBsonBoolean: return (BsonBoolean)((int)value != 0);
528-
case Conversion.Int32ToBsonDouble: return new BsonDouble((double)(int)value);
529-
case Conversion.Int32ToBsonInt64: return new BsonInt64((long)(int)value);
528+
case Conversion.Int32ToBsonDouble: return (BsonDouble)(double)(int)value;
529+
case Conversion.Int32ToBsonInt64: return (BsonInt64)(long)(int)value;
530530
case Conversion.Int64ToBsonBoolean: return (BsonBoolean)((long)value != 0);
531-
case Conversion.Int64ToBsonDouble: return new BsonDouble((double)(long)value);
531+
case Conversion.Int64ToBsonDouble: return (BsonDouble)(double)(long)value;
532532
case Conversion.Int64ToBsonTimestamp: return new BsonTimestamp((long)value);
533533
case Conversion.NewBsonBoolean: return (BsonBoolean)((bool)value);
534-
case Conversion.NewBsonDouble: return new BsonDouble((double)value);
535-
case Conversion.NewBsonInt32: return new BsonInt32((int)value);
536-
case Conversion.NewBsonInt64: return new BsonInt64((long)value);
534+
case Conversion.NewBsonDouble: return (BsonDouble)(double)value;
535+
case Conversion.NewBsonInt32: return (BsonInt32)(int)value;
536+
case Conversion.NewBsonInt64: return (BsonInt64)(long)value;
537537
case Conversion.NewBsonObjectId: return new BsonObjectId((ObjectId)value);
538-
case Conversion.NewBsonString: return new BsonString((string)value);
538+
case Conversion.NewBsonString: return (BsonString)(string)value;
539539
case Conversion.RegexToBsonRegularExpression: return new BsonRegularExpression((Regex)value);
540540
case Conversion.SByteToBsonBoolean: return (BsonBoolean)((sbyte)value != 0);
541-
case Conversion.SByteToBsonDouble: return new BsonDouble((double)(sbyte)value);
542-
case Conversion.SByteToBsonInt32: return new BsonInt32((int)(sbyte)value);
543-
case Conversion.SByteToBsonInt64: return new BsonInt64((long)(sbyte)value);
541+
case Conversion.SByteToBsonDouble: return (BsonDouble)(double)(sbyte)value;
542+
case Conversion.SByteToBsonInt32: return (BsonInt32)(int)(sbyte)value;
543+
case Conversion.SByteToBsonInt64: return (BsonInt64)(long)(sbyte)value;
544544
case Conversion.SingleToBsonBoolean: var f = (float)value; return (BsonBoolean)(!(float.IsNaN(f) || f == 0.0f));
545-
case Conversion.SingleToBsonDouble: return new BsonDouble((double)(float)value);
545+
case Conversion.SingleToBsonDouble: return (BsonDouble)(double)(float)value;
546546
case Conversion.StringToBsonBoolean: return (BsonBoolean)((string)value != "");
547547
case Conversion.StringToBsonDateTime:
548548
var formats = new string[] { "yyyy-MM-ddK", "yyyy-MM-ddTHH:mm:ssK", "yyyy-MM-ddTHH:mm:ss.FFFFFFFK" };
549549
var dt = DateTime.ParseExact((string)value, formats, null, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal);
550550
return new BsonDateTime(dt);
551-
case Conversion.StringToBsonDouble: return new BsonDouble(JsonConvert.ToDouble((string)value));
552-
case Conversion.StringToBsonInt32: return new BsonInt32(JsonConvert.ToInt32((string)value));
553-
case Conversion.StringToBsonInt64: return new BsonInt64(JsonConvert.ToInt64((string)value));
551+
case Conversion.StringToBsonDouble: return (BsonDouble)JsonConvert.ToDouble((string)value);
552+
case Conversion.StringToBsonInt32: return (BsonInt32)JsonConvert.ToInt32((string)value);
553+
case Conversion.StringToBsonInt64: return (BsonInt64)JsonConvert.ToInt64((string)value);
554554
case Conversion.StringToBsonJavaScript: return new BsonJavaScript((string)value);
555555
case Conversion.StringToBsonJavaScriptWithScope: return new BsonJavaScriptWithScope((string)value, new BsonDocument());
556556
case Conversion.StringToBsonObjectId: return new BsonObjectId(ObjectId.Parse((string)value));
557557
case Conversion.StringToBsonRegularExpression: return new BsonRegularExpression((string)value);
558558
case Conversion.StringToBsonSymbol: return BsonSymbolTable.Lookup((string)value);
559559
case Conversion.StringToBsonTimestamp: return new BsonTimestamp(JsonConvert.ToInt64((string)value));
560560
case Conversion.UInt16ToBsonBoolean: return (BsonValue)((ushort)value != 0);
561-
case Conversion.UInt16ToBsonDouble: return new BsonDouble((double)(ushort)value);
562-
case Conversion.UInt16ToBsonInt32: return new BsonInt32((int)(ushort)value);
563-
case Conversion.UInt16ToBsonInt64: return new BsonInt64((long)(ushort)value);
561+
case Conversion.UInt16ToBsonDouble: return (BsonDouble)(double)(ushort)value;
562+
case Conversion.UInt16ToBsonInt32: return (BsonInt32)(int)(ushort)value;
563+
case Conversion.UInt16ToBsonInt64: return (BsonInt64)(long)(ushort)value;
564564
case Conversion.UInt32ToBsonBoolean: return (BsonBoolean)((uint)value != 0);
565-
case Conversion.UInt32ToBsonDouble: return new BsonDouble((double)(uint)value);
566-
case Conversion.UInt32ToBsonInt32: return new BsonInt32((int)(uint)value);
567-
case Conversion.UInt32ToBsonInt64: return new BsonInt64((long)(uint)value);
565+
case Conversion.UInt32ToBsonDouble: return (BsonDouble)(double)(uint)value;
566+
case Conversion.UInt32ToBsonInt32: return (BsonInt32)(int)(uint)value;
567+
case Conversion.UInt32ToBsonInt64: return (BsonInt64)(long)(uint)value;
568568
case Conversion.UInt64ToBsonBoolean: return (BsonBoolean)((ulong)value != 0);
569-
case Conversion.UInt64ToBsonDouble: return new BsonDouble((double)(ulong)value);
570-
case Conversion.UInt64ToBsonInt64: return new BsonInt64((long)(ulong)value);
569+
case Conversion.UInt64ToBsonDouble: return (BsonDouble)(double)(ulong)value;
570+
case Conversion.UInt64ToBsonInt64: return (BsonInt64)(long)(ulong)value;
571571
case Conversion.UInt64ToBsonTimestamp: return new BsonTimestamp((long)(ulong)value);
572572
}
573573

src/MongoDB.Bson/ObjectModel/BsonValue.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
using System;
1717
using System.Collections.Generic;
1818
using System.Text.RegularExpressions;
19-
using MongoDB.Bson.IO;
20-
using MongoDB.Bson.Serialization;
2119

2220
namespace MongoDB.Bson
2321
{
@@ -1056,11 +1054,11 @@ public static BsonValue Create(object value)
10561054
}
10571055
else if (value is int)
10581056
{
1059-
return new BsonInt32((int)value);
1057+
return (BsonInt32)(int)value;
10601058
}
10611059
else if (value is string)
10621060
{
1063-
return new BsonString((string)value);
1061+
return (BsonString)(string)value;
10641062
}
10651063
else if (value is bool)
10661064
{
@@ -1072,11 +1070,11 @@ public static BsonValue Create(object value)
10721070
}
10731071
else if (value is long)
10741072
{
1075-
return new BsonInt64((long)value);
1073+
return (BsonInt64)(long)value;
10761074
}
10771075
else if (value is double)
10781076
{
1079-
return new BsonDouble((double)value);
1077+
return (BsonDouble)(double)value;
10801078
}
10811079
else
10821080
{

src/MongoDB.Bson/Serialization/Serializers/BsonDoubleSerializer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2010-2014 MongoDB Inc.
1+
/* Copyright 2010-2015 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -52,7 +52,7 @@ public static BsonDoubleSerializer Instance
5252
protected override BsonDouble DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args)
5353
{
5454
var bsonReader = context.Reader;
55-
return new BsonDouble(bsonReader.ReadDouble());
55+
return (BsonDouble)bsonReader.ReadDouble();
5656
}
5757

5858
/// <summary>

src/MongoDB.Bson/Serialization/Serializers/BsonInt32Serializer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2010-2014 MongoDB Inc.
1+
/* Copyright 2010-2015 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -52,7 +52,7 @@ public static BsonInt32Serializer Instance
5252
protected override BsonInt32 DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args)
5353
{
5454
var bsonReader = context.Reader;
55-
return new BsonInt32(bsonReader.ReadInt32());
55+
return (BsonInt32)bsonReader.ReadInt32();
5656
}
5757

5858
/// <summary>

src/MongoDB.Bson/Serialization/Serializers/BsonInt64Serializer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2010-2014 MongoDB Inc.
1+
/* Copyright 2010-2015 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -52,7 +52,7 @@ public static BsonInt64Serializer Instance
5252
protected override BsonInt64 DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args)
5353
{
5454
var bsonReader = context.Reader;
55-
return new BsonInt64(bsonReader.ReadInt64());
55+
return (BsonInt64)bsonReader.ReadInt64();
5656
}
5757

5858
/// <summary>

src/MongoDB.Bson/Serialization/Serializers/BsonStringSerializer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2010-2014 MongoDB Inc.
1+
/* Copyright 2010-2015 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -52,7 +52,7 @@ public static BsonStringSerializer Instance
5252
protected override BsonString DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args)
5353
{
5454
var bsonReader = context.Reader;
55-
return new BsonString(bsonReader.ReadString());
55+
return (BsonString)bsonReader.ReadString();
5656
}
5757

5858
/// <summary>

src/MongoDB.Driver.Core/WriteConcern.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2013-2014 MongoDB Inc.
1+
/* Copyright 2013-2015 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -514,7 +514,7 @@ public override int GetHashCode()
514514
/// <inheritdoc/>
515515
public override BsonValue ToBsonValue()
516516
{
517-
return new BsonInt32(_value);
517+
return (BsonInt32)_value;
518518
}
519519

520520
/// <inheritdoc/>

src/MongoDB.Driver.Legacy/GridFS/MongoGridFS.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2010-2014 MongoDB Inc.
1+
/* Copyright 2010-2015 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -1028,7 +1028,7 @@ public MongoGridFSFileInfo Upload(
10281028
{
10291029
{ "_id", ObjectId.GenerateNewId() },
10301030
{ "files_id", files_id },
1031-
{ "n", (n < int.MaxValue) ? (BsonValue)new BsonInt32((int)n) : new BsonInt64(n) },
1031+
{ "n", n < int.MaxValue ? (BsonValue)(BsonInt32)(int)n : (BsonInt64)n },
10321032
{ "data", new BsonBinaryData(data) }
10331033
};
10341034
chunksCollection.Insert(chunk, _settings.WriteConcern);

0 commit comments

Comments
 (0)