Json.NET
Code Coverage Statistics for Source File

Newtonsoft.Json.Tests\Bson\BsonWriterTests.cs

Symbol Coverage: 98.66% (295 of 299)

Branch Coverage: 85.71% (24 of 28)

Cyclomatic Complexity Avg: 1.20 Max:3

Code Lines: 338


L V Source
1
#region License
2
// Copyright (c) 2007 James Newton-King
3
//
4
// Permission is hereby granted, free of charge, to any person
5
// obtaining a copy of this software and associated documentation
6
// files (the "Software"), to deal in the Software without
7
// restriction, including without limitation the rights to use,
8
// copy, modify, merge, publish, distribute, sublicense, and/or sell
9
// copies of the Software, and to permit persons to whom the
10
// Software is furnished to do so, subject to the following
11
// conditions:
12
//
13
// The above copyright notice and this permission notice shall be
14
// included in all copies or substantial portions of the Software.
15
//
16
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
// OTHER DEALINGS IN THE SOFTWARE.
24
#endregion
25

  
26
using System;
27
using System.Collections.Generic;
28
using System.Linq;
29
using System.Text;
30
using NUnit.Framework;
31
using Newtonsoft.Json.Bson;
32
using System.IO;
33
using Newtonsoft.Json.Utilities;
34
using Newtonsoft.Json.Tests.TestObjects;
35
using System.Globalization;
36

  
37
namespace Newtonsoft.Json.Tests.Bson
38
{
39
  public class BsonWriterTests : TestFixtureBase
40
  {
41
    [Test]
42
    public void WriteSingleObject()
43
    {
44
 1
      MemoryStream ms = new MemoryStream();
45
 1
      BsonWriter writer = new BsonWriter(ms);
46

  
47
 1
      writer.WriteStartObject();
48
 1
      writer.WritePropertyName("Blah");
49
 1
      writer.WriteValue(1);
50
 1
      writer.WriteEndObject();
51

  
52
 1
      string bson = MiscellaneousUtils.BytesToHex(ms.ToArray());
53
 1
      Assert.AreEqual("0F-00-00-00-10-42-6C-61-68-00-01-00-00-00-00", bson);
54
 1
    }
55

  
56
#if !PocketPC && !NET20
57
    [Test]
58
    public void WriteValues()
59
    {
60
 1
      MemoryStream ms = new MemoryStream();
61
 1
      BsonWriter writer = new BsonWriter(ms);
62

  
63
 1
      writer.WriteStartArray();
64
 1
      writer.WriteValue(long.MaxValue);
65
 1
      writer.WriteValue((ulong)long.MaxValue);
66
 1
      writer.WriteValue(int.MaxValue);
67
 1
      writer.WriteValue((uint)int.MaxValue);
68
 1
      writer.WriteValue(byte.MaxValue);
69
 1
      writer.WriteValue(sbyte.MaxValue);
70
 1
      writer.WriteValue('a');
71
 1
      writer.WriteValue(decimal.MaxValue);
72
 1
      writer.WriteValue(double.MaxValue);
73
 1
      writer.WriteValue(float.MaxValue);
74
 1
      writer.WriteValue(true);
75
 1
      writer.WriteValue(new byte[] { 0, 1, 2, 3, 4 });
76
 1
      writer.WriteValue(new DateTimeOffset(2000, 12, 29, 12, 30, 0, TimeSpan.Zero));
77
 1
      writer.WriteValue(new DateTime(2000, 12, 29, 12, 30, 0, DateTimeKind.Utc));
78
 1
      writer.WriteEnd();
79

  
80
 1
      string bson = MiscellaneousUtils.BytesToHex(ms.ToArray());
81
 1
      Assert.AreEqual("8C-00-00-00-12-30-00-FF-FF-FF-FF-FF-FF-FF-7F-12-31-00-FF-FF-FF-FF-FF-FF-FF-7F-10-32-00-FF-FF-FF-7F-10-33-00-FF-FF-FF-7F-10-34-00-FF-00-00-00-10-35-00-7F-00-00-00-02-36-00-02-00-00-00-61-00-01-37-00-00-00-00-00-00-00-F0-45-01-38-00-FF-FF-FF-FF-FF-FF-EF-7F-01-39-00-00-00-00-E0-FF-FF-EF-47-08-31-30-00-01-05-31-31-00-05-00-00-00-02-00-01-02-03-04-09-31-32-00-40-C5-E2-BA-E3-00-00-00-09-31-33-00-40-C5-E2-BA-E3-00-00-00-00", bson);
82
 1
    }
83
#endif
84

  
85
    [Test]
86
    public void WriteArrayBsonFromSite()
87
    {
88
 1
      MemoryStream ms = new MemoryStream();
89
 1
      BsonWriter writer = new BsonWriter(ms);
90
 1
      writer.WriteStartArray();
91
 1
      writer.WriteValue("a");
92
 1
      writer.WriteValue("b");
93
 1
      writer.WriteValue("c");
94
 1
      writer.WriteEndArray();
95
      
96
 1
      writer.Flush();
97

  
98
 1
      ms.Seek(0, SeekOrigin.Begin);
99

  
100
 1
      string expected = "20-00-00-00-02-30-00-02-00-00-00-61-00-02-31-00-02-00-00-00-62-00-02-32-00-02-00-00-00-63-00-00";
101
 1
      string bson = MiscellaneousUtils.BytesToHex(ms.ToArray());
102

  
103
 1
      Assert.AreEqual(expected, bson);
104
 1
    }
105

  
106
    [Test]
107
    public void WriteBytes()
108
    {
109
 1
      byte[] data = Encoding.UTF8.GetBytes("Hello world!");
110

  
111
 1
      MemoryStream ms = new MemoryStream();
112
 1
      BsonWriter writer = new BsonWriter(ms);
113
 1
      writer.WriteStartArray();
114
 1
      writer.WriteValue("a");
115
 1
      writer.WriteValue("b");
116
 1
      writer.WriteValue(data);
117
 1
      writer.WriteEndArray();
118

  
119
 1
      writer.Flush();
120

  
121
 1
      ms.Seek(0, SeekOrigin.Begin);
122

  
123
 1
      string expected = "2B-00-00-00-02-30-00-02-00-00-00-61-00-02-31-00-02-00-00-00-62-00-05-32-00-0C-00-00-00-02-48-65-6C-6C-6F-20-77-6F-72-6C-64-21-00";
124
 1
      string bson = MiscellaneousUtils.BytesToHex(ms.ToArray());
125

  
126
 1
      Assert.AreEqual(expected, bson);
127
 1
    }
128

  
129
    [Test]
130
    public void WriteNestedArray()
131
    {
132
 1
      MemoryStream ms = new MemoryStream();
133
 1
      BsonWriter writer = new BsonWriter(ms);
134
 1
      writer.WriteStartObject();
135

  
136
 1
      writer.WritePropertyName("_id");
137
 1
      writer.WriteValue(MiscellaneousUtils.HexToBytes("4A-78-93-79-17-22-00-00-00-00-61-CF"));
138

  
139
 1
      writer.WritePropertyName("a");
140
 1
      writer.WriteStartArray();
141
 1
      for (int i = 1; i <= 8; i++)
142
      {
143
 8
        double value = (i != 5)
144
 8
                         ? Convert.ToDouble(i)
145
 8
                         : 5.78960446186581E+77d;
146

  
147
 8
        writer.WriteValue(value);
148
      }
149
 1
      writer.WriteEndArray();
150

  
151
 1
      writer.WritePropertyName("b");
152
 1
      writer.WriteValue("test");
153

  
154
 1
      writer.WriteEndObject();
155

  
156
 1
      writer.Flush();
157

  
158
 1
      ms.Seek(0, SeekOrigin.Begin);
159

  
160
 1
      string expected = "87-00-00-00-05-5F-69-64-00-0C-00-00-00-02-4A-78-93-79-17-22-00-00-00-00-61-CF-04-61-00-5D-00-00-00-01-30-00-00-00-00-00-00-00-F0-3F-01-31-00-00-00-00-00-00-00-00-40-01-32-00-00-00-00-00-00-00-08-40-01-33-00-00-00-00-00-00-00-10-40-01-34-00-00-00-00-00-00-00-14-50-01-35-00-00-00-00-00-00-00-18-40-01-36-00-00-00-00-00-00-00-1C-40-01-37-00-00-00-00-00-00-00-20-40-00-02-62-00-05-00-00-00-74-65-73-74-00-00";
161
 1
      string bson = MiscellaneousUtils.BytesToHex(ms.ToArray());
162

  
163
 1
      Assert.AreEqual(expected, bson);
164
 1
    }
165

  
166
    [Test]
167
    public void WriteSerializedStore()
168
    {
169
 1
      MemoryStream ms = new MemoryStream();
170
 1
      BsonWriter writer = new BsonWriter(ms);
171

  
172
 1
      Store s1 = new Store();
173
 1
      s1.Color = StoreColor.White;
174
 1
      s1.Cost = 999.59m;
175
 1
      s1.Employees = int.MaxValue - 1;
176
 1
      s1.Open = true;
177
 1
      s1.product.Add(new Product
178
 1
                       {
179
 1
                         ExpiryDate = new DateTime(2000, 9, 28, 3, 59, 58, DateTimeKind.Local),
180
 1
                         Name = "BSON!",
181
 1
                         Price = -0.1m,
182
 1
                         Sizes = new [] { "First", "Second" }
183
 1
                       });
184
 1
      s1.Establised = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Local);
185

  
186
 1
      JsonSerializer serializer = new JsonSerializer();
187
 1
      serializer.Serialize(writer, s1);
188

  
189
 1
      ms.Seek(0, SeekOrigin.Begin);
190
 1
      BsonReader reader = new BsonReader(ms);
191
 1
      Store s2 = (Store)serializer.Deserialize(reader, typeof (Store));
192

  
193
 1
      Assert.AreNotEqual(s1, s2);
194
 1
      Assert.AreEqual(s1.Color, s2.Color);
195
 1
      Assert.AreEqual(s1.Cost, s2.Cost);
196
 1
      Assert.AreEqual(s1.Employees, s2.Employees);
197
 1
      Assert.AreEqual(s1.Escape, s2.Escape);
198
 1
      Assert.AreEqual(s1.Establised, s2.Establised);
199
 1
      Assert.AreEqual(s1.Mottos.Count, s2.Mottos.Count);
200
 1
      Assert.AreEqual(s1.Mottos.First(), s2.Mottos.First());
201
 1
      Assert.AreEqual(s1.Mottos.Last(), s2.Mottos.Last());
202
 1
      Assert.AreEqual(s1.Open, s2.Open);
203
 1
      Assert.AreEqual(s1.product.Count, s2.product.Count);
204
 1
      Assert.AreEqual(s1.RoomsPerFloor.Length, s2.RoomsPerFloor.Length);
205
 1
      Assert.AreEqual(s1.Symbol, s2.Symbol);
206
 1
      Assert.AreEqual(s1.Width, s2.Width);
207

  
208
 1
      MemoryStream ms1 = new MemoryStream();
209
 1
      BsonWriter writer1 = new BsonWriter(ms1);
210

  
211
 1
      serializer.Serialize(writer1, s1);
212

  
213
 1
      Assert.AreEqual(ms.ToArray(), ms1.ToArray());
214

  
215
 1
      string s = JsonConvert.SerializeObject(s1);
216
 1
      byte[] textData = Encoding.UTF8.GetBytes(s);
217

  
218
 1
      int l1 = textData.Length;
219
 1
      int l2 = ms.ToArray().Length;
220

  
221
 1
      Console.WriteLine(l1);
222
 1
      Console.WriteLine(l2);
223
 1
    }
224

  
225
    [Test]
226
    public void WriteLargeStrings()
227
    {
228
 1
      MemoryStream ms = new MemoryStream();
229
 1
      BsonWriter writer = new BsonWriter(ms);
230

  
231
 1
      StringBuilder largeStringBuilder = new StringBuilder();
232
 1
      for (int i = 0; i < 100; i++)
233
      {
234
 100
        if (i > 0)
235
 99
          largeStringBuilder.Append("-");
236

  
237
 100
        largeStringBuilder.Append(i.ToString(CultureInfo.InvariantCulture));
238
      }
239
 1
      string largeString = largeStringBuilder.ToString();
240

  
241
 1
      writer.WriteStartObject();
242
 1
      writer.WritePropertyName(largeString);
243
 1
      writer.WriteValue(largeString);
244
 1
      writer.WriteEndObject();
245

  
246
 1
      string bson = MiscellaneousUtils.BytesToHex(ms.ToArray());
247
 1
      Assert.AreEqual("4E-02-00-00-02-30-2D-31-2D-32-2D-33-2D-34-2D-35-2D-36-2D-37-2D-38-2D-39-2D-31-30-2D-31-31-2D-31-32-2D-31-33-2D-31-34-2D-31-35-2D-31-36-2D-31-37-2D-31-38-2D-31-39-2D-32-30-2D-32-31-2D-32-32-2D-32-33-2D-32-34-2D-32-35-2D-32-36-2D-32-37-2D-32-38-2D-32-39-2D-33-30-2D-33-31-2D-33-32-2D-33-33-2D-33-34-2D-33-35-2D-33-36-2D-33-37-2D-33-38-2D-33-39-2D-34-30-2D-34-31-2D-34-32-2D-34-33-2D-34-34-2D-34-35-2D-34-36-2D-34-37-2D-34-38-2D-34-39-2D-35-30-2D-35-31-2D-35-32-2D-35-33-2D-35-34-2D-35-35-2D-35-36-2D-35-37-2D-35-38-2D-35-39-2D-36-30-2D-36-31-2D-36-32-2D-36-33-2D-36-34-2D-36-35-2D-36-36-2D-36-37-2D-36-38-2D-36-39-2D-37-30-2D-37-31-2D-37-32-2D-37-33-2D-37-34-2D-37-35-2D-37-36-2D-37-37-2D-37-38-2D-37-39-2D-38-30-2D-38-31-2D-38-32-2D-38-33-2D-38-34-2D-38-35-2D-38-36-2D-38-37-2D-38-38-2D-38-39-2D-39-30-2D-39-31-2D-39-32-2D-39-33-2D-39-34-2D-39-35-2D-39-36-2D-39-37-2D-39-38-2D-39-39-00-22-01-00-00-30-2D-31-2D-32-2D-33-2D-34-2D-35-2D-36-2D-37-2D-38-2D-39-2D-31-30-2D-31-31-2D-31-32-2D-31-33-2D-31-34-2D-31-35-2D-31-36-2D-31-37-2D-31-38-2D-31-39-2D-32-30-2D-32-31-2D-32-32-2D-32-33-2D-32-34-2D-32-35-2D-32-36-2D-32-37-2D-32-38-2D-32-39-2D-33-30-2D-33-31-2D-33-32-2D-33-33-2D-33-34-2D-33-35-2D-33-36-2D-33-37-2D-33-38-2D-33-39-2D-34-30-2D-34-31-2D-34-32-2D-34-33-2D-34-34-2D-34-35-2D-34-36-2D-34-37-2D-34-38-2D-34-39-2D-35-30-2D-35-31-2D-35-32-2D-35-33-2D-35-34-2D-35-35-2D-35-36-2D-35-37-2D-35-38-2D-35-39-2D-36-30-2D-36-31-2D-36-32-2D-36-33-2D-36-34-2D-36-35-2D-36-36-2D-36-37-2D-36-38-2D-36-39-2D-37-30-2D-37-31-2D-37-32-2D-37-33-2D-37-34-2D-37-35-2D-37-36-2D-37-37-2D-37-38-2D-37-39-2D-38-30-2D-38-31-2D-38-32-2D-38-33-2D-38-34-2D-38-35-2D-38-36-2D-38-37-2D-38-38-2D-38-39-2D-39-30-2D-39-31-2D-39-32-2D-39-33-2D-39-34-2D-39-35-2D-39-36-2D-39-37-2D-39-38-2D-39-39-00-00", bson);
248
 1
    }
249

  
250
    [Test]
251
    public void SerializeGoogleGeoCode()
252
    {
253
 1
      string json = @"{
254
 1
  ""name"": ""1600 Amphitheatre Parkway, Mountain View, CA, USA"",
255
 1
  ""Status"": {
256
 1
    ""code"": 200,
257
 1
    ""request"": ""geocode""
258
 1
  },
259
 1
  ""Placemark"": [
260
 1
    {
261
 1
      ""address"": ""1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA"",
262
 1
      ""AddressDetails"": {
263
 1
        ""Country"": {
264
 1
          ""CountryNameCode"": ""US"",
265
 1
          ""AdministrativeArea"": {
266
 1
            ""AdministrativeAreaName"": ""CA"",
267
 1
            ""SubAdministrativeArea"": {
268
 1
              ""SubAdministrativeAreaName"": ""Santa Clara"",
269
 1
              ""Locality"": {
270
 1
                ""LocalityName"": ""Mountain View"",
271
 1
                ""Thoroughfare"": {
272
 1
                  ""ThoroughfareName"": ""1600 Amphitheatre Pkwy""
273
 1
                },
274
 1
                ""PostalCode"": {
275
 1
                  ""PostalCodeNumber"": ""94043""
276
 1
                }
277
 1
              }
278
 1
            }
279
 1
          }
280
 1
        },
281
 1
        ""Accuracy"": 8
282
 1
      },
283
 1
      ""Point"": {
284
 1
        ""coordinates"": [-122.083739, 37.423021, 0]
285
 1
      }
286
 1
    }
287
 1
  ]
288
 1
}";
289

  
290
 1
      GoogleMapGeocoderStructure jsonGoogleMapGeocoder = JsonConvert.DeserializeObject<GoogleMapGeocoderStructure>(json);
291

  
292
 1
      MemoryStream ms = new MemoryStream();
293
 1
      BsonWriter writer = new BsonWriter(ms);
294

  
295
 1
      JsonSerializer serializer = new JsonSerializer();
296
 1
      serializer.Serialize(writer, jsonGoogleMapGeocoder);
297

  
298
 1
      ms.Seek(0, SeekOrigin.Begin);
299
 1
      BsonReader reader = new BsonReader(ms);
300
 1
      GoogleMapGeocoderStructure bsonGoogleMapGeocoder = (GoogleMapGeocoderStructure)serializer.Deserialize(reader, typeof (GoogleMapGeocoderStructure));
301

  
302
 1
      Assert.IsNotNull(bsonGoogleMapGeocoder);
303
 1
      Assert.AreEqual("1600 Amphitheatre Parkway, Mountain View, CA, USA", bsonGoogleMapGeocoder.Name);
304
 1
      Assert.AreEqual("200", bsonGoogleMapGeocoder.Status.Code);
305
 1
      Assert.AreEqual("geocode", bsonGoogleMapGeocoder.Status.Request);
306

  
307
 1
      IList<Placemark> placemarks = bsonGoogleMapGeocoder.Placemark;
308
 1
      Assert.IsNotNull(placemarks);
309
 1
      Assert.AreEqual(1, placemarks.Count);
310

  
311
 1
      Placemark placemark = placemarks[0];
312
 1
      Assert.AreEqual("1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA", placemark.Address);
313
 1
      Assert.AreEqual(8, placemark.AddressDetails.Accuracy);
314
 1
      Assert.AreEqual("US", placemark.AddressDetails.Country.CountryNameCode);
315
 1
      Assert.AreEqual("CA", placemark.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName);
316
 1
      Assert.AreEqual("Santa Clara", placemark.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.SubAdministrativeAreaName);
317
 1
      Assert.AreEqual("Mountain View", placemark.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName);
318
 1
      Assert.AreEqual("1600 Amphitheatre Pkwy", placemark.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare.ThoroughfareName);
319
 1
      Assert.AreEqual("94043", placemark.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber);
320
 1
      Assert.AreEqual(-122.083739m, placemark.Point.Coordinates[0]);
321
 1
      Assert.AreEqual(37.423021m, placemark.Point.Coordinates[1]);
322
 1
      Assert.AreEqual(0m, placemark.Point.Coordinates[2]);
323
 1
    }
