Skip to content

Commit 1f52042

Browse files
author
rstam
committed
CSHARP-694: Added unit tests for using different Encodings with JsonReader(Writer). Realized that the Encoding is actually set at the StreamReader(Writer) level so JsonReader(Writer)Settings don't need an Encoding property.
1 parent 676753f commit 1f52042

File tree

5 files changed

+143
-27
lines changed

5 files changed

+143
-27
lines changed

MongoDB.Bson/IO/BsonWriterSettings.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
*/
1515

1616
using System;
17-
using System.Text;
1817

1918
namespace MongoDB.Bson.IO
2019
{

MongoDB.Bson/IO/JsonReaderSettings.cs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
*/
1515

1616
using System;
17-
using System.Text;
1817

1918
namespace MongoDB.Bson.IO
2019
{
@@ -29,7 +28,6 @@ public class JsonReaderSettings : BsonReaderSettings
2928

3029
// private fields
3130
private bool _closeInput = false;
32-
private Encoding _encoding = new UTF8Encoding(false, true);
3331

3432
// constructors
3533
/// <summary>
@@ -82,23 +80,6 @@ public bool CloseInput
8280
}
8381
}
8482

85-
/// <summary>
86-
/// Gets or sets the Encoding.
87-
/// </summary>
88-
public Encoding Encoding
89-
{
90-
get { return _encoding; }
91-
set
92-
{
93-
if (value == null)
94-
{
95-
throw new ArgumentNullException("value");
96-
}
97-
if (IsFrozen) { throw new InvalidOperationException("JsonReaderSettings is frozen."); }
98-
_encoding = value;
99-
}
100-
}
101-
10283
// public methods
10384
/// <summary>
10485
/// Creates a clone of the settings.
@@ -119,7 +100,6 @@ protected override BsonReaderSettings CloneImplementation()
119100
var clone = new JsonReaderSettings
120101
{
121102
CloseInput = _closeInput,
122-
Encoding = _encoding,
123103
GuidRepresentation = GuidRepresentation
124104
};
125105
return clone;

MongoDB.Bson/IO/JsonWriterSettings.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class JsonWriterSettings : BsonWriterSettings
2929

3030
// private fields
3131
private bool _closeOutput = false;
32-
private Encoding _encoding = new UTF8Encoding(false, true);
32+
private Encoding _encoding = Encoding.UTF8;
3333
private bool _indent = false;
3434
private string _indentChars = " ";
3535
private string _newLineChars = "\r\n";
@@ -108,17 +108,14 @@ public bool CloseOutput
108108
}
109109

110110
/// <summary>
111-
/// Gets or sets the Encoding.
111+
/// Gets or sets the output Encoding.
112112
/// </summary>
113+
[Obsolete("Set the Encoding when you create a StreamWriter instead (this property is ignored).")]
113114
public Encoding Encoding
114115
{
115116
get { return _encoding; }
116117
set
117118
{
118-
if (value == null)
119-
{
120-
throw new ArgumentNullException("value");
121-
}
122119
if (IsFrozen) { throw new InvalidOperationException("JsonWriterSettings is frozen."); }
123120
_encoding = value;
124121
}
@@ -209,7 +206,9 @@ protected override BsonWriterSettings CloneImplementation()
209206
var clone = new JsonWriterSettings
210207
{
211208
CloseOutput = _closeOutput,
209+
#pragma warning disable 618
212210
Encoding = _encoding,
211+
#pragma warning restore
213212
GuidRepresentation = GuidRepresentation,
214213
Indent = _indent,
215214
IndentChars = _indentChars,

MongoDB.BsonUnitTests/IO/JsonReaderTests.cs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System;
1717
using System.IO;
1818
using System.Linq;
19+
using System.Text;
1920
using MongoDB.Bson;
2021
using MongoDB.Bson.IO;
2122
using MongoDB.Bson.Serialization;
@@ -653,5 +654,83 @@ public void TestUndefined()
653654
}
654655
Assert.AreEqual(json, BsonSerializer.Deserialize<BsonUndefined>(new StringReader(json)).ToJson());
655656
}
657+
658+
[Test]
659+
public void TestUtf16BigEndian()
660+
{
661+
var encoding = new UnicodeEncoding(true, false, true);
662+
663+
var bytes = BsonUtils.ParseHexString("007b00200022007800220020003a002000310020007d");
664+
using (var memoryStream = new MemoryStream(bytes))
665+
using (var streamReader = new StreamReader(memoryStream, encoding))
666+
{
667+
var document = BsonSerializer.Deserialize<BsonDocument>(streamReader);
668+
Assert.AreEqual(1, document["x"].AsInt32);
669+
}
670+
}
671+
672+
[Test]
673+
public void TestUtf16BigEndianAutoDetect()
674+
{
675+
var bytes = BsonUtils.ParseHexString("feff007b00200022007800220020003a002000310020007d");
676+
using (var memoryStream = new MemoryStream(bytes))
677+
using (var streamReader = new StreamReader(memoryStream, true))
678+
{
679+
var document = BsonSerializer.Deserialize<BsonDocument>(streamReader);
680+
Assert.AreEqual(1, document["x"].AsInt32);
681+
}
682+
}
683+
684+
[Test]
685+
public void TestUtf16LittleEndian()
686+
{
687+
var encoding = new UnicodeEncoding(false, false, true);
688+
689+
var bytes = BsonUtils.ParseHexString("7b00200022007800220020003a002000310020007d00");
690+
using (var memoryStream = new MemoryStream(bytes))
691+
using (var streamReader = new StreamReader(memoryStream, encoding))
692+
{
693+
var document = BsonSerializer.Deserialize<BsonDocument>(streamReader);
694+
Assert.AreEqual(1, document["x"].AsInt32);
695+
}
696+
}
697+
698+
[Test]
699+
public void TestUtf16LittleEndianAutoDetect()
700+
{
701+
var bytes = BsonUtils.ParseHexString("fffe7b00200022007800220020003a002000310020007d00");
702+
using (var memoryStream = new MemoryStream(bytes))
703+
using (var streamReader = new StreamReader(memoryStream, true))
704+
{
705+
var document = BsonSerializer.Deserialize<BsonDocument>(streamReader);
706+
Assert.AreEqual(1, document["x"].AsInt32);
707+
}
708+
}
709+
710+
[Test]
711+
public void TestUtf8()
712+
{
713+
var encoding = new UTF8Encoding(false, true);
714+
715+
var bytes = BsonUtils.ParseHexString("7b20227822203a2031207d");
716+
using (var memoryStream = new MemoryStream(bytes))
717+
using (var streamReader = new StreamReader(memoryStream, encoding))
718+
{
719+
var document = BsonSerializer.Deserialize<BsonDocument>(streamReader);
720+
Assert.AreEqual(1, document["x"].AsInt32);
721+
}
722+
}
723+
724+
[Test]
725+
public void TestUtf8AutoDetect()
726+
{
727+
var bytes = BsonUtils.ParseHexString("7b20227822203a2031207d");
728+
using (var memoryStream = new MemoryStream(bytes))
729+
using (var streamReader = new StreamReader(memoryStream, true))
730+
{
731+
var document = BsonSerializer.Deserialize<BsonDocument>(streamReader);
732+
Assert.AreEqual(1, document["x"].AsInt32);
733+
}
734+
}
656735
}
657736
}

