2 using System.Collections.Generic;
5 using Newtonsoft.Json.Utilities;
7 namespace Newtonsoft.Json.Linq
10 /// Represents a writer that provides a fast, non-cached, forward-only way of generating Json data.
12 public class JTokenWriter : JsonWriter
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;
20 /// Gets the token being writen.
22 /// <value>The token being writen.</value>
35 /// Initializes a new instance of the <see cref="JTokenWriter"/> class writing to the given <see cref="JContainer"/>.
37 /// <param name="container">The container being written to.</param>
38 public JTokenWriter(JContainer container)
40 ValidationUtils.ArgumentNotNull(container, "container");
47 /// Initializes a new instance of the <see cref="JTokenWriter"/> class.
54 /// Flushes whatever is in the buffer to the underlying streams and also flushes the underlying stream.
56 public override void Flush()
61 /// Closes this stream and the underlying stream.
63 public override void Close()
69 /// Writes the beginning of a Json object.
71 public override void WriteStartObject()
73 base.WriteStartObject();
75 AddParent(new JObject());
78 private void AddParent(JContainer container)
83 _parent.Add(container);
88 private void RemoveParent()
90 _parent = _parent.Parent;
92 if (_parent != null && _parent.Type == JTokenType.Property)
93 _parent = _parent.Parent;
97 /// Writes the beginning of a Json array.
99 public override void WriteStartArray()
101 base.WriteStartArray();
103 AddParent(new JArray());
107 /// Writes the start of a constructor with the given name.
109 /// <param name="name">The name of the constructor.</param>
110 public override void WriteStartConstructor(string name)
112 base.WriteStartConstructor(name);
114 AddParent(new JConstructor(name));
120 /// <param name="token">The token.</param>
121 protected override void WriteEnd(JsonToken token)
127 /// Writes the property name of a name/value pair on a Json object.
129 /// <param name="name">The name of the property.</param>
130 public override void WritePropertyName(string name)
132 base.WritePropertyName(name);
134 AddParent(new JProperty(name));
137 private void AddValue(object value, JsonToken token)
139 AddValue(new JValue(value), token);
142 internal void AddValue(JValue value, JsonToken token)
148 if (_parent.Type == JTokenType.Property)
149 _parent = _parent.Parent;
157 #region WriteValue methods
159 /// Writes a null value.
161 public override void WriteNull()
164 AddValue(null, JsonToken.Null);
168 /// Writes an undefined value.
170 public override void WriteUndefined()
172 base.WriteUndefined();
173 AddValue(null, JsonToken.Undefined);
179 /// <param name="json">The raw JSON to write.</param>
180 public override void WriteRaw(string json)
183 AddValue(new JRaw(json), JsonToken.Raw);
187 /// Writes out a comment <code>/*...*/</code> containing the specified text.
189 /// <param name="text">Text to place inside the comment.</param>
190 public override void WriteComment(string text)
192 base.WriteComment(text);
193 AddValue(JValue.CreateComment(text), JsonToken.Comment);
197 /// Writes a <see cref="String"/> value.
199 /// <param name="value">The <see cref="String"/> value to write.</param>
200 public override void WriteValue(string value)
202 base.WriteValue(value);
203 AddValue(value ?? string.Empty, JsonToken.String);
207 /// Writes a <see cref="Int32"/> value.
209 /// <param name="value">The <see cref="Int32"/> value to write.</param>
210 public override void WriteValue(int value)
212 base.WriteValue(value);
213 AddValue(value, JsonToken.Integer);
217 /// Writes a <see cref="UInt32"/> value.
219 /// <param name="value">The <see cref="UInt32"/> value to write.</param>
220 [CLSCompliant(false)]
221 public override void WriteValue(uint value)
223 base.WriteValue(value);
224 AddValue(value, JsonToken.Integer);
228 /// Writes a <see cref="Int64"/> value.
230 /// <param name="value">The <see cref="Int64"/> value to write.</param>
231 public override void WriteValue(long value)
233 base.WriteValue(value);
234 AddValue(value, JsonToken.Integer);
238 /// Writes a <see cref="UInt64"/> value.
240 /// <param name="value">The <see cref="UInt64"/> value to write.</param>
241 [CLSCompliant(false)]
242 public override void WriteValue(ulong value)
244 base.WriteValue(value);
245 AddValue(value, JsonToken.Integer);
249 /// Writes a <see cref="Single"/> value.
251 /// <param name="value">The <see cref="Single"/> value to write.</param>
252 public override void WriteValue(float value)
254 base.WriteValue(value);
255 AddValue(value, JsonToken.Float);
259 /// Writes a <see cref="Double"/> value.
261 /// <param name="value">The <see cref="Double"/> value to write.</param>
262 public override void WriteValue(double value)
264 base.WriteValue(value);
265 AddValue(value, JsonToken.Float);
269 /// Writes a <see cref="Boolean"/> value.
271 /// <param name="value">The <see cref="Boolean"/> value to write.</param>
272 public override void WriteValue(bool value)
274 base.WriteValue(value);
275 AddValue(value, JsonToken.Boolean);
279 /// Writes a <see cref="Int16"/> value.
281 /// <param name="value">The <see cref="Int16"/> value to write.</param>
282 public override void WriteValue(short value)
284 base.WriteValue(value);
285 AddValue(value, JsonToken.Integer);
289 /// Writes a <see cref="UInt16"/> value.
291 /// <param name="value">The <see cref="UInt16"/> value to write.</param>
292 [CLSCompliant(false)]
293 public override void WriteValue(ushort value)
295 base.WriteValue(value);
296 AddValue(value, JsonToken.Integer);
300 /// Writes a <see cref="Char"/> value.
302 /// <param name="value">The <see cref="Char"/> value to write.</param>
303 public override void WriteValue(char value)
305 base.WriteValue(value);
306 AddValue(value.ToString(), JsonToken.String);
310 /// Writes a <see cref="Byte"/> value.
312 /// <param name="value">The <see cref="Byte"/> value to write.</param>
313 public override void WriteValue(byte value)
315 base.WriteValue(value);
316 AddValue(value, JsonToken.Integer);
320 /// Writes a <see cref="SByte"/> value.
322 /// <param name="value">The <see cref="SByte"/> value to write.</param>
323 [CLSCompliant(false)]
324 public override void WriteValue(sbyte value)
326 base.WriteValue(value);
327 AddValue(value, JsonToken.Integer);
331 /// Writes a <see cref="Decimal"/> value.
333 /// <param name="value">The <see cref="Decimal"/> value to write.</param>
334 public override void WriteValue(decimal value)
336 base.WriteValue(value);
337 AddValue(value, JsonToken.Float);
341 /// Writes a <see cref="DateTime"/> value.
343 /// <param name="value">The <see cref="DateTime"/> value to write.</param>
344 public override void WriteValue(DateTime value)
346 base.WriteValue(value);
347 AddValue(value, JsonToken.Date);
350 #if !PocketPC && !NET20
352 /// Writes a <see cref="DateTimeOffset"/> value.
354 /// <param name="value">The <see cref="DateTimeOffset"/> value to write.</param>
355 public override void WriteValue(DateTimeOffset value)
357 base.WriteValue(value);
358 AddValue(value, JsonToken.Date);
363 /// Writes a <see cref="T:Byte[]"/> value.
365 /// <param name="value">The <see cref="T:Byte[]"/> value to write.</param>
366 public override void WriteValue(byte[] value)
368 base.WriteValue(value);
369 AddValue(value, JsonToken.Bytes);