324

  
325
    [Test]
326
    public void WriteEmptyStrings()
327
    {
328
 1
      MemoryStream ms = new MemoryStream();
329
 1
      BsonWriter writer = new BsonWriter(ms);
330

  
331
 1
      writer.WriteStartObject();
332
 1
      writer.WritePropertyName("");
333
 1
      writer.WriteValue("");
334
 1
      writer.WriteEndObject();
335

  
336
 1
      string bson = MiscellaneousUtils.BytesToHex(ms.ToArray());
337
 1
      Assert.AreEqual("0C-00-00-00-02-00-01-00-00-00-00-00", bson);
338
 1
    }
339

  
340
    [Test]
341
    [ExpectedException(typeof(JsonWriterException), ExpectedMessage = "Cannot write JSON comment as BSON.")]
342
    public void WriteComment()
343
    {
344
 1
      MemoryStream ms = new MemoryStream();
345
 1
      BsonWriter writer = new BsonWriter(ms);
346

  
347
 1
      writer.WriteStartArray();
348
 1
      writer.WriteComment("fail");
349
 0
    }
350

  
351
    [Test]
352
    [ExpectedException(typeof(JsonWriterException), ExpectedMessage = "Cannot write JSON constructor as BSON.")]
353
    public void WriteConstructor()
