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

« back to all changes in this revision

Viewing changes to external/Newtonsoft.Json/Src/Newtonsoft.Json/Linq/JTokenWriter.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.Globalization;
 
28
using Newtonsoft.Json.Utilities;
 
29
 
 
30
namespace Newtonsoft.Json.Linq
 
31
{
 
32
  /// <summary>
 
33
  /// Represents a writer that provides a fast, non-cached, forward-only way of generating Json data.
 
34
  /// </summary>
 
35
  public class JTokenWriter : JsonWriter
 
36
  {
 
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;
 
41
 
 
42
    /// <summary>
 
43
    /// Gets the token being writen.
 
44
    /// </summary>
 
45
    /// <value>The token being writen.</value>
 
46
    public JToken Token
 
47
    {
 
48
      get
 
49
      {
 
50
        if (_token != null)
 
51
          return _token;
 
52
 
 
53
        return _value;
 
54
      }
 
55
    }
 
56
 
 
57
    /// <summary>
 
58
    /// Initializes a new instance of the <see cref="JTokenWriter"/> class writing to the given <see cref="JContainer"/>.
 
59
    /// </summary>
 
60
    /// <param name="container">The container being written to.</param>
 
61
    public JTokenWriter(JContainer container)
 
62
    {
 
63
      ValidationUtils.ArgumentNotNull(container, "container");
 
64
 
 
65
      _token = container;
 
66
      _parent = container;
 
67
    }
 
68
 
 
69
    /// <summary>
 
70
    /// Initializes a new instance of the <see cref="JTokenWriter"/> class.
 
71
    /// </summary>
 
72
    public JTokenWriter()
 
73
    {
 
74
    }
 
75
 
 
76
    /// <summary>
 
77
    /// Flushes whatever is in the buffer to the underlying streams and also flushes the underlying stream.
 
78
    /// </summary>
 
79
    public override void Flush()
 
80
    {
 
81
    }
 
82
 
 
83
    /// <summary>
 
84
    /// Closes this stream and the underlying stream.
 
85
    /// </summary>
 
86
    public override void Close()
 
87
    {
 
88
      base.Close();
 
89
    }
 
90
 
 
91
    /// <summary>
 
92
    /// Writes the beginning of a Json object.
 
93
    /// </summary>
 
94
    public override void WriteStartObject()
 
95
    {
 
96
      base.WriteStartObject();
 
97
 
 
98
      AddParent(new JObject());
 
99
    }
 
100
 
 
101
    private void AddParent(JContainer container)
 
102
    {
 
103
      if (_parent == null)
 
104
        _token = container;
 
105
      else
 
106
        _parent.AddAndSkipParentCheck(container);
 
107
 
 
108
      _parent = container;
 
109
    }
 
110
 
 
111
    private void RemoveParent()
 
112
    {
 
113
      _parent = _parent.Parent;
 
114
 
 
115
      if (_parent != null && _parent.Type == JTokenType.Property)
 
116
        _parent = _parent.Parent;
 
117
    }
 
118
 
 
119
    /// <summary>
 
120
    /// Writes the beginning of a Json array.
 
121
    /// </summary>
 
122
    public override void WriteStartArray()
 
123
    {
 
124
      base.WriteStartArray();
 
125
 
 
126
      AddParent(new JArray());
 
127
    }
 
128
 
 
129
    /// <summary>
 
130
    /// Writes the start of a constructor with the given name.
 
131
    /// </summary>
 
132
    /// <param name="name">The name of the constructor.</param>
 
133
    public override void WriteStartConstructor(string name)
 
134
    {
 
135
      base.WriteStartConstructor(name);
 
136
 
 
137
      AddParent(new JConstructor(name));
 
138
    }
 
139
 
 
140
    /// <summary>
 
141
    /// Writes the end.
 
142
    /// </summary>
 
143
    /// <param name="token">The token.</param>
 
144
    protected override void WriteEnd(JsonToken token)
 
145
    {
 
146
      RemoveParent();
 
147
    }
 
148
 
 
149
    /// <summary>
 
150
    /// Writes the property name of a name/value pair on a Json object.
 
151
    /// </summary>
 
152
    /// <param name="name">The name of the property.</param>
 
153
    public override void WritePropertyName(string name)
 
154
    {
 
155
      base.WritePropertyName(name);
 
156
 
 
157
      AddParent(new JProperty(name));
 
158
    }
 
159
 
 
160
    private void AddValue(object value, JsonToken token)
 
161
    {
 
162
      AddValue(new JValue(value), token);
 
163
    }
 
164
 
 
165
    internal void AddValue(JValue value, JsonToken token)
 
166
    {
 
167
      if (_parent != null)
 
168
      {
 
169
        _parent.Add(value);
 
170
 
 
171
        if (_parent.Type == JTokenType.Property)
 
172
          _parent = _parent.Parent;
 
173
      }
 
174
      else
 
175
      {
 
176
        _value = value;
 
177
      }
 
178
    }
 
179
 
 
180
    #region WriteValue methods
 
181
    /// <summary>
 
182
    /// Writes a null value.
 
183
    /// </summary>
 
184
    public override void WriteNull()
 
185
    {
 
186
      base.WriteNull();
 
187
      AddValue(null, JsonToken.Null);
 
188
    }
 
189
 
 
190
    /// <summary>
 
191
    /// Writes an undefined value.
 
192
    /// </summary>
 
193
    public override void WriteUndefined()
 
194
    {
 
195
      base.WriteUndefined();
 
196
      AddValue(null, JsonToken.Undefined);
 
197
    }
 
198
 
 
199
    /// <summary>
 
200
    /// Writes raw JSON.
 
201
    /// </summary>
 
202
    /// <param name="json">The raw JSON to write.</param>
 
203
    public override void WriteRaw(string json)
 
204
    {
 
205
      base.WriteRaw(json);
 
206
      AddValue(new JRaw(json), JsonToken.Raw);
 
207
    }
 
208
 
 
209
    /// <summary>
 
210
    /// Writes out a comment <code>/*...*/</code> containing the specified text.
 
211
    /// </summary>
 
212
    /// <param name="text">Text to place inside the comment.</param>
 
213
    public override void WriteComment(string text)
 
214
    {
 
215
      base.WriteComment(text);
 
216
      AddValue(JValue.CreateComment(text), JsonToken.Comment);
 
217
    }
 
218
 
 
219
    /// <summary>
 
220
    /// Writes a <see cref="String"/> value.
 
221
    /// </summary>
 
222
    /// <param name="value">The <see cref="String"/> value to write.</param>
 
223
    public override void WriteValue(string value)
 
224
    {
 
225
      base.WriteValue(value);
 
226
      AddValue(value ?? string.Empty, JsonToken.String);
 
227
    }
 
228
 
 
229
    /// <summary>
 
230
    /// Writes a <see cref="Int32"/> value.
 
231
    /// </summary>
 
232
    /// <param name="value">The <see cref="Int32"/> value to write.</param>
 
233
    public override void WriteValue(int value)
 
234
    {
 
235
      base.WriteValue(value);
 
236
      AddValue(value, JsonToken.Integer);
 
237
    }
 
238
 
 
239
    /// <summary>
 
240
    /// Writes a <see cref="UInt32"/> value.
 
241
    /// </summary>
 
242
    /// <param name="value">The <see cref="UInt32"/> value to write.</param>
 
243
    [CLSCompliant(false)]
 
244
    public override void WriteValue(uint value)
 
245
    {
 
246
      base.WriteValue(value);
 
247
      AddValue(value, JsonToken.Integer);
 
248
    }
 
249
 
 
250
    /// <summary>
 
251
    /// Writes a <see cref="Int64"/> value.
 
252
    /// </summary>
 
253
    /// <param name="value">The <see cref="Int64"/> value to write.</param>
 
254
    public override void WriteValue(long value)
 
255
    {
 
256
      base.WriteValue(value);
 
257
      AddValue(value, JsonToken.Integer);
 
258
    }
 
259
 
 
260
    /// <summary>
 
261
    /// Writes a <see cref="UInt64"/> value.
 
262
    /// </summary>
 
263
    /// <param name="value">The <see cref="UInt64"/> value to write.</param>
 
264
    [CLSCompliant(false)]
 
265
    public override void WriteValue(ulong value)
 
266
    {
 
267
      base.WriteValue(value);
 
268
      AddValue(value, JsonToken.Integer);
 
269
    }
 
270
 
 
271
    /// <summary>
 
272
    /// Writes a <see cref="Single"/> value.
 
273
    /// </summary>
 
274
    /// <param name="value">The <see cref="Single"/> value to write.</param>
 
275
    public override void WriteValue(float value)
 
276
    {
 
277
      base.WriteValue(value);
 
278
      AddValue(value, JsonToken.Float);
 
279
    }
 
280
 
 
281
    /// <summary>
 
282
    /// Writes a <see cref="Double"/> value.
 
283
    /// </summary>
 
284
    /// <param name="value">The <see cref="Double"/> value to write.</param>
 
285
    public override void WriteValue(double value)
 
286
    {
 
287
      base.WriteValue(value);
 
288
      AddValue(value, JsonToken.Float);
 
289
    }
 
290
 
 
291
    /// <summary>
 
292
    /// Writes a <see cref="Boolean"/> value.
 
293
    /// </summary>
 
294
    /// <param name="value">The <see cref="Boolean"/> value to write.</param>
 
295
    public override void WriteValue(bool value)
 
296
    {
 
297
      base.WriteValue(value);
 
298
      AddValue(value, JsonToken.Boolean);
 
299
    }
 
300
 
 
301
    /// <summary>
 
302
    /// Writes a <see cref="Int16"/> value.
 
303
    /// </summary>
 
304
    /// <param name="value">The <see cref="Int16"/> value to write.</param>
 
305
    public override void WriteValue(short value)
 
306
    {
 
307
      base.WriteValue(value);
 
308
      AddValue(value, JsonToken.Integer);
 
309
    }
 
310
 
 
311
    /// <summary>
 
312
    /// Writes a <see cref="UInt16"/> value.
 
313
    /// </summary>
 
314
    /// <param name="value">The <see cref="UInt16"/> value to write.</param>
 
315
    [CLSCompliant(false)]
 
316
    public override void WriteValue(ushort value)
 
317
    {
 
318
      base.WriteValue(value);
 
319
      AddValue(value, JsonToken.Integer);
 
320
    }
 
321
 
 
322
    /// <summary>
 
323
    /// Writes a <see cref="Char"/> value.
 
324
    /// </summary>
 
325
    /// <param name="value">The <see cref="Char"/> value to write.</param>
 
326
    public override void WriteValue(char value)
 
327
    {
 
328
      base.WriteValue(value);
 
329
      string s = null;
 
330
#if !(NETFX_CORE || PORTABLE)
 
331
      s = value.ToString(CultureInfo.InvariantCulture);
 
332
#else
 
333
      s = value.ToString();
 
334
#endif
 
335
      AddValue(s, JsonToken.String);
 
336
    }
 
337
 
 
338
    /// <summary>
 
339
    /// Writes a <see cref="Byte"/> value.
 
340
    /// </summary>
 
341
    /// <param name="value">The <see cref="Byte"/> value to write.</param>
 
342
    public override void WriteValue(byte value)
 
343
    {
 
344
      base.WriteValue(value);
 
345
      AddValue(value, JsonToken.Integer);
 
346
    }
 
347
 
 
348
    /// <summary>
 
349
    /// Writes a <see cref="SByte"/> value.
 
350
    /// </summary>
 
351
    /// <param name="value">The <see cref="SByte"/> value to write.</param>
 
352
    [CLSCompliant(false)]
 
353
    public override void WriteValue(sbyte value)
 
354
    {
 
355
      base.WriteValue(value);
 
356
      AddValue(value, JsonToken.Integer);
 
357
    }
 
358
 
 
359
    /// <summary>
 
360
    /// Writes a <see cref="Decimal"/> value.
 
361
    /// </summary>
 
362
    /// <param name="value">The <see cref="Decimal"/> value to write.</param>
 
363
    public override void WriteValue(decimal value)
 
364
    {
 
365
      base.WriteValue(value);
 
366
      AddValue(value, JsonToken.Float);
 
367
    }
 
368
 
 
369
    /// <summary>
 
370
    /// Writes a <see cref="DateTime"/> value.
 
371
    /// </summary>
 
372
    /// <param name="value">The <see cref="DateTime"/> value to write.</param>
 
373
    public override void WriteValue(DateTime value)
 
374
    {
 
375
      base.WriteValue(value);
 
376
      value = JsonConvert.EnsureDateTime(value, DateTimeZoneHandling);
 
377
      AddValue(value, JsonToken.Date);
 
378
    }
 
379
 
 
380
#if !PocketPC && !NET20
 
381
    /// <summary>
 
382
    /// Writes a <see cref="DateTimeOffset"/> value.
 
383
    /// </summary>
 
384
    /// <param name="value">The <see cref="DateTimeOffset"/> value to write.</param>
 
385
    public override void WriteValue(DateTimeOffset value)
 
386
    {
 
387
      base.WriteValue(value);
 
388
      AddValue(value, JsonToken.Date);
 
389
    }
 
390
#endif
 
391
 
 
392
    /// <summary>
 
393
    /// Writes a <see cref="T:Byte[]"/> value.
 
394
    /// </summary>
 
395
    /// <param name="value">The <see cref="T:Byte[]"/> value to write.</param>
 
396
    public override void WriteValue(byte[] value)
 
397
    {
 
398
      base.WriteValue(value);
 
399
      AddValue(value, JsonToken.Bytes);
 
400
    }
 
401
 
 
402
    /// <summary>
 
403
    /// Writes a <see cref="TimeSpan"/> value.
 
404
    /// </summary>
 
405
    /// <param name="value">The <see cref="TimeSpan"/> value to write.</param>
 
406
    public override void WriteValue(TimeSpan value)
 
407
    {
 
408
      base.WriteValue(value);
 
409
      AddValue(value, JsonToken.String);
 
410
    }
 
411
 
 
412
    /// <summary>
 
413
    /// Writes a <see cref="Guid"/> value.
 
414
    /// </summary>
 
415
    /// <param name="value">The <see cref="Guid"/> value to write.</param>
 
416
    public override void WriteValue(Guid value)
 
417
    {
 
418
      base.WriteValue(value);
 
419
      AddValue(value, JsonToken.String);
 
420
    }
 
421
 
 
422
    /// <summary>
 
423
    /// Writes a <see cref="Uri"/> value.
 
424
    /// </summary>
 
425
    /// <param name="value">The <see cref="Uri"/> value to write.</param>
 
426
    public override void WriteValue(Uri value)
 
427
    {
 
428
      base.WriteValue(value);
 
429
      AddValue(value, JsonToken.String);
 
430
    }
 
431
    #endregion
 
432
  }
 
433
}