Json.NET
Code Coverage Statistics for Source File

Newtonsoft.Json\Linq\JTokenWriter.cs

Symbol Coverage: 76.85% (83 of 108)

Branch Coverage: 80.43% (37 of 46)

Cyclomatic Complexity Avg: 1.20 Max:3

Code Lines: 108


L V Source
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using Newtonsoft.Json.Utilities;
6

  
7
namespace Newtonsoft.Json.Linq
8
{
9
  /// <summary>
10
  /// Represents a writer that provides a fast, non-cached, forward-only way of generating Json data.
11
  /// </summary>
12
  public class JTokenWriter : JsonWriter
13
  {
14
    private JContainer _token;
15
    private JContainer _parent;
16
    // used when writer is writing single value and the value has no containing parent
17
    private JValue _value;
18

  
19
    /// <summary>
20
    /// Gets the token being writen.
21
    /// </summary>
22
    /// <value>The token being writen.</value>
23
    public JToken Token
24
    {
25
      get
26
      {
27
 50
        if (_token != null)
28
 32
          return _token;
29

  
30
 18
        return _value;
31
 50
      }
32
    }
33

  
34
    /// <summary>
35
    /// Initializes a new instance of the <see cref="JTokenWriter"/> class writing to the given <see cref="JContainer"/>.
36
    /// </summary>
37
    /// <param name="container">The container being written to.</param>
38
 1
    public JTokenWriter(JContainer container)
39
    {
40
 1
      ValidationUtils.ArgumentNotNull(container, "container");
41

  
42
 1
      _token = container;
43
 1
      _parent = container;
44
 1
    }
45

  
46
    /// <summary>
47
    /// Initializes a new instance of the <see cref="JTokenWriter"/> class.
48
    /// </summary>
49
 51
    public JTokenWriter()
50
    {
51
 51
    }
52

  
53
    /// <summary>
54
    /// Flushes whatever is in the buffer to the underlying streams and also flushes the underlying stream.
55
    /// </summary>
56
    public override void Flush()
57
    {
58
 0
    }
59

  
60
    /// <summary>
61
    /// Closes this stream and the underlying stream.
62
    /// </summary>
63
    public override void Close()
64
    {
65
 46
      base.Close();
66
 46
    }
67

  
68
    /// <summary>
69
    /// Writes the beginning of a Json object.
70
    /// </summary>
71
    public override void WriteStartObject()
72
    {
73
 38
      base.WriteStartObject();
74

  
75
 38
      AddParent(new JObject());
76
 38
    }
77

  
78
    private void AddParent(JContainer container)
79
    {
80
 203
      if (_parent == null)
81
 33
        _token = container;
82
      else
83
 170
        _parent.Add(container);
84

  
85
 203
      _parent = container;
86
 203
    }
87

  
88
    private void RemoveParent()
89
    {
90
 66
      _parent = _parent.Parent;
91

  
92
 66
      if (_parent != null && _parent.Type == JTokenType.Property)
93
 19
        _parent = _parent.Parent;
94
 66
    }
95

  
96
    /// <summary>
97
    /// Writes the beginning of a Json array.
98
    /// </summary>
99
    public override void WriteStartArray()
100
    {
101
 28
      base.WriteStartArray();
102

  
103
 28
      AddParent(new JArray());
104
 28
    }
105

  
106
    /// <summary>
107
    /// Writes the start of a constructor with the given name.
108
    /// </summary>
109
    /// <param name="name">The name of the constructor.</param>
110
    public override void WriteStartConstructor(string name)
111
    {
112
 0
      base.WriteStartConstructor(name);
113

  
114
 0
      AddParent(new JConstructor(name));
115
 0
    }
116

  
117
    /// <summary>
118
    /// Writes the end.
119
    /// </summary>
120
    /// <param name="token">The token.</param>
121
    protected override void WriteEnd(JsonToken token)
122
    {
123
 66
      RemoveParent();
124
 66
    }
125

  
126
    /// <summary>
127
    /// Writes the property name of a name/value pair on a Json object.
128
    /// </summary>
129
    /// <param name="name">The name of the property.</param>
130
    public override void WritePropertyName(string name)
131
    {
132
 137
      base.WritePropertyName(name);
133

  
134
 137
      AddParent(new JProperty(name));
135
 137
    }
136

  
137
    private void AddValue(object value, JsonToken token)
138
    {
139
 211
      AddValue(new JValue(value), token);
140
 211
    }
141

  
142
    internal void AddValue(JValue value, JsonToken token)
143
    {
144
 227
      if (_parent != null)
145
      {
146
 209
        _parent.Add(value);
147

  
148
 209
        if (_parent.Type == JTokenType.Property)
149
 118
          _parent = _parent.Parent;
150
      }
151
      else
152
      {
153
 18
        _value = value;
154
      }
155
 227
    }
156

  
157
    #region WriteValue methods
158
    /// <summary>
159
    /// Writes a null value.
160
    /// </summary>
161
    public override void WriteNull()
162
    {
163
 6
      base.WriteNull();
164
 6
      AddValue(null, JsonToken.Null);
165
 6
    }
166

  
167
    /// <summary>
168
    /// Writes an undefined value.
169
    /// </summary>
170
    public override void WriteUndefined()
171
    {
172
 1
      base.WriteUndefined();
173
 1
      AddValue(null, JsonToken.Undefined);
174
 1
    }
175

  
176
    /// <summary>
177
    /// Writes raw JSON.
178
    /// </summary>
179
    /// <param name="json">The raw JSON to write.</param>
180
    public override void WriteRaw(string json)
181
    {
182
 7
      base.WriteRaw(json);
183
 7
      AddValue(new JRaw(json), JsonToken.Raw);
184
 7
    }
185

  
186
    /// <summary>
187
    /// Writes out a comment <code>/*...*/</code> containing the specified text.
188
    /// </summary>
189
    /// <param name="text">Text to place inside the comment.</param>
190
    public override void WriteComment(string text)
191
    {
192
 2
      base.WriteComment(text);
193
 2
      AddValue(JValue.CreateComment(text), JsonToken.Comment);
194
 2
    }
195

  
196
    /// <summary>
197
    /// Writes a <see cref="String"/> value.
198
    /// </summary>
199
    /// <param name="value">The <see cref="String"/> value to write.</param>
200
    public override void WriteValue(string value)
201
    {
202
 135
      base.WriteValue(value);
203
 135
      AddValue(value ?? string.Empty, JsonToken.String);
204
 135
    }
205

  
206
    /// <summary>
207
    /// Writes a <see cref="Int32"/> value.
208
    /// </summary>
209
    /// <param name="value">The <see cref="Int32"/> value to write.</param>
210
    public override void WriteValue(int value)
211
    {
212
 17
      base.WriteValue(value);
213
 17
      AddValue(value, JsonToken.Integer);
214
 17
    }
215

  
216
    /// <summary>
217
    /// Writes a <see cref="UInt32"/> value.
218
    /// </summary>
219
    /// <param name="value">The <see cref="UInt32"/> value to write.</param>
220
    public override void WriteValue(uint value)
221
    {
222
 0
      base.WriteValue(value);
223
 0
      AddValue(value, JsonToken.Integer);
224
 0
    }
225

  
226
    /// <summary>
227
    /// Writes a <see cref="Int64"/> value.
228
    /// </summary>
229
    /// <param name="value">The <see cref="Int64"/> value to write.</param>
230
    public override void WriteValue(long value)
231
    {
232
 41
      base.WriteValue(value);
233
 41
      AddValue(value, JsonToken.Integer);
234
 41
    }
235

  
236
    /// <summary>
237
    /// Writes a <see cref="UInt64"/> value.
238
    /// </summary>
239
    /// <param name="value">The <see cref="UInt64"/> value to write.</param>
240
    public override void WriteValue(ulong value)
241
    {
242
 0
      base.WriteValue(value);
243
 0
      AddValue(value, JsonToken.Integer);
244
 0
    }
245

  
246
    /// <summary>
247
    /// Writes a <see cref="Single"/> value.
248
    /// </summary>
249
    /// <param name="value">The <see cref="Single"/> value to write.</param>
250
    public override void WriteValue(float value)
251
    {
252
 0
      base.WriteValue(value);
253
 0
      AddValue(value, JsonToken.Float);
254
 0
    }
255

  
256
    /// <summary>
257
    /// Writes a <see cref="Double"/> value.
258
    /// </summary>
259
    /// <param name="value">The <see cref="Double"/> value to write.</param>
260
    public override void WriteValue(double value)
261
    {
262
 4
      base.WriteValue(value);
263
 4
      AddValue(value, JsonToken.Float);
264
 4
    }
265

  
266
    /// <summary>
267
    /// Writes a <see cref="Boolean"/> value.
268
    /// </summary>
269
    /// <param name="value">The <see cref="Boolean"/> value to write.</param>
270
    public override void WriteValue(bool value)
271
    {
272
 5
      base.WriteValue(value);
273
 5
      AddValue(value, JsonToken.Boolean);
274
 5
    }
275

  
276
    /// <summary>
277
    /// Writes a <see cref="Int16"/> value.
278
    /// </summary>
279
    /// <param name="value">The <see cref="Int16"/> value to write.</param>
280
    public override void WriteValue(short value)
281
    {
282
 0
      base.WriteValue(value);
283
 0
      AddValue(value, JsonToken.Integer);
284
 0
    }
285

  
286
    /// <summary>
287
    /// Writes a <see cref="UInt16"/> value.
288
    /// </summary>
289
    /// <param name="value">The <see cref="UInt16"/> value to write.</param>
290
    public override void WriteValue(ushort value)
291
    {
292
 0
      base.WriteValue(value);
293
 0
      AddValue(value, JsonToken.Integer);
294
 0
    }
295

  
296
    /// <summary>
297
    /// Writes a <see cref="Char"/> value.
298
    /// </summary>
299
    /// <param name="value">The <see cref="Char"/> value to write.</param>
300
    public override void WriteValue(char value)
301
    {
302
 1
      base.WriteValue(value);
303
 1
      AddValue(value.ToString(), JsonToken.String);
304
 1
    }
305

  
306
    /// <summary>
307
    /// Writes a <see cref="Byte"/> value.
308
    /// </summary>
309
    /// <param name="value">The <see cref="Byte"/> value to write.</param>
310
    public override void WriteValue(byte value)
311
    {
312
 0
      base.WriteValue(value);
313
 0
      AddValue(value, JsonToken.Integer);
314
 0
    }
315

  
316
    /// <summary>
317
    /// Writes a <see cref="SByte"/> value.
318
    /// </summary>
319
    /// <param name="value">The <see cref="SByte"/> value to write.</param>
320
    public override void WriteValue(sbyte value)
321
    {
322
 0
      base.WriteValue(value);
323
 0
      AddValue(value, JsonToken.Integer);
324
 0
    }
325

  
326
    /// <summary>
327
    /// Writes a <see cref="Decimal"/> value.
328
    /// </summary>
329
    /// <param name="value">The <see cref="Decimal"/> value to write.</param>
330
    public override void WriteValue(decimal value)
331
    {
332
 1
      base.WriteValue(value);
333
 1
      AddValue(value, JsonToken.Float);
334
 1
    }
335

  
336
    /// <summary>
337
    /// Writes a <see cref="DateTime"/> value.
338
    /// </summary>
339
    /// <param name="value">The <see cref="DateTime"/> value to write.</param>
340
    public override void WriteValue(DateTime value)
341
    {
342
 1
      base.WriteValue(value);
343
 1
      AddValue(value, JsonToken.Date);
344
 1
    }
345

  
346
#if !PocketPC && !NET20
347
    /// <summary>
348
    /// Writes a <see cref="DateTimeOffset"/> value.
349
    /// </summary>
350
    /// <param name="value">The <see cref="DateTimeOffset"/> value to write.</param>
351
    public override void WriteValue(DateTimeOffset value)
352
    {
353
 4
      base.WriteValue(value);
354
 4
      AddValue(value, JsonToken.Date);
355
 4
    }
356
#endif
357

  
358
    /// <summary>
359
    /// Writes a <see cref="T:Byte[]"/> value.
360
    /// </summary>
361
    /// <param name="value">The <see cref="T:Byte[]"/> value to write.</param>
362
    public override void WriteValue(byte[] value)
363
    {
364
 2
      base.WriteValue(value);
365
 2
      AddValue(value, JsonToken.Bytes);
366
 2
    }
367
    #endregion
368
  }
369
}