354
    {
355
 1
      MemoryStream ms = new MemoryStream();
356
 1
      BsonWriter writer = new BsonWriter(ms);
357

  
358
 1
      writer.WriteStartArray();
359
 1
      writer.WriteStartConstructor("fail");
360
 0
    }
361

  
362
    [Test]
363
    [ExpectedException(typeof(JsonWriterException), ExpectedMessage = "Cannot write raw JSON as BSON.")]
364
    public void WriteRaw()
365
    {
366
 1
      MemoryStream ms = new MemoryStream();
367
 1
      BsonWriter writer = new BsonWriter(ms);
368

  
369
 1
      writer.WriteStartArray();
370
 1
      writer.WriteRaw("fail");
371
 0
    }
372

  
373
    [Test]
374
    [ExpectedException(typeof(JsonWriterException), ExpectedMessage = "Cannot write raw JSON as BSON.")]
375
    public void WriteRawValue()
376
    {
377
 1
      MemoryStream ms = new MemoryStream();
378
 1
      BsonWriter writer = new BsonWriter(ms);
379

  
380
 1
      writer.WriteStartArray();
381
 1
      writer.WriteRawValue("fail");
382
 0
    }
383

  
384
    [Test]
385
    public void Example()
386
    {
387
 1
      Product p = new Product();
388
 1
      p.ExpiryDate = DateTime.Parse("2009-04-05T14:45:00Z");
389
 1
      p.Name = "Carlos' Spicy Wieners";
390
 1
      p.Price = 9.95m;
391
 1
      p.Sizes = new[] { "Small", "Medium", "Large" };
392

  
393
 1
      MemoryStream ms = new MemoryStream();
394
 1
      JsonSerializer serializer = new JsonSerializer();
395

  
396
      // serialize product to BSON
397
 1
      BsonWriter writer = new BsonWriter(ms);
398
 1
      serializer.Serialize(writer, p);
399

  
400
 1
      Console.WriteLine(BitConverter.ToString(ms.ToArray()));
401
      // 7C-00-00-00-02-4E-61-6D-65-00-16-00-00-00-43-61-72-6C-
402
      // 6F-73-27-20-53-70-69-63-79-20-57-69-65-6E-65-72-73-00-
403
      // 09-45-78-70-69-72-79-44-61-74-65-00-E0-51-BD-76-20-01-
404
      // 00-00-01-50-72-69-63-65-00-66-66-66-66-66-E6-23-40-04-
405
      // 53-69-7A-65-73-00-2D-00-00-00-02-30-00-06-00-00-00-53-
406
      // 6D-61-6C-6C-00-02-31-00-07-00-00-00-4D-65-64-69-75-6D-
407
      // 00-02-32-00-06-00-00-00-4C-61-72-67-65-00-00-00
408

  
409

  
410
 1
      ms.Seek(0, SeekOrigin.Begin);
411

  
412
      // deserialize product from BSON
413
 1
      BsonReader reader = new BsonReader(ms);
414
 1
      Product deserializedProduct = serializer.Deserialize<Product>(reader);
415

  
416
 1
      Console.WriteLine(deserializedProduct.Name);
417
      // Carlos' Spicy Wieners
418

  
419

  
420
 1
      Assert.AreEqual("Carlos' Spicy Wieners", deserializedProduct.Name);
421
 1
      Assert.AreEqual(9.95m, deserializedProduct.Price);
422
 1
      Assert.AreEqual(3, deserializedProduct.Sizes.Length);
423
 1
    }