MongoDB.BsonUnitTests/IO/JsonWriterTests.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
*/
1515

1616
using System;
17+
using System.IO;
18+
using System.Text;
1719
using MongoDB.Bson;
1820
using MongoDB.Bson.IO;
1921
using MongoDB.Bson.Serialization;
@@ -525,5 +527,62 @@ public void TestUndefined()
525527
string actual = document.ToJson();
526528
Assert.AreEqual(expected, actual);
527529
}
530+
531+
[Test]
532+
public void TestUtf16BigEndian()
533+
{
534+
var encoding = new UnicodeEncoding(true, true, true);
535+
536+
using (var memoryStream = new MemoryStream())
537+
{
538+
using (var streamWriter = new StreamWriter(memoryStream, encoding))
539+
using (var jsonWriter = new JsonWriter(streamWriter, JsonWriterSettings.Defaults))
540+
{
541+
var document = new BsonDocument("x", 1);
542+
BsonSerializer.Serialize(jsonWriter, document);
543+
}
544+
545+
var bytes = memoryStream.ToArray();
546+
Assert.AreEqual("feff007b00200022007800220020003a002000310020007d", BsonUtils.ToHexString(bytes));
547+
}
548+
}
549+
550+
[Test]
551+
public void TestUtf16LittleEndian()
552+
{
553+
var encoding = new UnicodeEncoding(false, true, true);
554+
555+
using (var memoryStream = new MemoryStream())
556+
{
557+
using (var streamWriter = new StreamWriter(memoryStream, encoding))
558+
using (var jsonWriter = new JsonWriter(streamWriter, JsonWriterSettings.Defaults))
559+
{
560+
var document = new BsonDocument("x", 1);
561+
BsonSerializer.Serialize(jsonWriter, document);
562+
}
563+
564+
var bytes = memoryStream.ToArray();
565+
Assert.AreEqual("fffe7b00200022007800220020003a002000310020007d00", BsonUtils.ToHexString(bytes));
566+
}
567+
}
568+
569+
[Test]
570+
public void TestUtf8()
571+
{
572+
var encoding = new UTF8Encoding(true, true);
573+
574+
using (var memoryStream = new MemoryStream())
575+
{
576+
using (var streamWriter = new StreamWriter(memoryStream, encoding))
577+
using (var jsonWriter = new JsonWriter(streamWriter, JsonWriterSettings.Defaults))
578+
{
579+
var document = new BsonDocument("x", 1);
580+
BsonSerializer.Serialize(jsonWriter, document);
581+
}
582+
583+
var bytes = memoryStream.ToArray();
584+
Assert.AreEqual("efbbbf7b20227822203a2031207d", BsonUtils.ToHexString(bytes));
585+
}
586+
}
528587
}
529588
}

0 commit comments

Comments
 (0)