~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

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(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
      UpdateScopeWithFinishedValue();
 
477
      AutoComplete(JsonToken.Undefined);
 
478
      AddValue(value, BsonType.Oid);
 
479
    }
 
480
 
 
481
    /// <summary>
 
482
    /// Writes a BSON regex.
 
483
    /// </summary>
 
484
    /// <param name="pattern">The regex pattern.</param>
 
485
    /// <param name="options">The regex options.</param>
 
486
    public void WriteRegex(string pattern, string options)
 
487
    {
 
488
      ValidationUtils.ArgumentNotNull(pattern, "pattern");
 
489
 
 
490
      // hack to update the writer state
 
491
      UpdateScopeWithFinishedValue();
 
492
      AutoComplete(JsonToken.Undefined);
 
493
      AddToken(new BsonRegex(pattern, options));
 
494
    }
 
495
  }
 
496
}
 
 
b'\\ No newline at end of file'