424

  
425
    [Test]
426
    public void WriteOid()
427
    {
428
 1
      MemoryStream ms = new MemoryStream();
429
 1
      BsonWriter writer = new BsonWriter(ms);
430

  
431
 1
      byte[] oid = new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
432

  
433
 1
      writer.WriteStartObject();
434
 1
      writer.WritePropertyName("_oid");
435
 1
      writer.WriteObjectId(oid);
436
 1
      writer.WriteEndObject();
437

  
438
 1
      string bson = MiscellaneousUtils.BytesToHex(ms.ToArray());
439
 1
      Assert.AreEqual("17-00-00-00-07-5F-6F-69-64-00-01-02-03-04-05-06-07-08-09-0A-0B-0C-00", bson);
440

  
441
 1
      ms.Seek(0, SeekOrigin.Begin);
442
 1
      BsonReader reader = new BsonReader(ms);
443

  
444
 1
      Assert.IsTrue(reader.Read());
445
 1
      Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
446

  
447
 1
      Assert.IsTrue(reader.Read());
448
 1
      Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
449

  
450
 1
      Assert.IsTrue(reader.Read());
451
 1
      Assert.AreEqual(JsonToken.Bytes, reader.TokenType);
452
 1
      Assert.AreEqual(oid, reader.Value);
453

  
454
 1
      Assert.IsTrue(reader.Read());
455
 1
      Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
456
 1
    }
