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

« back to all changes in this revision

Viewing changes to lib/Newtonsoft.Json/Src/Newtonsoft.Json/JsonTextWriter.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.Generic;
28
 
using System.Text;
29
 
using System.IO;
30
 
using System.Xml;
31
 
using Newtonsoft.Json.Utilities;
32
 
 
33
 
namespace Newtonsoft.Json
34
 
{
35
 
  /// <summary>
36
 
  /// Represents a writer that provides a fast, non-cached, forward-only way of generating Json data.
37
 
  /// </summary>
38
 
  public class JsonTextWriter : JsonWriter
39
 
  {
40
 
    private readonly TextWriter _writer;
41
 
    private Base64Encoder _base64Encoder;
42
 
    private char _indentChar;
43
 
    private int _indentation;
44
 
    private char _quoteChar;
45
 
    private bool _quoteName;
46
 
 
47
 
    private Base64Encoder Base64Encoder
48
 
    {
49
 
      get
50
 
      {
51
 
        if (_base64Encoder == null)
52
 
          _base64Encoder = new Base64Encoder(_writer);
53
 
 
54
 
        return _base64Encoder;
55
 
      }
56
 
    }
57
 
 
58
 
    /// <summary>
59
 
    /// Gets or sets how many IndentChars to write for each level in the hierarchy when <paramref name="Formatting"/> is set to <c>Formatting.Indented</c>.
60
 
    /// </summary>
61
 
    public int Indentation
62
 
    {
63
 
      get { return _indentation; }
64
 
      set
65
 
      {
66
 
        if (value < 0)
67
 
          throw new ArgumentException("Indentation value must be greater than 0.");
68
 
 
69
 
        _indentation = value;
70
 
      }
71
 
    }
72
 
 
73
 
    /// <summary>
74
 
    /// Gets or sets which character to use to quote attribute values.
75
 
    /// </summary>
76
 
    public char QuoteChar
77
 
    {
78
 
      get { return _quoteChar; }
79
 
      set
80
 
      {
81
 
        if (value != '"' && value != '\'')
82
 
          throw new ArgumentException(@"Invalid JavaScript string quote character. Valid quote characters are ' and "".");
83
 
 
84
 
        _quoteChar = value;
85
 
      }
86
 
    }
87
 
 
88
 
    /// <summary>
89
 
    /// Gets or sets which character to use for indenting when <paramref name="Formatting"/> is set to <c>Formatting.Indented</c>.
90
 
    /// </summary>
91
 
    public char IndentChar
92
 
    {
93
 
      get { return _indentChar; }
94
 
      set { _indentChar = value; }
95
 
    }
96
 
 
97
 
    /// <summary>
98
 
    /// Gets or sets a value indicating whether object names will be surrounded with quotes.
99
 
    /// </summary>
100
 
    public bool QuoteName
101
 
    {
102
 
      get { return _quoteName; }
103
 
      set { _quoteName = value; }
104
 
    }
105
 
 
106
 
    /// <summary>
107
 
    /// Creates an instance of the <c>JsonWriter</c> class using the specified <see cref="TextWriter"/>. 
108
 
    /// </summary>
109
 
    /// <param name="textWriter">The <c>TextWriter</c> to write to.</param>
110
 
    public JsonTextWriter(TextWriter textWriter)
111
 
    {
112
 
      if (textWriter == null)
113
 
        throw new ArgumentNullException("textWriter");
114
 
 
115
 
      _writer = textWriter;
116
 
      _quoteChar = '"';
117
 
      _quoteName = true;
118
 
      _indentChar = ' ';
119
 
      _indentation = 2;
120
 
    }
121
 
 
122
 
    /// <summary>
123
 
    /// Flushes whatever is in the buffer to the underlying streams and also flushes the underlying stream.
124
 
    /// </summary>
125
 
    public override void Flush()
126
 
    {
127
 
      _writer.Flush();
128
 
    }
129
 
 
130
 
    /// <summary>
131
 
    /// Closes this stream and the underlying stream.
132
 
    /// </summary>
133
 
    public override void Close()
134
 
    {
135
 
      base.Close();
136
 
 
137
 
      _writer.Close();
138
 
    }
139
 
 
140
 
    /// <summary>
141
 
    /// Writes the beginning of a Json object.
142
 
    /// </summary>
143
 
    public override void WriteStartObject()
144
 
    {
145
 
      base.WriteStartObject();
146
 
 
147
 
      _writer.Write("{");
148
 
    }
149
 
 
150
 
    /// <summary>
151
 
    /// Writes the beginning of a Json array.
152
 
    /// </summary>
153
 
    public override void WriteStartArray()
154
 
    {
155
 
      base.WriteStartArray();
156
 
 
157
 
      _writer.Write("[");
158
 
    }
159
 
 
160
 
    /// <summary>
161
 
    /// Writes the start of a constructor with the given name.
162
 
    /// </summary>
163
 
    /// <param name="name">The name of the constructor.</param>
164
 
    public override void WriteStartConstructor(string name)
165
 
    {
166
 
      base.WriteStartConstructor(name);
167
 
 
168
 
      _writer.Write("new ");
169
 
      _writer.Write(name);
170
 
      _writer.Write("(");
171
 
    }
172
 
 
173
 
    /// <summary>
174
 
    /// Writes the specified end token.
175
 
    /// </summary>
176
 
    /// <param name="token">The end token to write.</param>
177
 
    protected override void WriteEnd(JsonToken token)
178
 
    {
179
 
      switch (token)
180
 
      {
181
 
        case JsonToken.EndObject:
182
 
          _writer.Write("}");
183
 
          break;
184
 
        case JsonToken.EndArray:
185
 
          _writer.Write("]");
186
 
          break;
187
 
        case JsonToken.EndConstructor:
188
 
          _writer.Write(")");
189
 
          break;
190
 
        default:
191
 
          throw new JsonWriterException("Invalid JsonToken: " + token);
192
 
      }
193
 
    }
194
 
 
195
 
    /// <summary>
196
 
    /// Writes the property name of a name/value pair on a Json object.
197
 
    /// </summary>
198
 
    /// <param name="name">The name of the property.</param>
199
 
    public override void WritePropertyName(string name)
200
 
    {
201
 
      base.WritePropertyName(name);
202
 
 
203
 
      JavaScriptUtils.WriteEscapedJavaScriptString(_writer, name, _quoteChar, _quoteName);
204
 
 
205
 
      _writer.Write(':');
206
 
    }
207
 
 
208
 
    /// <summary>
209
 
    /// Writes indent characters.
210
 
    /// </summary>
211
 
    protected override void WriteIndent()
212
 
    {
213
 
      if (Formatting == Formatting.Indented)
214
 
      {
215
 
        _writer.Write(Environment.NewLine);
216
 
 
217
 
        // levels of indentation multiplied by the indent count
218
 
        int currentIndentCount = Top * _indentation;
219
 
 
220
 
        for (int i = 0; i < currentIndentCount; i++)
221
 
        {
222
 
          _writer.Write(_indentChar);
223
 
        }
224
 
      }
225
 
    }
226
 
 
227
 
    /// <summary>
228
 
    /// Writes the JSON value delimiter.
229
 
    /// </summary>
230
 
    protected override void WriteValueDelimiter()
231
 
    {
232
 
      _writer.Write(',');
233
 
    }
234
 
 
235
 
    /// <summary>
236
 
    /// Writes an indent space.
237
 
    /// </summary>
238
 
    protected override void WriteIndentSpace()
239
 
    {
240
 
      _writer.Write(' ');
241
 
    }
242
 
 
243
 
    private void WriteValueInternal(string value, JsonToken token)
244
 
    {
245
 
      _writer.Write(value);
246
 
    }
247
 
 
248
 
    #region WriteValue methods
249
 
    /// <summary>
250
 
    /// Writes a null value.
251
 
    /// </summary>
252
 
    public override void WriteNull()
253
 
    {
254
 
      base.WriteNull();
255
 
      WriteValueInternal(JsonConvert.Null, JsonToken.Null);
256
 
    }
257
 
 
258
 
    /// <summary>
259
 
    /// Writes an undefined value.
260
 
    /// </summary>
261
 
    public override void WriteUndefined()
262
 
    {
263
 
      base.WriteUndefined();
264
 
      WriteValueInternal(JsonConvert.Undefined, JsonToken.Undefined);
265
 
    }
266
 
 
267
 
    /// <summary>
268
 
    /// Writes raw JSON.
269
 
    /// </summary>
270
 
    /// <param name="json">The raw JSON to write.</param>
271
 
    public override void WriteRaw(string json)
272
 
    {
273
 
      base.WriteRaw(json);
274
 
 
275
 
      _writer.Write(json);
276
 
    }
277
 
 
278
 
    /// <summary>
279
 
    /// Writes a <see cref="String"/> value.
280
 
    /// </summary>
281
 
    /// <param name="value">The <see cref="String"/> value to write.</param>
282
 
    public override void WriteValue(string value)
283
 
    {
284
 
      base.WriteValue(value);
285
 
      if (value == null)
286
 
        WriteValueInternal(JsonConvert.Null, JsonToken.Null);
287
 
      else
288
 
        JavaScriptUtils.WriteEscapedJavaScriptString(_writer, value, _quoteChar, true);
289
 
    }
290
 
 
291
 
    /// <summary>
292
 
    /// Writes a <see cref="Int32"/> value.
293
 
    /// </summary>
294
 
    /// <param name="value">The <see cref="Int32"/> value to write.</param>
295
 
    public override void WriteValue(int value)
296
 
    {
297
 
      base.WriteValue(value);
298
 
      WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
299
 
    }
300
 
 
301
 
    /// <summary>
302
 
    /// Writes a <see cref="UInt32"/> value.
303
 
    /// </summary>
304
 
    /// <param name="value">The <see cref="UInt32"/> value to write.</param>
305
 
    [CLSCompliant(false)]
306
 
    public override void WriteValue(uint value)
307
 
    {
308
 
      base.WriteValue(value);
309
 
      WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
310
 
    }
311
 
 
312
 
    /// <summary>
313
 
    /// Writes a <see cref="Int64"/> value.
314
 
    /// </summary>
315
 
    /// <param name="value">The <see cref="Int64"/> value to write.</param>
316
 
    public override void WriteValue(long value)
317
 
    {
318
 
      base.WriteValue(value);
319
 
      WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
320
 
    }
321
 
 
322
 
    /// <summary>
323
 
    /// Writes a <see cref="UInt64"/> value.
324
 
    /// </summary>
325
 
    /// <param name="value">The <see cref="UInt64"/> value to write.</param>
326
 
    [CLSCompliant(false)]
327
 
    public override void WriteValue(ulong value)
328
 
    {
329
 
      base.WriteValue(value);
330
 
      WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
331
 
    }
332
 
 
333
 
    /// <summary>
334
 
    /// Writes a <see cref="Single"/> value.
335
 
    /// </summary>
336
 
    /// <param name="value">The <see cref="Single"/> value to write.</param>
337
 
    public override void WriteValue(float value)
338
 
    {
339
 
      base.WriteValue(value);
340
 
      WriteValueInternal(JsonConvert.ToString(value), JsonToken.Float);
341
 
    }
342
 
 
343
 
    /// <summary>
344
 
    /// Writes a <see cref="Double"/> value.
345
 
    /// </summary>
346
 
    /// <param name="value">The <see cref="Double"/> value to write.</param>
347
 
    public override void WriteValue(double value)
348
 
    {
349
 
      base.WriteValue(value);
350
 
      WriteValueInternal(JsonConvert.ToString(value), JsonToken.Float);
351
 
    }
352
 
 
353
 
    /// <summary>
354
 
    /// Writes a <see cref="Boolean"/> value.
355
 
    /// </summary>
356
 
    /// <param name="value">The <see cref="Boolean"/> value to write.</param>
357
 
    public override void WriteValue(bool value)
358
 
    {
359
 
      base.WriteValue(value);
360
 
      WriteValueInternal(JsonConvert.ToString(value), JsonToken.Boolean);
361
 
    }
362
 
 
363
 
    /// <summary>
364
 
    /// Writes a <see cref="Int16"/> value.
365
 
    /// </summary>
366
 
    /// <param name="value">The <see cref="Int16"/> value to write.</param>
367
 
    public override void WriteValue(short value)
368
 
    {
369
 
      base.WriteValue(value);
370
 
      WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
371
 
    }
372
 
 
373
 
    /// <summary>
374
 
    /// Writes a <see cref="UInt16"/> value.
375
 
    /// </summary>
376
 
    /// <param name="value">The <see cref="UInt16"/> value to write.</param>
377
 
    [CLSCompliant(false)]
378
 
    public override void WriteValue(ushort value)
379
 
    {
380
 
      base.WriteValue(value);
381
 
      WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
382
 
    }
383
 
 
384
 
    /// <summary>
385
 
    /// Writes a <see cref="Char"/> value.
386
 
    /// </summary>
387
 
    /// <param name="value">The <see cref="Char"/> value to write.</param>
388
 
    public override void WriteValue(char value)
389
 
    {
390
 
      base.WriteValue(value);
391
 
      WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
392
 
    }
393
 
 
394
 
    /// <summary>
395
 
    /// Writes a <see cref="Byte"/> value.
396
 
    /// </summary>
397
 
    /// <param name="value">The <see cref="Byte"/> value to write.</param>
398
 
    public override void WriteValue(byte value)
399
 
    {
400
 
      base.WriteValue(value);
401
 
      WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
402
 
    }
403
 
 
404
 
    /// <summary>
405
 
    /// Writes a <see cref="SByte"/> value.
406
 
    /// </summary>
407
 
    /// <param name="value">The <see cref="SByte"/> value to write.</param>
408
 
    [CLSCompliant(false)]
409
 
    public override void WriteValue(sbyte value)
410
 
    {
411
 
      base.WriteValue(value);
412
 
      WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
413
 
    }
414
 
 
415
 
    /// <summary>
416
 
    /// Writes a <see cref="Decimal"/> value.
417
 
    /// </summary>
418
 
    /// <param name="value">The <see cref="Decimal"/> value to write.</param>
419
 
    public override void WriteValue(decimal value)
420
 
    {
421
 
      base.WriteValue(value);
422
 
      WriteValueInternal(JsonConvert.ToString(value), JsonToken.Float);
423
 
    }
424
 
 
425
 
    /// <summary>
426
 
    /// Writes a <see cref="DateTime"/> value.
427
 
    /// </summary>
428
 
    /// <param name="value">The <see cref="DateTime"/> value to write.</param>
429
 
    public override void WriteValue(DateTime value)
430
 
    {
431
 
      base.WriteValue(value);
432
 
      JsonConvert.WriteDateTimeString(_writer, value);
433
 
    }
434
 
 
435
 
    /// <summary>
436
 
    /// Writes a <see cref="T:Byte[]"/> value.
437
 
    /// </summary>
438
 
    /// <param name="value">The <see cref="T:Byte[]"/> value to write.</param>
439
 
    public override void WriteValue(byte[] value)
440
 
    {
441
 
      base.WriteValue(value);
442
 
 
443
 
      if (value != null)
444
 
      {
445
 
        _writer.Write(_quoteChar);
446
 
        Base64Encoder.Encode(value, 0, value.Length);
447
 
        Base64Encoder.Flush();
448
 
        _writer.Write(_quoteChar);
449
 
      }
450
 
    }
451
 
 
452
 
#if !PocketPC && !NET20
453
 
    /// <summary>
454
 
    /// Writes a <see cref="DateTimeOffset"/> value.
455
 
    /// </summary>
456
 
    /// <param name="value">The <see cref="DateTimeOffset"/> value to write.</param>
457
 
    public override void WriteValue(DateTimeOffset value)
458
 
    {
459
 
      base.WriteValue(value);
460
 
      WriteValueInternal(JsonConvert.ToString(value), JsonToken.Date);
461
 
    }
462
 
#endif
463
 
    #endregion
464
 
 
465
 
    /// <summary>
466
 
    /// Writes out a comment <code>/*...*/</code> containing the specified text. 
467
 
    /// </summary>
468
 
    /// <param name="text">Text to place inside the comment.</param>
469
 
    public override void WriteComment(string text)
470
 
    {
471
 
      base.WriteComment(text);
472
 
 
473
 
      _writer.Write("/*");
474
 
      _writer.Write(text);
475
 
      _writer.Write("*/");
476
 
    }
477
 
 
478
 
    /// <summary>
479
 
    /// Writes out the given white space.
480
 
    /// </summary>
481
 
    /// <param name="ws">The string of white space characters.</param>
482
 
    public override void WriteWhitespace(string ws)
483
 
    {
484
 
      base.WriteWhitespace(ws);
485
 
 
486
 
      _writer.Write(ws);
487
 
    }
488
 
  }
 
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.Globalization;
 
29
using System.Text;
 
30
using System.IO;
 
31
using System.Xml;
 
32
using Newtonsoft.Json.Utilities;
 
33
 
 
34
namespace Newtonsoft.Json
 
35
{
 
36
  /// <summary>
 
37
  /// Represents a writer that provides a fast, non-cached, forward-only way of generating Json data.
 
38
  /// </summary>
 
39
  public class JsonTextWriter : JsonWriter
 
40
  {
 
41
    private readonly TextWriter _writer;
 
42
    private Base64Encoder _base64Encoder;
 
43
    private char _indentChar;
 
44
    private int _indentation;
 
45
    private char _quoteChar;
 
46
    private bool _quoteName;
 
47
 
 
48
    private Base64Encoder Base64Encoder
 
49
    {
 
50
      get
 
51
      {
 
52
        if (_base64Encoder == null)
 
53
          _base64Encoder = new Base64Encoder(_writer);
 
54
 
 
55
        return _base64Encoder;
 
56
      }
 
57
    }
 
58
 
 
59
    /// <summary>
 
60
    /// Gets or sets how many IndentChars to write for each level in the hierarchy when <see cref="Formatting"/> is set to <c>Formatting.Indented</c>.
 
61
    /// </summary>
 
62
    public int Indentation
 
63
    {
 
64
      get { return _indentation; }
 
65
      set
 
66
      {
 
67
        if (value < 0)
 
68
          throw new ArgumentException("Indentation value must be greater than 0.");
 
69
 
 
70
        _indentation = value;
 
71
      }
 
72
    }
 
73
 
 
74
    /// <summary>
 
75
    /// Gets or sets which character to use to quote attribute values.
 
76
    /// </summary>
 
77
    public char QuoteChar
 
78
    {
 
79
      get { return _quoteChar; }
 
80
      set
 
81
      {
 
82
        if (value != '"' && value != '\'')
 
83
          throw new ArgumentException(@"Invalid JavaScript string quote character. Valid quote characters are ' and "".");
 
84
 
 
85
        _quoteChar = value;
 
86
      }
 
87
    }
 
88
 
 
89
    /// <summary>
 
90
    /// Gets or sets which character to use for indenting when <see cref="Formatting"/> is set to <c>Formatting.Indented</c>.
 
91
    /// </summary>
 
92
    public char IndentChar
 
93
    {
 
94
      get { return _indentChar; }
 
95
      set { _indentChar = value; }
 
96
    }
 
97
 
 
98
    /// <summary>
 
99
    /// Gets or sets a value indicating whether object names will be surrounded with quotes.
 
100
    /// </summary>
 
101
    public bool QuoteName
 
102
    {
 
103
      get { return _quoteName; }
 
104
      set { _quoteName = value; }
 
105
    }
 
106
 
 
107
    /// <summary>
 
108
    /// Creates an instance of the <c>JsonWriter</c> class using the specified <see cref="TextWriter"/>. 
 
109
    /// </summary>
 
110
    /// <param name="textWriter">The <c>TextWriter</c> to write to.</param>
 
111
    public JsonTextWriter(TextWriter textWriter)
 
112
    {
 
113
      if (textWriter == null)
 
114
        throw new ArgumentNullException("textWriter");
 
115
 
 
116
      _writer = textWriter;
 
117
      _quoteChar = '"';
 
118
      _quoteName = true;
 
119
      _indentChar = ' ';
 
120
      _indentation = 2;
 
121
    }
 
122
 
 
123
    /// <summary>
 
124
    /// Flushes whatever is in the buffer to the underlying streams and also flushes the underlying stream.
 
125
    /// </summary>
 
126
    public override void Flush()
 
127
    {
 
128
      _writer.Flush();
 
129
    }
 
130
 
 
131
    /// <summary>
 
132
    /// Closes this stream and the underlying stream.
 
133
    /// </summary>
 
134
    public override void Close()
 
135
    {
 
136
      base.Close();
 
137
 
 
138
      if (CloseOutput && _writer != null)
 
139
#if !(NETFX_CORE || PORTABLE)
 
140
        _writer.Close();
 
141
#else
 
142
        _writer.Dispose();
 
143
#endif
 
144
    }
 
145
 
 
146
    /// <summary>
 
147
    /// Writes the beginning of a Json object.
 
148
    /// </summary>
 
149
    public override void WriteStartObject()
 
150
    {
 
151
      base.WriteStartObject();
 
152
 
 
153
      _writer.Write("{");
 
154
    }
 
155
 
 
156
    /// <summary>
 
157
    /// Writes the beginning of a Json array.
 
158
    /// </summary>
 
159
    public override void WriteStartArray()
 
160
    {
 
161
      base.WriteStartArray();
 
162
 
 
163
      _writer.Write("[");
 
164
    }
 
165
 
 
166
    /// <summary>
 
167
    /// Writes the start of a constructor with the given name.
 
168
    /// </summary>
 
169
    /// <param name="name">The name of the constructor.</param>
 
170
    public override void WriteStartConstructor(string name)
 
171
    {
 
172
      base.WriteStartConstructor(name);
 
173
 
 
174
      _writer.Write("new ");
 
175
      _writer.Write(name);
 
176
      _writer.Write("(");
 
177
    }
 
178
 
 
179
    /// <summary>
 
180
    /// Writes the specified end token.
 
181
    /// </summary>
 
182
    /// <param name="token">The end token to write.</param>
 
183
    protected override void WriteEnd(JsonToken token)
 
184
    {
 
185
      switch (token)
 
186
      {
 
187
        case JsonToken.EndObject:
 
188
          _writer.Write("}");
 
189
          break;
 
190
        case JsonToken.EndArray:
 
191
          _writer.Write("]");
 
192
          break;
 
193
        case JsonToken.EndConstructor:
 
194
          _writer.Write(")");
 
195
          break;
 
196
        default:
 
197
          throw JsonWriterException.Create(this, "Invalid JsonToken: " + token, null);
 
198
      }
 
199
    }
 
200
 
 
201
    /// <summary>
 
202
    /// Writes the property name of a name/value pair on a Json object.
 
203
    /// </summary>
 
204
    /// <param name="name">The name of the property.</param>
 
205
    public override void WritePropertyName(string name)
 
206
    {
 
207
      base.WritePropertyName(name);
 
208
 
 
209
      JavaScriptUtils.WriteEscapedJavaScriptString(_writer, name, _quoteChar, _quoteName);
 
210
 
 
211
      _writer.Write(':');
 
212
    }
 
213
 
 
214
    /// <summary>
 
215
    /// Writes indent characters.
 
216
    /// </summary>
 
217
    protected override void WriteIndent()
 
218
    {
 
219
      _writer.Write(Environment.NewLine);
 
220
 
 
221
      // levels of indentation multiplied by the indent count
 
222
      int currentIndentCount = Top*_indentation;
 
223
 
 
224
      while (currentIndentCount > 0)
 
225
      {
 
226
        // write up to a max of 10 characters at once to avoid creating too many new strings
 
227
        int writeCount = Math.Min(currentIndentCount, 10);
 
228
 
 
229
        _writer.Write(new string(_indentChar, writeCount));
 
230
 
 
231
        currentIndentCount -= writeCount;
 
232
      }
 
233
    }
 
234
 
 
235
    /// <summary>
 
236
    /// Writes the JSON value delimiter.
 
237
    /// </summary>
 
238
    protected override void WriteValueDelimiter()
 
239
    {
 
240
      _writer.Write(',');
 
241
    }
 
242
 
 
243
    /// <summary>
 
244
    /// Writes an indent space.
 
245
    /// </summary>
 
246
    protected override void WriteIndentSpace()
 
247
    {
 
248
      _writer.Write(' ');
 
249
    }
 
250
 
 
251
    private void WriteValueInternal(string value, JsonToken token)
 
252
    {
 
253
      _writer.Write(value);
 
254
    }
 
255
 
 
256
    #region WriteValue methods
 
257
    /// <summary>
 
258
    /// Writes a null value.
 
259
    /// </summary>
 
260
    public override void WriteNull()
 
261
    {
 
262
      base.WriteNull();
 
263
      WriteValueInternal(JsonConvert.Null, JsonToken.Null);
 
264
    }
 
265
 
 
266
    /// <summary>
 
267
    /// Writes an undefined value.
 
268
    /// </summary>
 
269
    public override void WriteUndefined()
 
270
    {
 
271
      base.WriteUndefined();
 
272
      WriteValueInternal(JsonConvert.Undefined, JsonToken.Undefined);
 
273
    }
 
274
 
 
275
    /// <summary>
 
276
    /// Writes raw JSON.
 
277
    /// </summary>
 
278
    /// <param name="json">The raw JSON to write.</param>
 
279
    public override void WriteRaw(string json)
 
280
    {
 
281
      base.WriteRaw(json);
 
282
 
 
283
      _writer.Write(json);
 
284
    }
 
285
 
 
286
    /// <summary>
 
287
    /// Writes a <see cref="String"/> value.
 
288
    /// </summary>
 
289
    /// <param name="value">The <see cref="String"/> value to write.</param>
 
290
    public override void WriteValue(string value)
 
291
    {
 
292
      base.WriteValue(value);
 
293
      if (value == null)
 
294
        WriteValueInternal(JsonConvert.Null, JsonToken.Null);
 
295
      else
 
296
        JavaScriptUtils.WriteEscapedJavaScriptString(_writer, value, _quoteChar, true);
 
297
    }
 
298
 
 
299
    /// <summary>
 
300
    /// Writes a <see cref="Int32"/> value.
 
301
    /// </summary>
 
302
    /// <param name="value">The <see cref="Int32"/> value to write.</param>
 
303
    public override void WriteValue(int value)
 
304
    {
 
305
      base.WriteValue(value);
 
306
      WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
 
307
    }
 
308
 
 
309
    /// <summary>
 
310
    /// Writes a <see cref="UInt32"/> value.
 
311
    /// </summary>
 
312
    /// <param name="value">The <see cref="UInt32"/> value to write.</param>
 
313
    [CLSCompliant(false)]
 
314
    public override void WriteValue(uint value)
 
315
    {
 
316
      base.WriteValue(value);
 
317
      WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
 
318
    }
 
319
 
 
320
    /// <summary>
 
321
    /// Writes a <see cref="Int64"/> value.
 
322
    /// </summary>
 
323
    /// <param name="value">The <see cref="Int64"/> value to write.</param>
 
324
    public override void WriteValue(long value)
 
325
    {
 
326
      base.WriteValue(value);
 
327
      WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
 
328
    }
 
329
 
 
330
    /// <summary>
 
331
    /// Writes a <see cref="UInt64"/> value.
 
332
    /// </summary>
 
333
    /// <param name="value">The <see cref="UInt64"/> value to write.</param>
 
334
    [CLSCompliant(false)]
 
335
    public override void WriteValue(ulong value)
 
336
    {
 
337
      base.WriteValue(value);
 
338
      WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
 
339
    }
 
340
 
 
341
    /// <summary>
 
342
    /// Writes a <see cref="Single"/> value.
 
343
    /// </summary>
 
344
    /// <param name="value">The <see cref="Single"/> value to write.</param>
 
345
    public override void WriteValue(float value)
 
346
    {
 
347
      base.WriteValue(value);
 
348
      WriteValueInternal(JsonConvert.ToString(value), JsonToken.Float);
 
349
    }
 
350
 
 
351
    /// <summary>
 
352
    /// Writes a <see cref="Double"/> value.
 
353
    /// </summary>
 
354
    /// <param name="value">The <see cref="Double"/> value to write.</param>
 
355
    public override void WriteValue(double value)
 
356
    {
 
357
      base.WriteValue(value);
 
358
      WriteValueInternal(JsonConvert.ToString(value), JsonToken.Float);
 
359
    }
 
360
 
 
361
    /// <summary>
 
362
    /// Writes a <see cref="Boolean"/> value.
 
363
    /// </summary>
 
364
    /// <param name="value">The <see cref="Boolean"/> value to write.</param>
 
365
    public override void WriteValue(bool value)
 
366
    {
 
367
      base.WriteValue(value);
 
368
      WriteValueInternal(JsonConvert.ToString(value), JsonToken.Boolean);
 
369
    }
 
370
 
 
371
    /// <summary>
 
372
    /// Writes a <see cref="Int16"/> value.
 
373
    /// </summary>
 
374
    /// <param name="value">The <see cref="Int16"/> value to write.</param>
 
375
    public override void WriteValue(short value)
 
376
    {
 
377
      base.WriteValue(value);
 
378
      WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
 
379
    }
 
380
 
 
381
    /// <summary>
 
382
    /// Writes a <see cref="UInt16"/> value.
 
383
    /// </summary>
 
384
    /// <param name="value">The <see cref="UInt16"/> value to write.</param>
 
385
    [CLSCompliant(false)]
 
386
    public override void WriteValue(ushort value)
 
387
    {
 
388
      base.WriteValue(value);
 
389
      WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
 
390
    }
 
391
 
 
392
    /// <summary>
 
393
    /// Writes a <see cref="Char"/> value.
 
394
    /// </summary>
 
395
    /// <param name="value">The <see cref="Char"/> value to write.</param>
 
396
    public override void WriteValue(char value)
 
397
    {
 
398
      base.WriteValue(value);
 
399
      WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
 
400
    }
 
401
 
 
402
    /// <summary>
 
403
    /// Writes a <see cref="Byte"/> value.
 
404
    /// </summary>
 
405
    /// <param name="value">The <see cref="Byte"/> value to write.</param>
 
406
    public override void WriteValue(byte value)
 
407
    {
 
408
      base.WriteValue(value);
 
409
      WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
 
410
    }
 
411
 
 
412
    /// <summary>
 
413
    /// Writes a <see cref="SByte"/> value.
 
414
    /// </summary>
 
415
    /// <param name="value">The <see cref="SByte"/> value to write.</param>
 
416
    [CLSCompliant(false)]
 
417
    public override void WriteValue(sbyte value)
 
418
    {
 
419
      base.WriteValue(value);
 
420
      WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
 
421
    }
 
422
 
 
423
    /// <summary>
 
424
    /// Writes a <see cref="Decimal"/> value.
 
425
    /// </summary>
 
426
    /// <param name="value">The <see cref="Decimal"/> value to write.</param>
 
427
    public override void WriteValue(decimal value)
 
428
    {
 
429
      base.WriteValue(value);
 
430
      WriteValueInternal(JsonConvert.ToString(value), JsonToken.Float);
 
431
    }
 
432
 
 
433
    /// <summary>
 
434
    /// Writes a <see cref="DateTime"/> value.
 
435
    /// </summary>
 
436
    /// <param name="value">The <see cref="DateTime"/> value to write.</param>
 
437
    public override void WriteValue(DateTime value)
 
438
    {
 
439
      base.WriteValue(value);
 
440
      value = JsonConvert.EnsureDateTime(value, DateTimeZoneHandling);
 
441
      JsonConvert.WriteDateTimeString(_writer, value, DateFormatHandling);
 
442
    }
 
443
 
 
444
    /// <summary>
 
445
    /// Writes a <see cref="T:Byte[]"/> value.
 
446
    /// </summary>
 
447
    /// <param name="value">The <see cref="T:Byte[]"/> value to write.</param>
 
448
    public override void WriteValue(byte[] value)
 
449
    {
 
450
      base.WriteValue(value);
 
451
 
 
452
      if (value != null)
 
453
      {
 
454
        _writer.Write(_quoteChar);
 
455
        Base64Encoder.Encode(value, 0, value.Length);
 
456
        Base64Encoder.Flush();
 
457
        _writer.Write(_quoteChar);
 
458
      }
 
459
    }
 
460
 
 
461
#if !PocketPC && !NET20
 
462
    /// <summary>
 
463
    /// Writes a <see cref="DateTimeOffset"/> value.
 
464
    /// </summary>
 
465
    /// <param name="value">The <see cref="DateTimeOffset"/> value to write.</param>
 
466
    public override void WriteValue(DateTimeOffset value)
 
467
    {
 
468
      base.WriteValue(value);
 
469
      WriteValueInternal(JsonConvert.ToString(value, DateFormatHandling), JsonToken.Date);
 
470
    }
 
471
#endif
 
472
 
 
473
    /// <summary>
 
474
    /// Writes a <see cref="Guid"/> value.
 
475
    /// </summary>
 
476
    /// <param name="value">The <see cref="Guid"/> value to write.</param>
 
477
    public override void WriteValue(Guid value)
 
478
    {
 
479
      base.WriteValue(value);
 
480
      WriteValueInternal(JsonConvert.ToString(value), JsonToken.String);
 
481
    }
 
482
 
 
483
    /// <summary>
 
484
    /// Writes a <see cref="TimeSpan"/> value.
 
485
    /// </summary>
 
486
    /// <param name="value">The <see cref="TimeSpan"/> value to write.</param>
 
487
    public override void WriteValue(TimeSpan value)
 
488
    {
 
489
      base.WriteValue(value);
 
490
      WriteValueInternal(JsonConvert.ToString(value), JsonToken.String);
 
491
    }
 
492
 
 
493
    /// <summary>
 
494
    /// Writes a <see cref="Uri"/> value.
 
495
    /// </summary>
 
496
    /// <param name="value">The <see cref="Uri"/> value to write.</param>
 
497
    public override void WriteValue(Uri value)
 
498
    {
 
499
      base.WriteValue(value);
 
500
      WriteValueInternal(JsonConvert.ToString(value), JsonToken.String);
 
501
    }
 
502
    #endregion
 
503
 
 
504
    /// <summary>
 
505
    /// Writes out a comment <code>/*...*/</code> containing the specified text. 
 
506
    /// </summary>
 
507
    /// <param name="text">Text to place inside the comment.</param>
 
508
    public override void WriteComment(string text)
 
509
    {
 
510
      base.WriteComment(text);
 
511
 
 
512
      _writer.Write("/*");
 
513
      _writer.Write(text);
 
514
      _writer.Write("*/");
 
515
    }
 
516
 
 
517
    /// <summary>
 
518
    /// Writes out the given white space.
 
519
    /// </summary>
 
520
    /// <param name="ws">The string of white space characters.</param>
 
521
    public override void WriteWhitespace(string ws)
 
522
    {
 
523
      base.WriteWhitespace(ws);
 
524
 
 
525
      _writer.Write(ws);
 
526
    }
 
527
  }
489
528
}
 
 
b'\\ No newline at end of file'