~ubuntu-branches/ubuntu/trusty/smuxi/trusty-proposed

« back to all changes in this revision

Viewing changes to lib/Newtonsoft.Json/Src/Newtonsoft.Json/Bson/BsonWriter.cs

  • Committer: Package Import Robot
  • Author(s): Mirco Bauer
  • Date: 2013-05-25 22:11:31 UTC
  • mfrom: (1.2.12)
  • Revision ID: package-import@ubuntu.com-20130525221131-nd2mc0kzubuwyx20
Tags: 0.8.11-1
* [22d13d5] Imported Upstream version 0.8.11
* [6d2b95a] Refreshed patches
* [89eb66e] Added ServiceStack libraries to smuxi-engine package
* [848ab10] Enable Campfire engine
* [c6dbdc7] Always build db4o for predictable build result
* [13ec489] Exclude OS X specific libraries from dh_clideps

Show diffs side-by-side

added added

removed removed

Lines of Context:
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;
28
 
using System.Collections.Generic;
29
 
using System.IO;
30
 
using System.Text;
31
 
using Newtonsoft.Json.Utilities;
32
 
using Newtonsoft.Json.Linq;
33
 
using System.Globalization;
34
 
 
35
 
namespace Newtonsoft.Json.Bson
36
 
{
37
 
  /// <summary>
38
 
  /// Represents a writer that provides a fast, non-cached, forward-only way of generating Json data.
39
 
  /// </summary>
40
 
  public class BsonWriter : JsonWriter
41
 
  {
42
 
    private readonly BsonBinaryWriter _writer;
43
 
 
44
 
    private BsonToken _root;
45
 
    private BsonToken _parent;
46
 
    private string _propertyName;
47
 
 
48
 
    /// <summary>
49
 
    /// Gets or sets the <see cref="DateTimeKind" /> used when writing <see cref="DateTime"/> values to BSON.
50
 
    /// When set to <see cref="DateTimeKind.Unspecified" /> no conversion will occur.
51
 
    /// </summary>
52
 
    /// <value>The <see cref="DateTimeKind" /> used when writing <see cref="DateTime"/> values to BSON.</value>
53
 
    public DateTimeKind DateTimeKindHandling
54
 
    {
55
 
      get { return _writer.DateTimeKindHandling; }
56
 
      set { _writer.DateTimeKindHandling = value; }
57
 
    }
58
 
 
59
 
    /// <summary>
60
 
    /// Initializes a new instance of the <see cref="BsonWriter"/> class.
61
 
    /// </summary>
62
 
    /// <param name="stream">The stream.</param>
63
 
    public BsonWriter(Stream stream)
64
 
    {
65
 
      ValidationUtils.ArgumentNotNull(stream, "stream");
66
 
      _writer = new BsonBinaryWriter(stream);
67
 
    }
68
 
 
69
 
    /// <summary>
70
 
    /// Flushes whatever is in the buffer to the underlying streams and also flushes the underlying stream.
71
 
    /// </summary>
72
 
    public override void Flush()
73
 
    {
74
 
      _writer.Flush();
75
 
    }
76
 
 
77
 
    /// <summary>
78
 
    /// Writes the end.
79
 
    /// </summary>
80
 
    /// <param name="token">The token.</param>
81
 
    protected override void WriteEnd(JsonToken token)
82
 
    {
83
 
      base.WriteEnd(token);
84
 
      RemoveParent();
85
 
 
86
 
      if (Top == 0)
87
 
      {
88
 
        _writer.WriteToken(_root);
89
 
      }
90
 
    }
91
 
 
92
 
    /// <summary>
93
 
    /// Writes out a comment <code>/*...*/</code> containing the specified text.
94
 
    /// </summary>
95
 
    /// <param name="text">Text to place inside the comment.</param>
96
 
    public override void WriteComment(string text)
97
 
    {
98
 
      throw new JsonWriterException("Cannot write JSON comment as BSON.");
99
 
    }
100
 
 
101
 
    /// <summary>
102
 
    /// Writes the start of a constructor with the given name.
103
 
    /// </summary>
104
 
    /// <param name="name">The name of the constructor.</param>
105
 
    public override void WriteStartConstructor(string name)
106
 
    {
107
 
      throw new JsonWriterException("Cannot write JSON constructor as BSON.");
108
 
    }
109
 
 
110
 
    /// <summary>
111
 
    /// Writes raw JSON.
112
 
    /// </summary>
113
 
    /// <param name="json">The raw JSON to write.</param>
114
 
    public override void WriteRaw(string json)
115
 
    {
116
 
      throw new JsonWriterException("Cannot write raw JSON as BSON.");
117
 
    }
118
 
 
119
 
    /// <summary>
120
 
    /// Writes raw JSON where a value is expected and updates the writer's state.
121
 
    /// </summary>
122
 
    /// <param name="json">The raw JSON to write.</param>
123
 
    public override void WriteRawValue(string json)
124
 
    {
125
 
      throw new JsonWriterException("Cannot write raw JSON as BSON.");
126
 
    }
127
 
 
128
 
    /// <summary>
129
 
    /// Writes the beginning of a Json array.
130
 
    /// </summary>
131
 
    public override void WriteStartArray()
132
 
    {
133
 
      base.WriteStartArray();
134
 
 
135
 
      AddParent(new BsonArray());
136
 
    }
137
 
 
138
 
    /// <summary>
139
 
    /// Writes the beginning of a Json object.
140
 
    /// </summary>
141
 
    public override void WriteStartObject()
142
 
    {
143
 
      base.WriteStartObject();
144
 
 
145
 
      AddParent(new BsonObject());
146
 
    }
147
 
 
148
 
    /// <summary>
149
 
    /// Writes the property name of a name/value pair on a Json object.
150
 
    /// </summary>
151
 
    /// <param name="name">The name of the property.</param>
152
 
    public override void WritePropertyName(string name)
153
 
    {
154
 
      base.WritePropertyName(name);
155
 
 
156
 
      _propertyName = name;
157
 
    }
158
 
 
159
 
    private void AddParent(BsonToken container)
160
 
    {
161
 
      AddToken(container);
162
 
      _parent = container;
163
 
    }
164
 
 
165
 
    private void RemoveParent()
166
 
    {
167
 
      _parent = _parent.Parent;
168
 
    }
169
 
 
170
 
    private void AddValue(object value, BsonType type)
171
 
    {
172
 
      AddToken(new BsonValue(value, type));
173
 
    }
174
 
 
175
 
    internal void AddToken(BsonToken token)
176
 
    {
177
 
      if (_parent != null)
178
 
      {
179
 
        if (_parent is BsonObject)
180
 
        {
181
 
          ((BsonObject)_parent).Add(_propertyName, token);
182
 
          _propertyName = null;
183
 
        }
184
 
        else
185
 
        {
186
 
          ((BsonArray)_parent).Add(token);
187
 
        }
188
 
      }
189
 
      else
190
 
      {
191
 
        _parent = token;
192
 
        _root = token;
193
 
      }
194
 
    }
195
 
 
196
 
    #region WriteValue methods
197
 
    /// <summary>
198
 
    /// Writes a null value.
199
 
    /// </summary>
200
 
    public override void WriteNull()
201
 
    {
202
 
      base.WriteNull();
203
 
      AddValue(null, BsonType.Null);
204
 
    }
205
 
 
206
 
    /// <summary>
207
 
    /// Writes an undefined value.
208
 
    /// </summary>
209
 
    public override void WriteUndefined()
210
 
    {
211
 
      base.WriteUndefined();
212
 
      AddValue(null, BsonType.Undefined);
213
 
    }
214
 
 
215
 
    /// <summary>
216
 
    /// Writes a <see cref="String"/> value.
217
 
    /// </summary>
218
 
    /// <param name="value">The <see cref="String"/> value to write.</param>
219
 
    public override void WriteValue(string value)
220
 
    {
221
 
      base.WriteValue(value);
222
 
      if (value == null)
223
 
        AddValue(null, BsonType.Null);
224
 
      else
225
 
        AddToken(new BsonString(value, true));
226
 
    }
227
 
 
228
 
    /// <summary>
229
 
    /// Writes a <see cref="Int32"/> value.
230
 
    /// </summary>
231
 
    /// <param name="value">The <see cref="Int32"/> value to write.</param>
232
 
    public override void WriteValue(int value)
233
 
    {
234
 
      base.WriteValue(value);
235
 
      AddValue(value, BsonType.Integer);
236
 
    }
237
 
 
238
 
    /// <summary>
239
 
    /// Writes a <see cref="UInt32"/> value.
240
 
    /// </summary>
241
 
    /// <param name="value">The <see cref="UInt32"/> value to write.</param>
242
 
    [CLSCompliant(false)]
243
 
    public override void WriteValue(uint value)
244
 
    {
245
 
      if (value > int.MaxValue)
246
 
        throw new JsonWriterException("Value is too large to fit in a signed 32 bit integer. BSON does not support unsigned values.");
247
 
 
248
 
      base.WriteValue(value);
249
 
      AddValue(value, BsonType.Integer);
250
 
    }
251
 
 
252
 
    /// <summary>
253
 
    /// Writes a <see cref="Int64"/> value.
254
 
    /// </summary>
255
 
    /// <param name="value">The <see cref="Int64"/> value to write.</param>
256
 
    public override void WriteValue(long value)
257
 
    {
258
 
      base.WriteValue(value);
259
 
      AddValue(value, BsonType.Long);
260
 
    }
261
 
 
262
 
    /// <summary>
263
 
    /// Writes a <see cref="UInt64"/> value.
264
 
    /// </summary>
265
 
    /// <param name="value">The <see cref="UInt64"/> value to write.</param>
266
 
    [CLSCompliant(false)]
267
 
    public override void WriteValue(ulong value)
268
 
    {
269
 
      if (value > long.MaxValue)
270
 
        throw new JsonWriterException("Value is too large to fit in a signed 64 bit integer. BSON does not support unsigned values.");
271
 
 
272
 
      base.WriteValue(value);
273
 
      AddValue(value, BsonType.Long);
274
 
    }
275
 
 
276
 
    /// <summary>
277
 
    /// Writes a <see cref="Single"/> value.
278
 
    /// </summary>
279
 
    /// <param name="value">The <see cref="Single"/> value to write.</param>
280
 
    public override void WriteValue(float value)
281
 
    {
282
 
      base.WriteValue(value);
283
 
      AddValue(value, BsonType.Number);
284
 
    }
285
 
 
286
 
    /// <summary>
287
 
    /// Writes a <see cref="Double"/> value.
288
 
    /// </summary>
289
 
    /// <param name="value">The <see cref="Double"/> value to write.</param>
290
 
    public override void WriteValue(double value)
291
 
    {
292
 
      base.WriteValue(value);
293
 
      AddValue(value, BsonType.Number);
294
 
    }
295
 
 
296
 
    /// <summary>
297
 
    /// Writes a <see cref="Boolean"/> value.
298
 
    /// </summary>
299
 
    /// <param name="value">The <see cref="Boolean"/> value to write.</param>
300
 
    public override void WriteValue(bool value)
301
 
    {
302
 
      base.WriteValue(value);
303
 
      AddValue(value, BsonType.Boolean);
304
 
    }
305
 
 
306
 
    /// <summary>
307
 
    /// Writes a <see cref="Int16"/> value.
308
 
    /// </summary>
309
 
    /// <param name="value">The <see cref="Int16"/> value to write.</param>
310
 
    public override void WriteValue(short value)
311
 
    {
312
 
      base.WriteValue(value);
313
 
      AddValue(value, BsonType.Integer);
314
 
    }
315
 
 
316
 
    /// <summary>
317
 
    /// Writes a <see cref="UInt16"/> value.
318
 
    /// </summary>
319
 
    /// <param name="value">The <see cref="UInt16"/> value to write.</param>
320
 
    [CLSCompliant(false)]
321
 
    public override void WriteValue(ushort value)
322
 
    {
323
 
      base.WriteValue(value);
324
 
      AddValue(value, BsonType.Integer);
325
 
    }
326
 
 
327
 
    /// <summary>
328
 
    /// Writes a <see cref="Char"/> value.
329
 
    /// </summary>
330
 
    /// <param name="value">The <see cref="Char"/> value to write.</param>
331
 
    public override void WriteValue(char value)
332
 
    {
333
 
      base.WriteValue(value);
334
 
      AddToken(new BsonString(value.ToString(), true));
335
 
    }
336
 
 
337
 
    /// <summary>
338
 
    /// Writes a <see cref="Byte"/> value.
339
 
    /// </summary>
340
 
    /// <param name="value">The <see cref="Byte"/> value to write.</param>
341
 
    public override void WriteValue(byte value)
342
 
    {
343
 
      base.WriteValue(value);
344
 
      AddValue(value, BsonType.Integer);
345
 
    }
346
 
 
347
 
    /// <summary>
348
 
    /// Writes a <see cref="SByte"/> value.
349
 
    /// </summary>
350
 
    /// <param name="value">The <see cref="SByte"/> value to write.</param>
351
 
    [CLSCompliant(false)]
352
 
    public override void WriteValue(sbyte value)
353
 
    {
354
 
      base.WriteValue(value);
355
 
      AddValue(value, BsonType.Integer);
356
 
    }
357
 
 
358
 
    /// <summary>
359
 
    /// Writes a <see cref="Decimal"/> value.
360
 
    /// </summary>
361
 
    /// <param name="value">The <see cref="Decimal"/> value to write.</param>
362
 
    public override void WriteValue(decimal value)
363
 
    {
364
 
      base.WriteValue(value);
365
 
      AddValue(value, BsonType.Number);
366
 
    }
367
 
 
368
 
    /// <summary>
369
 
    /// Writes a <see cref="DateTime"/> value.
370
 
    /// </summary>
371
 
    /// <param name="value">The <see cref="DateTime"/> value to write.</param>
372
 
    public override void WriteValue(DateTime value)
373
 
    {
374
 
      base.WriteValue(value);
375
 
      AddValue(value, BsonType.Date);
376
 
    }
377
 
 
378
 
#if !PocketPC && !NET20
379
 
    /// <summary>
380
 
    /// Writes a <see cref="DateTimeOffset"/> value.
381
 
    /// </summary>
382
 
    /// <param name="value">The <see cref="DateTimeOffset"/> value to write.</param>
383
 
    public override void WriteValue(DateTimeOffset value)
384
 
    {
385
 
      base.WriteValue(value);
386
 
      AddValue(value, BsonType.Date);
387
 
    }
388
 
#endif
389
 
 
390
 
    /// <summary>
391
 
    /// Writes a <see cref="T:Byte[]"/> value.
392
 
    /// </summary>
393
 
    /// <param name="value">The <see cref="T:Byte[]"/> value to write.</param>
394
 
    public override void WriteValue(byte[] value)
395
 
    {
396
 
      base.WriteValue(value);
397
 
      AddValue(value, BsonType.Binary);
398
 
    }
399
 
    #endregion
400
 
 
401
 
    /// <summary>
402
 
    /// Writes a <see cref="T:Byte[]"/> value that represents a BSON object id.
403
 
    /// </summary>
404
 
    /// <param name="value"></param>
405
 
    public void WriteObjectId(byte[] value)
406
 
    {
407
 
      ValidationUtils.ArgumentNotNull(value, "value");
408
 
 
409
 
      if (value.Length != 12)
410
 
        throw new Exception("An object id must be 12 bytes");
411
 
 
412
 
      // hack to update the writer state
413
 
      AutoComplete(JsonToken.Undefined);
414
 
      AddValue(value, BsonType.Oid);
415
 
    }
416
 
 
417
 
    /// <summary>
418
 
    /// Writes a BSON regex.
419
 
    /// </summary>
420
 
    /// <param name="pattern">The regex pattern.</param>
421
 
    /// <param name="options">The regex options.</param>
422
 
    public void WriteRegex(string pattern, string options)
423
 
    {
424
 
      ValidationUtils.ArgumentNotNull(pattern, "pattern");
425
 
 
426
 
      // hack to update the writer state
427
 
      AutoComplete(JsonToken.Undefined);
428
 
      AddToken(new BsonRegex(pattern, options));
429
 
    }
430
 
  }
 
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;
 
28
using System.Collections.Generic;
 
29
using System.IO;
 
30
using System.Text;
 
31
using Newtonsoft.Json.Utilities;
 
32
using Newtonsoft.Json.Linq;
 
33
using System.Globalization;
 
34
 
 
35
namespace Newtonsoft.Json.Bson
 
36
{
 
37
  /// <summary>
 
38
  /// Represents a writer that provides a fast, non-cached, forward-only way of generating Json data.
 
39
  /// </summary>
 
40
  public class BsonWriter : JsonWriter
 
41
  {
 
42
    private readonly BsonBinaryWriter _writer;
 
43
 
 
44
    private BsonToken _root;
 
45
    private BsonToken _parent;
 
46
    private string _propertyName;
 
47
 
 
48
    /// <summary>
 
49
    /// Gets or sets the <see cref="DateTimeKind" /> used when writing <see cref="DateTime"/> values to BSON.
 
50
    /// When set to <see cref="DateTimeKind.Unspecified" /> no conversion will occur.
 
51
    /// </summary>
 
52
    /// <value>The <see cref="DateTimeKind" /> used when writing <see cref="DateTime"/> values to BSON.</value>
 
53
    public DateTimeKind DateTimeKindHandling
 
54
    {
 
55
      get { return _writer.DateTimeKindHandling; }
 
56
      set { _writer.DateTimeKindHandling = value; }
 
57
    }
 
58
 
 
59
    /// <summary>
 
60
    /// Initializes a new instance of the <see cref="BsonWriter"/> class.
 
61
    /// </summary>
 
62
    /// <param name="stream">The stream.</param>
 
63
    public BsonWriter(Stream stream)
 
64
    {
 
65
      ValidationUtils.ArgumentNotNull(stream, "stream");
 
66
      _writer = new BsonBinaryWriter(new BinaryWriter(stream));
 
67
    }
 
68
 
 
69
    /// <summary>
 
70
    /// Initializes a new instance of the <see cref="BsonWriter"/> class.
 
71
    /// </summary>
 
72
    /// <param name="writer">The writer.</param>
 
73
    public BsonWriter(BinaryWriter writer)
 
74
    {
 
75
      ValidationUtils.ArgumentNotNull(writer, "writer");
 
76
      _writer = new BsonBinaryWriter(writer);
 
77
    }
 
78
 
 
79
    /// <summary>
 
80
    /// Flushes whatever is in the buffer to the underlying streams and also flushes the underlying stream.
 
81
    /// </summary>
 
82
    public override void Flush()
 
83
    {
 
84
      _writer.Flush();
 
85
    }
 
86
 
 
87
    /// <summary>
 
88
    /// Writes the end.
 
89
    /// </summary>
 
90
    /// <param name="token">The token.</param>
 
91
    protected override void WriteEnd(JsonToken token)
 
92
    {
 
93
      base.WriteEnd(token);
 
94
      RemoveParent();
 
95
 
 
96
      if (Top == 0)
 
97
      {
 
98
        _writer.WriteToken(_root);
 
99
      }
 
100
    }
 
101
 
 
102
    /// <summary>
 
103
    /// Writes out a comment <code>/*...*/</code> containing the specified text.
 
104
    /// </summary>
 
105
    /// <param name="text">Text to place inside the comment.</param>
 
106
    public override void WriteComment(string text)
 
107
    {
 
108
      throw JsonWriterException.Create(this, "Cannot write JSON comment as BSON.", null);
 
109
    }
 
110
 
 
111
    /// <summary>
 
112
    /// Writes the start of a constructor with the given name.
 
113
    /// </summary>
 
114
    /// <param name="name">The name of the constructor.</param>
 
115
    public override void WriteStartConstructor(string name)
 
116
    {
 
117
      throw JsonWriterException.Create(this, "Cannot write JSON constructor as BSON.", null);
 
118
    }
 
119
 
 
120
    /// <summary>
 
121
    /// Writes raw JSON.
 
122
    /// </summary>
 
123
    /// <param name="json">The raw JSON to write.</param>
 
124
    public override void WriteRaw(string json)
 
125
    {
 
126
      throw JsonWriterException.Create(this, "Cannot write raw JSON as BSON.", null);
 
127
    }
 
128
 
 
129
    /// <summary>
 
130
    /// Writes raw JSON where a value is expected and updates the writer's state.
 
131
    /// </summary>
 
132
    /// <param name="json">The raw JSON to write.</param>
 
133
    public override void WriteRawValue(string json)
 
134
    {
 
135
      throw JsonWriterException.Create(this, "Cannot write raw JSON as BSON.", null);
 
136
    }
 
137
 
 
138
    /// <summary>
 
139
    /// Writes the beginning of a Json array.
 
140
    /// </summary>
 
141
    public override void WriteStartArray()
 
142
    {
 
143
      base.WriteStartArray();
 
144
 
 
145
      AddParent(new BsonArray());
 
146
    }
 
147
 
 
148
    /// <summary>
 
149
    /// Writes the beginning of a Json object.
 
150
    /// </summary>
 
151
    public override void WriteStartObject()
 
152
    {
 
153
      base.WriteStartObject();
 
154
 
 
155
      AddParent(new BsonObject());
 
156
    }
 
157
 
 
158
    /// <summary>
 
159
    /// Writes the property name of a name/value pair on a Json object.
 
160
    /// </summary>
 
161
    /// <param name="name">The name of the property.</param>
 
162
    public override void WritePropertyName(string name)
 
163
    {
 
164
      base.WritePropertyName(name);
 
165
 
 
166
      _propertyName = name;
 
167
    }
 
168
 
 
169
    /// <summary>
 
170
    /// Closes this stream and the underlying stream.
 
171
    /// </summary>
 
172
    public override void Close()
 
173
    {
 
174
      base.Close();
 
175
 
 
176
      if (CloseOutput && _writer != null)
 
177
        _writer.Close();
 
178
    }
 
179
 
 
180
    private void AddParent(BsonToken container)
 
181
    {
 
182
      AddToken(container);
 
183
      _parent = container;
 
184
    }
 
185
 
 
186
    private void RemoveParent()
 
187
    {
 
188
      _parent = _parent.Parent;
 
189
    }
 
190
 
 
191
    private void AddValue(object value, BsonType type)
 
192
    {
 
193
      AddToken(new BsonValue(value, type));
 
194
    }
 
195
 
 
196
    internal void AddToken(BsonToken token)
 
197
    {
 
198
      if (_parent != null)
 
199
      {
 
200
        if (_parent is BsonObject)
 
201
        {
 
202
          ((BsonObject) _parent).Add(_propertyName, token);
 
203
          _propertyName = null;
 
204
        }
 
205
        else
 
206
        {
 
207
          ((BsonArray) _parent).Add(token);
 
208
        }
 
209
      }
 
210
      else
 
211
      {
 
212
        if (token.Type != BsonType.Object && token.Type != BsonType.Array)
 
213
          throw JsonWriterException.Create(this, "Error writing {0} value. BSON must start with an Object or Array.".FormatWith(CultureInfo.InvariantCulture, token.Type), null);
 
214
 
 
215
        _parent = token;
 
216
        _root = token;
 
217
      }
 
218
    }
 
219
 
 
220
    #region WriteValue methods
 
221
 
 
222
    /// <summary>
 
223
    /// Writes a null value.
 
224
    /// </summary>
 
225
    public override void WriteNull()
 
226
    {
 
227
      base.WriteNull();
 
228
      AddValue(null, BsonType.Null);
 
229
    }
 
230
 
 
231
    /// <summary>
 
232
    /// Writes an undefined value.
 
233
    /// </summary>
 
234
    public override void WriteUndefined()
 
235
    {
 
236
      base.WriteUndefined();
 
237
      AddValue(null, BsonType.Undefined);
 
238
    }
 
239
 
 
240
    /// <summary>
 
241
    /// Writes a <see cref="String"/> value.
 
242
    /// </summary>
 
243
    /// <param name="value">The <see cref="String"/> value to write.</param>
 
244
    public override void WriteValue(string value)
 
245
    {
 
246
      base.WriteValue(value);
 
247
      if (value == null)
 
248
        AddValue(null, BsonType.Null);
 
249
      else
 
250
        AddToken(new BsonString(value, true));
 
251
    }
 
252
 
 
253
    /// <summary>
 
254
    /// Writes a <see cref="Int32"/> value.
 
255
    /// </summary>
 
256
    /// <param name="value">The <see cref="Int32"/> value to write.</param>
 
257
    public override void WriteValue(int value)
 
258
    {
 
259
      base.WriteValue(value);
 
260
      AddValue(value, BsonType.Integer);
 
261
    }
 
262
 
 
263
    /// <summary>
 
264
    /// Writes a <see cref="UInt32"/> value.
 
265
    /// </summary>
 
266
    /// <param name="value">The <see cref="UInt32"/> value to write.</param>
 
267
    [CLSCompliant(false)]
 
268
    public override void WriteValue(uint value)
 
269
    {
 
270
      if (value > int.MaxValue)
 
271
        throw JsonWriterException.Create(this, "Value is too large to fit in a signed 32 bit integer. BSON does not support unsigned values.", null);
 
272
 
 
273
      base.WriteValue(value);
 
274
      AddValue(value, BsonType.Integer);
 
275
    }
 
276
 
 
277
    /// <summary>
 
278
    /// Writes a <see cref="Int64"/> value.
 
279
    /// </summary>
 
280
    /// <param name="value">The <see cref="Int64"/> value to write.</param>
 
281
    public override void WriteValue(long value)
 
282
    {
 
283
      base.WriteValue(value);
 
284
      AddValue(value, BsonType.Long);
 
285
    }
 
286
 
 
287
    /// <summary>
 
288
    /// Writes a <see cref="UInt64"/> value.
 
289
    /// </summary>
 
290
    /// <param name="value">The <see cref="UInt64"/> value to write.</param>
 
291
    [CLSCompliant(false)]
 
292
    public override void WriteValue(ulong value)
 
293
    {
 
294
      if (value > long.MaxValue)
 
295
        throw JsonWriterException.Create(this, "Value is too large to fit in a signed 64 bit integer. BSON does not support unsigned values.", null);
 
296
 
 
297
      base.WriteValue(value);
 
298
      AddValue(value, BsonType.Long);
 
299
    }
 
300
 
 
301
    /// <summary>
 
302
    /// Writes a <see cref="Single"/> value.
 
303
    /// </summary>
 
304
    /// <param name="value">The <see cref="Single"/> value to write.</param>
 
305
    public override void WriteValue(float value)
 
306
    {
 
307
      base.WriteValue(value);
 
308
      AddValue(value, BsonType.Number);
 
309
    }
 
310
 
 
311
    /// <summary>
 
312
    /// Writes a <see cref="Double"/> value.
 
313
    /// </summary>
 
314
    /// <param name="value">The <see cref="Double"/> value to write.</param>
 
315
    public override void WriteValue(double value)
 
316
    {
 
317
      base.WriteValue(value);
 
318
      AddValue(value, BsonType.Number);
 
319
    }
 
320
 
 
321
    /// <summary>
 
322
    /// Writes a <see cref="Boolean"/> value.
 
323
    /// </summary>
 
324
    /// <param name="value">The <see cref="Boolean"/> value to write.</param>
 
325
    public override void WriteValue(bool value)
 
326
    {
 
327
      base.WriteValue(value);
 
328
      AddValue(value, BsonType.Boolean);
 
329
    }
 
330
 
 
331
    /// <summary>
 
332
    /// Writes a <see cref="Int16"/> value.
 
333
    /// </summary>
 
334
    /// <param name="value">The <see cref="Int16"/> value to write.</param>
 
335
    public override void WriteValue(short value)
 
336
    {
 
337
      base.WriteValue(value);
 
338
      AddValue(value, BsonType.Integer);
 
339
    }
 
340
 
 
341
    /// <summary>
 
342
    /// Writes a <see cref="UInt16"/> value.
 
343
    /// </summary>
 
344
    /// <param name="value">The <see cref="UInt16"/> value to write.</param>
 
345
    [CLSCompliant(false)]
 
346
    public override void WriteValue(ushort value)
 
347
    {
 
348
      base.WriteValue(value);
 
349
      AddValue(value, BsonType.Integer);
 
350
    }
 
351
 
 
352
    /// <summary>
 
353
    /// Writes a <see cref="Char"/> value.
 
354
    /// </summary>
 
355
    /// <param name="value">The <see cref="Char"/> value to write.</param>
 
356
    public override void WriteValue(char value)
 
357
    {
 
358
      base.WriteValue(value);
 
359
      string s = null;
 
360
#if !(NETFX_CORE || PORTABLE)
 
361
      s = value.ToString(CultureInfo.InvariantCulture);
 
362
#else
 
363
      s = value.ToString();
 
364
#endif
 
365
      AddToken(new BsonString(s, true));
 
366
    }
 
367
 
 
368
    /// <summary>
 
369
    /// Writes a <see cref="Byte"/> value.
 
370
    /// </summary>
 
371
    /// <param name="value">The <see cref="Byte"/> value to write.</param>
 
372
    public override void WriteValue(byte value)
 
373
    {
 
374
      base.WriteValue(value);
 
375
      AddValue(value, BsonType.Integer);
 
376
    }
 
377
 
 
378
    /// <summary>
 
379
    /// Writes a <see cref="SByte"/> value.
 
380
    /// </summary>
 
381
    /// <param name="value">The <see cref="SByte"/> value to write.</param>
 
382
    [CLSCompliant(false)]
 
383
    public override void WriteValue(sbyte value)
 
384
    {
 
385
      base.WriteValue(value);
 
386
      AddValue(value, BsonType.Integer);
 
387
    }
 
388
 
 
389
    /// <summary>
 
390
    /// Writes a <see cref="Decimal"/> value.
 
391
    /// </summary>
 
392
    /// <param name="value">The <see cref="Decimal"/> value to write.</param>
 
393
    public override void WriteValue(decimal value)
 
394
    {
 
395
      base.WriteValue(value);
 
396
      AddValue(value, BsonType.Number);
 
397
    }
 
398
 
 
399
    /// <summary>
 
400
    /// Writes a <see cref="DateTime"/> value.
 
401
    /// </summary>
 
402
    /// <param name="value">The <see cref="DateTime"/> value to write.</param>
 
403
    public override void WriteValue(DateTime value)
 
404
    {
 
405
      base.WriteValue(value);
 
406
      value = JsonConvert.EnsureDateTime(value, DateTimeZoneHandling);
 
407
      AddValue(value, BsonType.Date);
 
408
    }
 
409
 
 
410
#if !PocketPC && !NET20
 
411
    /// <summary>
 
412
    /// Writes a <see cref="DateTimeOffset"/> value.
 
413
    /// </summary>
 
414
    /// <param name="value">The <see cref="DateTimeOffset"/> value to write.</param>
 
415
    public override void WriteValue(DateTimeOffset value)
 
416
    {
 
417
      base.WriteValue(value);
 
418
      AddValue(value, BsonType.Date);
 
419
    }
 
420
#endif
 
421
 
 
422
    /// <summary>
 
423
    /// Writes a <see cref="T:Byte[]"/> value.
 
424
    /// </summary>
 
425
    /// <param name="value">The <see cref="T:Byte[]"/> value to write.</param>
 
426
    public override void WriteValue(byte[] value)
 
427
    {
 
428
      base.WriteValue(value);
 
429
      AddValue(value, BsonType.Binary);
 
430
    }
 
431
 
 
432
    /// <summary>
 
433
    /// Writes a <see cref="Guid"/> value.
 
434
    /// </summary>
 
435
    /// <param name="value">The <see cref="Guid"/> value to write.</param>
 
436
    public override void WriteValue(Guid value)
 
437
    {
 
438
      base.WriteValue(value);
 
439
      AddToken(new BsonString(value.ToString(), true));
 
440
    }
 
441
 
 
442
    /// <summary>
 
443
    /// Writes a <see cref="TimeSpan"/> value.
 
444
    /// </summary>
 
445
    /// <param name="value">The <see cref="TimeSpan"/> value to write.</param>
 
446
    public override void WriteValue(TimeSpan value)
 
447
    {
 
448
      base.WriteValue(value);
 
449
      AddToken(new BsonString(value.ToString(), true));
 
450
    }
 
451
 
 
452
    /// <summary>
 
453
    /// Writes a <see cref="Uri"/> value.
 
454
    /// </summary>
 
455
    /// <param name="value">The <see cref="Uri"/> value to write.</param>
 
456
    public override void WriteValue(Uri value)
 
457
    {
 
458
      base.WriteValue(value);
 
459
      AddToken(new BsonString(value.ToString(), true));
 
460
    }
 
461
 
 
462
    #endregion
 
463
 
 
464
    /// <summary>
 
465
    /// Writes a <see cref="T:Byte[]"/> value that represents a BSON object id.
 
466
    /// </summary>
 
467
    /// <param name="value">The Object ID value to write.</param>
 
468
    public void WriteObjectId(byte[] value)
 
469
    {
 
470
      ValidationUtils.ArgumentNotNull(value, "value");
 
471
 
 
472
      if (value.Length != 12)
 
473
        throw JsonWriterException.Create(this, "An object id must be 12 bytes", null);
 
474
 
 
475
      // hack to update the writer state
 
476
      AutoComplete(JsonToken.Undefined);
 
477
      AddValue(value, BsonType.Oid);
 
478
    }
 
479
 
 
480
    /// <summary>
 
481
    /// Writes a BSON regex.
 
482
    /// </summary>
 
483
    /// <param name="pattern">The regex pattern.</param>
 
484
    /// <param name="options">The regex options.</param>
 
485
    public void WriteRegex(string pattern, string options)
 
486
    {
 
487
      ValidationUtils.ArgumentNotNull(pattern, "pattern");
 
488
 
 
489
      // hack to update the writer state
 
490
      AutoComplete(JsonToken.Undefined);
 
491
      AddToken(new BsonRegex(pattern, options));
 
492
    }
 
493
  }
431
494
}
 
 
b'\\ No newline at end of file'