457

  
458
    [Test]
459
    public void WriteOidPlusContent()
460
    {
461
 1
      MemoryStream ms = new MemoryStream();
462
 1
      BsonWriter writer = new BsonWriter(ms);
463

  
464
 1
      writer.WriteStartObject();
465
 1
      writer.WritePropertyName("_id");
466
 1
      writer.WriteObjectId(MiscellaneousUtils.HexToBytes("4ABBED9D1D8B0F0218000001"));
467
 1
      writer.WritePropertyName("test");
468
 1
      writer.WriteValue("1234£56");
469
 1
      writer.WriteEndObject();
470

  
471
 1
      byte[] expected = MiscellaneousUtils.HexToBytes("29000000075F6964004ABBED9D1D8B0F02180000010274657374000900000031323334C2A335360000");
472

  
473
 1
      Assert.AreEqual(expected, ms.ToArray());
474
 1
    }
475

  
476
    [Test]
477
    public void WriteRegexPlusContent()
478
    {
479
 1
      MemoryStream ms = new MemoryStream();
480
 1
      BsonWriter writer = new BsonWriter(ms);
481

  
482
 1
      writer.WriteStartObject();
483
 1
      writer.WritePropertyName("regex");
484
 1
      writer.WriteRegex("abc", "i");
485
 1
      writer.WritePropertyName("test");
486
 1
      writer.WriteRegex(string.Empty, null);
487
 1
      writer.WriteEndObject();
488

  
489
 1
      byte[] expected = MiscellaneousUtils.HexToBytes("1A-00-00-00-0B-72-65-67-65-78-00-61-62-63-00-69-00-0B-74-65-73-74-00-00-00-00");
490

  
491
 1
      Assert.AreEqual(expected, ms.ToArray());
492
 1
    }
