2
// Copyright (c) 2007 James Newton-King
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
13
// The above copyright notice and this permission notice shall be
14
// included in all copies or substantial portions of the Software.
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.
27
using System.Collections.Generic;
31
using Newtonsoft.Json.Utilities;
33
namespace Newtonsoft.Json
36
/// Represents a writer that provides a fast, non-cached, forward-only way of generating Json data.
38
public class JsonTextWriter : JsonWriter
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;
47
private Base64Encoder Base64Encoder
51
if (_base64Encoder == null)
52
_base64Encoder = new Base64Encoder(_writer);
54
return _base64Encoder;
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>.
61
public int Indentation
63
get { return _indentation; }
67
throw new ArgumentException("Indentation value must be greater than 0.");
74
/// Gets or sets which character to use to quote attribute values.
78
get { return _quoteChar; }
81
if (value != '"' && value != '\'')
82
throw new ArgumentException(@"Invalid JavaScript string quote character. Valid quote characters are ' and "".");
89
/// Gets or sets which character to use for indenting when <paramref name="Formatting"/> is set to <c>Formatting.Indented</c>.
91
public char IndentChar
93
get { return _indentChar; }
94
set { _indentChar = value; }
98
/// Gets or sets a value indicating whether object names will be surrounded with quotes.
100
public bool QuoteName
102
get { return _quoteName; }
103
set { _quoteName = value; }
107
/// Creates an instance of the <c>JsonWriter</c> class using the specified <see cref="TextWriter"/>.
109
/// <param name="textWriter">The <c>TextWriter</c> to write to.</param>
110
public JsonTextWriter(TextWriter textWriter)
112
if (textWriter == null)
113
throw new ArgumentNullException("textWriter");
115
_writer = textWriter;
123
/// Flushes whatever is in the buffer to the underlying streams and also flushes the underlying stream.
125
public override void Flush()
131
/// Closes this stream and the underlying stream.
133
public override void Close()
141
/// Writes the beginning of a Json object.
143
public override void WriteStartObject()
145
base.WriteStartObject();
151
/// Writes the beginning of a Json array.
153
public override void WriteStartArray()
155
base.WriteStartArray();
161
/// Writes the start of a constructor with the given name.
163
/// <param name="name">The name of the constructor.</param>
164
public override void WriteStartConstructor(string name)
166
base.WriteStartConstructor(name);
168
_writer.Write("new ");
174
/// Writes the specified end token.
176
/// <param name="token">The end token to write.</param>
177
protected override void WriteEnd(JsonToken token)
181
case JsonToken.EndObject:
184
case JsonToken.EndArray:
187
case JsonToken.EndConstructor:
191
throw new JsonWriterException("Invalid JsonToken: " + token);
196
/// Writes the property name of a name/value pair on a Json object.
198
/// <param name="name">The name of the property.</param>
199
public override void WritePropertyName(string name)
201
base.WritePropertyName(name);
203
JavaScriptUtils.WriteEscapedJavaScriptString(_writer, name, _quoteChar, _quoteName);
209
/// Writes indent characters.
211
protected override void WriteIndent()
213
if (Formatting == Formatting.Indented)
215
_writer.Write(Environment.NewLine);
217
// levels of indentation multiplied by the indent count
218
int currentIndentCount = Top * _indentation;
220
for (int i = 0; i < currentIndentCount; i++)
222
_writer.Write(_indentChar);
228
/// Writes the JSON value delimiter.
230
protected override void WriteValueDelimiter()
236
/// Writes an indent space.
238
protected override void WriteIndentSpace()
243
private void WriteValueInternal(string value, JsonToken token)
245
_writer.Write(value);
248
#region WriteValue methods
250
/// Writes a null value.
252
public override void WriteNull()
255
WriteValueInternal(JsonConvert.Null, JsonToken.Null);
259
/// Writes an undefined value.
261
public override void WriteUndefined()
263
base.WriteUndefined();
264
WriteValueInternal(JsonConvert.Undefined, JsonToken.Undefined);
270
/// <param name="json">The raw JSON to write.</param>
271
public override void WriteRaw(string json)
279
/// Writes a <see cref="String"/> value.
281
/// <param name="value">The <see cref="String"/> value to write.</param>
282
public override void WriteValue(string value)
284
base.WriteValue(value);
286
WriteValueInternal(JsonConvert.Null, JsonToken.Null);
288
JavaScriptUtils.WriteEscapedJavaScriptString(_writer, value, _quoteChar, true);
292
/// Writes a <see cref="Int32"/> value.
294
/// <param name="value">The <see cref="Int32"/> value to write.</param>
295
public override void WriteValue(int value)
297
base.WriteValue(value);
298
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
302
/// Writes a <see cref="UInt32"/> value.
304
/// <param name="value">The <see cref="UInt32"/> value to write.</param>
305
[CLSCompliant(false)]
306
public override void WriteValue(uint value)
308
base.WriteValue(value);
309
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
313
/// Writes a <see cref="Int64"/> value.
315
/// <param name="value">The <see cref="Int64"/> value to write.</param>
316
public override void WriteValue(long value)
318
base.WriteValue(value);
319
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
323
/// Writes a <see cref="UInt64"/> value.
325
/// <param name="value">The <see cref="UInt64"/> value to write.</param>
326
[CLSCompliant(false)]
327
public override void WriteValue(ulong value)
329
base.WriteValue(value);
330
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
334
/// Writes a <see cref="Single"/> value.
336
/// <param name="value">The <see cref="Single"/> value to write.</param>
337
public override void WriteValue(float value)
339
base.WriteValue(value);
340
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Float);
344
/// Writes a <see cref="Double"/> value.
346
/// <param name="value">The <see cref="Double"/> value to write.</param>
347
public override void WriteValue(double value)
349
base.WriteValue(value);
350
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Float);
354
/// Writes a <see cref="Boolean"/> value.
356
/// <param name="value">The <see cref="Boolean"/> value to write.</param>
357
public override void WriteValue(bool value)
359
base.WriteValue(value);
360
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Boolean);
364
/// Writes a <see cref="Int16"/> value.
366
/// <param name="value">The <see cref="Int16"/> value to write.</param>
367
public override void WriteValue(short value)
369
base.WriteValue(value);
370
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
374
/// Writes a <see cref="UInt16"/> value.
376
/// <param name="value">The <see cref="UInt16"/> value to write.</param>
377
[CLSCompliant(false)]
378
public override void WriteValue(ushort value)
380
base.WriteValue(value);
381
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
385
/// Writes a <see cref="Char"/> value.
387
/// <param name="value">The <see cref="Char"/> value to write.</param>
388
public override void WriteValue(char value)
390
base.WriteValue(value);
391
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
395
/// Writes a <see cref="Byte"/> value.
397
/// <param name="value">The <see cref="Byte"/> value to write.</param>
398
public override void WriteValue(byte value)
400
base.WriteValue(value);
401
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
405
/// Writes a <see cref="SByte"/> value.
407
/// <param name="value">The <see cref="SByte"/> value to write.</param>
408
[CLSCompliant(false)]
409
public override void WriteValue(sbyte value)
411
base.WriteValue(value);
412
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
416
/// Writes a <see cref="Decimal"/> value.
418
/// <param name="value">The <see cref="Decimal"/> value to write.</param>
419
public override void WriteValue(decimal value)
421
base.WriteValue(value);
422
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Float);
426
/// Writes a <see cref="DateTime"/> value.
428
/// <param name="value">The <see cref="DateTime"/> value to write.</param>
429
public override void WriteValue(DateTime value)
431
base.WriteValue(value);
432
JsonConvert.WriteDateTimeString(_writer, value);
436
/// Writes a <see cref="T:Byte[]"/> value.
438
/// <param name="value">The <see cref="T:Byte[]"/> value to write.</param>
439
public override void WriteValue(byte[] value)
441
base.WriteValue(value);
445
_writer.Write(_quoteChar);
446
Base64Encoder.Encode(value, 0, value.Length);
447
Base64Encoder.Flush();
448
_writer.Write(_quoteChar);
452
#if !PocketPC && !NET20
454
/// Writes a <see cref="DateTimeOffset"/> value.
456
/// <param name="value">The <see cref="DateTimeOffset"/> value to write.</param>
457
public override void WriteValue(DateTimeOffset value)
459
base.WriteValue(value);
460
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Date);
466
/// Writes out a comment <code>/*...*/</code> containing the specified text.
468
/// <param name="text">Text to place inside the comment.</param>
469
public override void WriteComment(string text)
471
base.WriteComment(text);
479
/// Writes out the given white space.
481
/// <param name="ws">The string of white space characters.</param>
482
public override void WriteWhitespace(string ws)
484
base.WriteWhitespace(ws);
2
// Copyright (c) 2007 James Newton-King
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
13
// The above copyright notice and this permission notice shall be
14
// included in all copies or substantial portions of the Software.
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.
27
using System.Collections.Generic;
28
using System.Globalization;
32
using Newtonsoft.Json.Utilities;
34
namespace Newtonsoft.Json
37
/// Represents a writer that provides a fast, non-cached, forward-only way of generating Json data.
39
public class JsonTextWriter : JsonWriter
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;
48
private Base64Encoder Base64Encoder
52
if (_base64Encoder == null)
53
_base64Encoder = new Base64Encoder(_writer);
55
return _base64Encoder;
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>.
62
public int Indentation
64
get { return _indentation; }
68
throw new ArgumentException("Indentation value must be greater than 0.");
75
/// Gets or sets which character to use to quote attribute values.
79
get { return _quoteChar; }
82
if (value != '"' && value != '\'')
83
throw new ArgumentException(@"Invalid JavaScript string quote character. Valid quote characters are ' and "".");
90
/// Gets or sets which character to use for indenting when <see cref="Formatting"/> is set to <c>Formatting.Indented</c>.
92
public char IndentChar
94
get { return _indentChar; }
95
set { _indentChar = value; }
99
/// Gets or sets a value indicating whether object names will be surrounded with quotes.
101
public bool QuoteName
103
get { return _quoteName; }
104
set { _quoteName = value; }
108
/// Creates an instance of the <c>JsonWriter</c> class using the specified <see cref="TextWriter"/>.
110
/// <param name="textWriter">The <c>TextWriter</c> to write to.</param>
111
public JsonTextWriter(TextWriter textWriter)
113
if (textWriter == null)
114
throw new ArgumentNullException("textWriter");
116
_writer = textWriter;
124
/// Flushes whatever is in the buffer to the underlying streams and also flushes the underlying stream.
126
public override void Flush()
132
/// Closes this stream and the underlying stream.
134
public override void Close()
138
if (CloseOutput && _writer != null)
139
#if !(NETFX_CORE || PORTABLE)
147
/// Writes the beginning of a Json object.
149
public override void WriteStartObject()
151
base.WriteStartObject();
157
/// Writes the beginning of a Json array.
159
public override void WriteStartArray()
161
base.WriteStartArray();
167
/// Writes the start of a constructor with the given name.
169
/// <param name="name">The name of the constructor.</param>
170
public override void WriteStartConstructor(string name)
172
base.WriteStartConstructor(name);
174
_writer.Write("new ");
180
/// Writes the specified end token.
182
/// <param name="token">The end token to write.</param>
183
protected override void WriteEnd(JsonToken token)
187
case JsonToken.EndObject:
190
case JsonToken.EndArray:
193
case JsonToken.EndConstructor:
197
throw JsonWriterException.Create(this, "Invalid JsonToken: " + token, null);
202
/// Writes the property name of a name/value pair on a Json object.
204
/// <param name="name">The name of the property.</param>
205
public override void WritePropertyName(string name)
207
base.WritePropertyName(name);
209
JavaScriptUtils.WriteEscapedJavaScriptString(_writer, name, _quoteChar, _quoteName);
215
/// Writes indent characters.
217
protected override void WriteIndent()
219
_writer.Write(Environment.NewLine);
221
// levels of indentation multiplied by the indent count
222
int currentIndentCount = Top*_indentation;
224
while (currentIndentCount > 0)
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);
229
_writer.Write(new string(_indentChar, writeCount));
231
currentIndentCount -= writeCount;
236
/// Writes the JSON value delimiter.
238
protected override void WriteValueDelimiter()
244
/// Writes an indent space.
246
protected override void WriteIndentSpace()
251
private void WriteValueInternal(string value, JsonToken token)
253
_writer.Write(value);
256
#region WriteValue methods
258
/// Writes a null value.
260
public override void WriteNull()
263
WriteValueInternal(JsonConvert.Null, JsonToken.Null);
267
/// Writes an undefined value.
269
public override void WriteUndefined()
271
base.WriteUndefined();
272
WriteValueInternal(JsonConvert.Undefined, JsonToken.Undefined);
278
/// <param name="json">The raw JSON to write.</param>
279
public override void WriteRaw(string json)
287
/// Writes a <see cref="String"/> value.
289
/// <param name="value">The <see cref="String"/> value to write.</param>
290
public override void WriteValue(string value)
292
base.WriteValue(value);
294
WriteValueInternal(JsonConvert.Null, JsonToken.Null);
296
JavaScriptUtils.WriteEscapedJavaScriptString(_writer, value, _quoteChar, true);
300
/// Writes a <see cref="Int32"/> value.
302
/// <param name="value">The <see cref="Int32"/> value to write.</param>
303
public override void WriteValue(int value)
305
base.WriteValue(value);
306
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
310
/// Writes a <see cref="UInt32"/> value.
312
/// <param name="value">The <see cref="UInt32"/> value to write.</param>
313
[CLSCompliant(false)]
314
public override void WriteValue(uint value)
316
base.WriteValue(value);
317
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
321
/// Writes a <see cref="Int64"/> value.
323
/// <param name="value">The <see cref="Int64"/> value to write.</param>
324
public override void WriteValue(long value)
326
base.WriteValue(value);
327
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
331
/// Writes a <see cref="UInt64"/> value.
333
/// <param name="value">The <see cref="UInt64"/> value to write.</param>
334
[CLSCompliant(false)]
335
public override void WriteValue(ulong value)
337
base.WriteValue(value);
338
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
342
/// Writes a <see cref="Single"/> value.
344
/// <param name="value">The <see cref="Single"/> value to write.</param>
345
public override void WriteValue(float value)
347
base.WriteValue(value);
348
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Float);
352
/// Writes a <see cref="Double"/> value.
354
/// <param name="value">The <see cref="Double"/> value to write.</param>
355
public override void WriteValue(double value)
357
base.WriteValue(value);
358
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Float);
362
/// Writes a <see cref="Boolean"/> value.
364
/// <param name="value">The <see cref="Boolean"/> value to write.</param>
365
public override void WriteValue(bool value)
367
base.WriteValue(value);
368
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Boolean);
372
/// Writes a <see cref="Int16"/> value.
374
/// <param name="value">The <see cref="Int16"/> value to write.</param>
375
public override void WriteValue(short value)
377
base.WriteValue(value);
378
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
382
/// Writes a <see cref="UInt16"/> value.
384
/// <param name="value">The <see cref="UInt16"/> value to write.</param>
385
[CLSCompliant(false)]
386
public override void WriteValue(ushort value)
388
base.WriteValue(value);
389
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
393
/// Writes a <see cref="Char"/> value.
395
/// <param name="value">The <see cref="Char"/> value to write.</param>
396
public override void WriteValue(char value)
398
base.WriteValue(value);
399
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
403
/// Writes a <see cref="Byte"/> value.
405
/// <param name="value">The <see cref="Byte"/> value to write.</param>
406
public override void WriteValue(byte value)
408
base.WriteValue(value);
409
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
413
/// Writes a <see cref="SByte"/> value.
415
/// <param name="value">The <see cref="SByte"/> value to write.</param>
416
[CLSCompliant(false)]
417
public override void WriteValue(sbyte value)
419
base.WriteValue(value);
420
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Integer);
424
/// Writes a <see cref="Decimal"/> value.
426
/// <param name="value">The <see cref="Decimal"/> value to write.</param>
427
public override void WriteValue(decimal value)
429
base.WriteValue(value);
430
WriteValueInternal(JsonConvert.ToString(value), JsonToken.Float);
434
/// Writes a <see cref="DateTime"/> value.
436
/// <param name="value">The <see cref="DateTime"/> value to write.</param>
437
public override void WriteValue(DateTime value)
439
base.WriteValue(value);
440
value = JsonConvert.EnsureDateTime(value, DateTimeZoneHandling);
441
JsonConvert.WriteDateTimeString(_writer, value, DateFormatHandling);
445
/// Writes a <see cref="T:Byte[]"/> value.
447
/// <param name="value">The <see cref="T:Byte[]"/> value to write.</param>
448
public override void WriteValue(byte[] value)
450
base.WriteValue(value);
454
_writer.Write(_quoteChar);
455
Base64Encoder.Encode(value, 0, value.Length);
456
Base64Encoder.Flush();
457
_writer.Write(_quoteChar);
461
#if !PocketPC && !NET20
463
/// Writes a <see cref="DateTimeOffset"/> value.
465
/// <param name="value">The <see cref="DateTimeOffset"/> value to write.</param>
466
public override void WriteValue(DateTimeOffset value)
468
base.WriteValue(value);
469
WriteValueInternal(JsonConvert.ToString(value, DateFormatHandling), JsonToken.Date);
474
/// Writes a <see cref="Guid"/> value.
476
/// <param name="value">The <see cref="Guid"/> value to write.</param>
477
public override void WriteValue(Guid value)
479
base.WriteValue(value);
480
WriteValueInternal(JsonConvert.ToString(value), JsonToken.String);
484
/// Writes a <see cref="TimeSpan"/> value.
486
/// <param name="value">The <see cref="TimeSpan"/> value to write.</param>
487
public override void WriteValue(TimeSpan value)
489
base.WriteValue(value);
490
WriteValueInternal(JsonConvert.ToString(value), JsonToken.String);
494
/// Writes a <see cref="Uri"/> value.
496
/// <param name="value">The <see cref="Uri"/> value to write.</param>
497
public override void WriteValue(Uri value)
499
base.WriteValue(value);
500
WriteValueInternal(JsonConvert.ToString(value), JsonToken.String);
505
/// Writes out a comment <code>/*...*/</code> containing the specified text.
507
/// <param name="text">Text to place inside the comment.</param>
508
public override void WriteComment(string text)
510
base.WriteComment(text);
518
/// Writes out the given white space.
520
/// <param name="ws">The string of white space characters.</param>
521
public override void WriteWhitespace(string ws)
523
base.WriteWhitespace(ws);
b'\\ No newline at end of file'