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.Globalization;
28
using Newtonsoft.Json.Utilities;
30
namespace Newtonsoft.Json.Linq
33
/// Represents a writer that provides a fast, non-cached, forward-only way of generating Json data.
35
public class JTokenWriter : JsonWriter
37
private JContainer _token;
38
private JContainer _parent;
39
// used when writer is writing single value and the value has no containing parent
40
private JValue _value;
43
/// Gets the token being writen.
45
/// <value>The token being writen.</value>
58
/// Initializes a new instance of the <see cref="JTokenWriter"/> class writing to the given <see cref="JContainer"/>.
60
/// <param name="container">The container being written to.</param>
61
public JTokenWriter(JContainer container)
63
ValidationUtils.ArgumentNotNull(container, "container");
70
/// Initializes a new instance of the <see cref="JTokenWriter"/> class.
77
/// Flushes whatever is in the buffer to the underlying streams and also flushes the underlying stream.
79
public override void Flush()
84
/// Closes this stream and the underlying stream.
86
public override void Close()
92
/// Writes the beginning of a Json object.
94
public override void WriteStartObject()
96
base.WriteStartObject();
98
AddParent(new JObject());
101
private void AddParent(JContainer container)
106
_parent.AddAndSkipParentCheck(container);
111
private void RemoveParent()
113
_parent = _parent.Parent;
115
if (_parent != null && _parent.Type == JTokenType.Property)
116
_parent = _parent.Parent;
120
/// Writes the beginning of a Json array.
122
public override void WriteStartArray()
124
base.WriteStartArray();
126
AddParent(new JArray());
130
/// Writes the start of a constructor with the given name.
132
/// <param name="name">The name of the constructor.</param>
133
public override void WriteStartConstructor(string name)
135
base.WriteStartConstructor(name);
137
AddParent(new JConstructor(name));
143
/// <param name="token">The token.</param>
144
protected override void WriteEnd(JsonToken token)
150
/// Writes the property name of a name/value pair on a Json object.
152
/// <param name="name">The name of the property.</param>
153
public override void WritePropertyName(string name)
155
base.WritePropertyName(name);
157
AddParent(new JProperty(name));
160
private void AddValue(object value, JsonToken token)
162
AddValue(new JValue(value), token);
165
internal void AddValue(JValue value, JsonToken token)
171
if (_parent.Type == JTokenType.Property)
172
_parent = _parent.Parent;
180
#region WriteValue methods
182
/// Writes a null value.
184
public override void WriteNull()
187
AddValue(null, JsonToken.Null);
191
/// Writes an undefined value.
193
public override void WriteUndefined()
195
base.WriteUndefined();
196
AddValue(null, JsonToken.Undefined);
202
/// <param name="json">The raw JSON to write.</param>
203
public override void WriteRaw(string json)
206
AddValue(new JRaw(json), JsonToken.Raw);
210
/// Writes out a comment <code>/*...*/</code> containing the specified text.
212
/// <param name="text">Text to place inside the comment.</param>
213
public override void WriteComment(string text)
215
base.WriteComment(text);
216
AddValue(JValue.CreateComment(text), JsonToken.Comment);
220
/// Writes a <see cref="String"/> value.
222
/// <param name="value">The <see cref="String"/> value to write.</param>
223
public override void WriteValue(string value)
225
base.WriteValue(value);
226
AddValue(value ?? string.Empty, JsonToken.String);
230
/// Writes a <see cref="Int32"/> value.
232
/// <param name="value">The <see cref="Int32"/> value to write.</param>
233
public override void WriteValue(int value)
235
base.WriteValue(value);
236
AddValue(value, JsonToken.Integer);
240
/// Writes a <see cref="UInt32"/> value.
242
/// <param name="value">The <see cref="UInt32"/> value to write.</param>
243
[CLSCompliant(false)]
244
public override void WriteValue(uint value)
246
base.WriteValue(value);
247
AddValue(value, JsonToken.Integer);
251
/// Writes a <see cref="Int64"/> value.
253
/// <param name="value">The <see cref="Int64"/> value to write.</param>
254
public override void WriteValue(long value)
256
base.WriteValue(value);
257
AddValue(value, JsonToken.Integer);
261
/// Writes a <see cref="UInt64"/> value.
263
/// <param name="value">The <see cref="UInt64"/> value to write.</param>
264
[CLSCompliant(false)]
265
public override void WriteValue(ulong value)
267
base.WriteValue(value);
268
AddValue(value, JsonToken.Integer);
272
/// Writes a <see cref="Single"/> value.
274
/// <param name="value">The <see cref="Single"/> value to write.</param>
275
public override void WriteValue(float value)
277
base.WriteValue(value);
278
AddValue(value, JsonToken.Float);
282
/// Writes a <see cref="Double"/> value.
284
/// <param name="value">The <see cref="Double"/> value to write.</param>
285
public override void WriteValue(double value)
287
base.WriteValue(value);
288
AddValue(value, JsonToken.Float);
292
/// Writes a <see cref="Boolean"/> value.
294
/// <param name="value">The <see cref="Boolean"/> value to write.</param>
295
public override void WriteValue(bool value)
297
base.WriteValue(value);
298
AddValue(value, JsonToken.Boolean);
302
/// Writes a <see cref="Int16"/> value.
304
/// <param name="value">The <see cref="Int16"/> value to write.</param>
305
public override void WriteValue(short value)
307
base.WriteValue(value);
308
AddValue(value, JsonToken.Integer);
312
/// Writes a <see cref="UInt16"/> value.
314
/// <param name="value">The <see cref="UInt16"/> value to write.</param>
315
[CLSCompliant(false)]
316
public override void WriteValue(ushort value)
318
base.WriteValue(value);
319
AddValue(value, JsonToken.Integer);
323
/// Writes a <see cref="Char"/> value.
325
/// <param name="value">The <see cref="Char"/> value to write.</param>
326
public override void WriteValue(char value)
328
base.WriteValue(value);
330
#if !(NETFX_CORE || PORTABLE)
331
s = value.ToString(CultureInfo.InvariantCulture);
333
s = value.ToString();
335
AddValue(s, JsonToken.String);
339
/// Writes a <see cref="Byte"/> value.
341
/// <param name="value">The <see cref="Byte"/> value to write.</param>
342
public override void WriteValue(byte value)
344
base.WriteValue(value);
345
AddValue(value, JsonToken.Integer);
349
/// Writes a <see cref="SByte"/> value.
351
/// <param name="value">The <see cref="SByte"/> value to write.</param>
352
[CLSCompliant(false)]
353
public override void WriteValue(sbyte value)
355
base.WriteValue(value);
356
AddValue(value, JsonToken.Integer);
360
/// Writes a <see cref="Decimal"/> value.
362
/// <param name="value">The <see cref="Decimal"/> value to write.</param>
363
public override void WriteValue(decimal value)
365
base.WriteValue(value);
366
AddValue(value, JsonToken.Float);
370
/// Writes a <see cref="DateTime"/> value.
372
/// <param name="value">The <see cref="DateTime"/> value to write.</param>
373
public override void WriteValue(DateTime value)
375
base.WriteValue(value);
376
value = JsonConvert.EnsureDateTime(value, DateTimeZoneHandling);
377
AddValue(value, JsonToken.Date);
380
#if !PocketPC && !NET20
382
/// Writes a <see cref="DateTimeOffset"/> value.
384
/// <param name="value">The <see cref="DateTimeOffset"/> value to write.</param>
385
public override void WriteValue(DateTimeOffset value)
387
base.WriteValue(value);
388
AddValue(value, JsonToken.Date);
393
/// Writes a <see cref="T:Byte[]"/> value.
395
/// <param name="value">The <see cref="T:Byte[]"/> value to write.</param>
396
public override void WriteValue(byte[] value)
398
base.WriteValue(value);
399
AddValue(value, JsonToken.Bytes);
403
/// Writes a <see cref="TimeSpan"/> value.
405
/// <param name="value">The <see cref="TimeSpan"/> value to write.</param>
406
public override void WriteValue(TimeSpan value)
408
base.WriteValue(value);
409
AddValue(value, JsonToken.String);
413
/// Writes a <see cref="Guid"/> value.
415
/// <param name="value">The <see cref="Guid"/> value to write.</param>
416
public override void WriteValue(Guid value)
418
base.WriteValue(value);
419
AddValue(value, JsonToken.String);
423
/// Writes a <see cref="Uri"/> value.
425
/// <param name="value">The <see cref="Uri"/> value to write.</param>
426
public override void WriteValue(Uri value)
428
base.WriteValue(value);
429
AddValue(value, JsonToken.String);