493

  
494
    [Test]
495
    public void SerializeEmptyAndNullStrings()
496
    {
497
 1
      Product p = new Product();
498
 1
      p.ExpiryDate = DateTime.Parse("2009-04-05T14:45:00Z");
499
 1
      p.Name = null;
500
 1
      p.Price = 9.95m;
501
 1
      p.Sizes = new[] { "Small", "", null };
502

  
503
 1
      MemoryStream ms = new MemoryStream();
504
 1
      JsonSerializer serializer = new JsonSerializer();
505

  
506
 1
      BsonWriter writer = new BsonWriter(ms);
507
 1
      serializer.Serialize(writer, p);
508

  
509
 1
      ms.Seek(0, SeekOrigin.Begin);
510

  
511
 1
      BsonReader reader = new BsonReader(ms);
512
 1
      Product deserializedProduct = serializer.Deserialize<Product>(reader);
513

  
514
 1
      Console.WriteLine(deserializedProduct.Name);
515

  
516
 1
      Assert.AreEqual(null, deserializedProduct.Name);
517
 1
      Assert.AreEqual(9.95m, deserializedProduct.Price);
518
 1
      Assert.AreEqual(3, deserializedProduct.Sizes.Length);
519
 1
      Assert.AreEqual("Small", deserializedProduct.Sizes[0]);
520
 1
      Assert.AreEqual("", deserializedProduct.Sizes[1]);
521
 1
      Assert.AreEqual(null, deserializedProduct.Sizes[2]);
522
 1
    }
523

  
524
    [Test]
525
    public void WriteReadEmptyAndNullStrings()
526
    {
527
 1
      MemoryStream ms = new MemoryStream();
528
 1
      BsonWriter writer = new BsonWriter(ms);
529

  
530
 1
      writer.WriteStartArray();
531
 1
      writer.WriteValue("Content!");
532
 1
      writer.WriteValue("");
533
 1
      writer.WriteValue((string)null);
534
 1
      writer.WriteEndArray();
535

  
536
 1
      ms.Seek(0, SeekOrigin.Begin);
537

  
538
 1
      BsonReader reader = new BsonReader(ms);
539
 1
      reader.ReadRootValueAsArray = true;
540

  
541
 1
      Assert.IsTrue(reader.Read());
542
 1
      Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
543

  
544
 1
      Assert.IsTrue(reader.Read());
545
 1
      Assert.AreEqual(JsonToken.String, reader.TokenType);
546
 1
      Assert.AreEqual("Content!", reader.Value);
547

  
548
 1
      Assert.IsTrue(reader.Read());
549
 1
      Assert.AreEqual(JsonToken.String, reader.TokenType);
550
 1
      Assert.AreEqual("", reader.Value);
551

  
552
 1
      Assert.IsTrue(reader.Read());
553
 1
      Assert.AreEqual(JsonToken.Null, reader.TokenType);
554
 1
      Assert.AreEqual(null, reader.Value);
555

  
556
 1
      Assert.IsTrue(reader.Read());
557
 1
      Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
558

  
559
 1
      Assert.IsFalse(reader.Read());
560
 1
    }
561
  }
562
}