#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Globalization;
#if !(NET20 || NET35 || PORTABLE40 || PORTABLE)
using System.Numerics;
#endif
using Newtonsoft.Json.Utilities;
namespace Newtonsoft.Json.Linq
{
///
/// Represents a writer that provides a fast, non-cached, forward-only way of generating JSON data.
///
public class JTokenWriter : JsonWriter
{
private JContainer _token;
private JContainer _parent;
// used when writer is writing single value and the value has no containing parent
private JValue _value;
private JToken _current;
///
/// Gets the at the writer's current position.
///
public JToken CurrentToken
{
get { return _current; }
}
///
/// Gets the token being writen.
///
/// The token being writen.
public JToken Token
{
get
{
if (_token != null)
{
return _token;
}
return _value;
}
}
///
/// Initializes a new instance of the class writing to the given .
///
/// The container being written to.
public JTokenWriter(JContainer container)
{
ValidationUtils.ArgumentNotNull(container, nameof(container));
_token = container;
_parent = container;
}
///
/// Initializes a new instance of the class.
///
public JTokenWriter()
{
}
///
/// Flushes whatever is in the buffer to the underlying streams and also flushes the underlying stream.
///
public override void Flush()
{
}
///
/// Closes this stream and the underlying stream.
///
public override void Close()
{
base.Close();
}
///
/// Writes the beginning of a JSON object.
///
public override void WriteStartObject()
{
base.WriteStartObject();
AddParent(new JObject());
}
private void AddParent(JContainer container)
{
if (_parent == null)
{
_token = container;
}
else
{
_parent.AddAndSkipParentCheck(container);
}
_parent = container;
_current = container;
}
private void RemoveParent()
{
_current = _parent;
_parent = _parent.Parent;
if (_parent != null && _parent.Type == JTokenType.Property)
{
_parent = _parent.Parent;
}
}
///
/// Writes the beginning of a JSON array.
///
public override void WriteStartArray()
{
base.WriteStartArray();
AddParent(new JArray());
}
///
/// Writes the start of a constructor with the given name.
///
/// The name of the constructor.
public override void WriteStartConstructor(string name)
{
base.WriteStartConstructor(name);
AddParent(new JConstructor(name));
}
///
/// Writes the end.
///
/// The token.
protected override void WriteEnd(JsonToken token)
{
RemoveParent();
}
///
/// Writes the property name of a name/value pair on a JSON object.
///
/// The name of the property.
public override void WritePropertyName(string name)
{
JObject o = _parent as JObject;
if (o != null)
{
// avoid duplicate property name exception
// last property name wins
o.Remove(name);
}
AddParent(new JProperty(name));
// don't set state until after in case of an error
// incorrect state will cause issues if writer is disposed when closing open properties
base.WritePropertyName(name);
}
private void AddValue(object value, JsonToken token)
{
AddValue(new JValue(value), token);
}
internal void AddValue(JValue value, JsonToken token)
{
if (_parent != null)
{
_parent.Add(value);
_current = _parent.Last;
if (_parent.Type == JTokenType.Property)
{
_parent = _parent.Parent;
}
}
else
{
_value = value ?? JValue.CreateNull();
_current = _value;
}
}
#region WriteValue methods
///
/// Writes a value.
/// An error will raised if the value cannot be written as a single JSON token.
///
/// The value to write.
public override void WriteValue(object value)
{
#if !(NET20 || NET35 || PORTABLE || PORTABLE40)
if (value is BigInteger)
{
InternalWriteValue(JsonToken.Integer);
AddValue(value, JsonToken.Integer);
}
else
#endif
{
base.WriteValue(value);
}
}
///
/// Writes a null value.
///
public override void WriteNull()
{
base.WriteNull();
AddValue(null, JsonToken.Null);
}
///
/// Writes an undefined value.
///
public override void WriteUndefined()
{
base.WriteUndefined();
AddValue(null, JsonToken.Undefined);
}
///
/// Writes raw JSON.
///
/// The raw JSON to write.
public override void WriteRaw(string json)
{
base.WriteRaw(json);
AddValue(new JRaw(json), JsonToken.Raw);
}
///
/// Writes out a comment /*...*/ containing the specified text.
///
/// Text to place inside the comment.
public override void WriteComment(string text)
{
base.WriteComment(text);
AddValue(JValue.CreateComment(text), JsonToken.Comment);
}
///
/// Writes a value.
///
/// The value to write.
public override void WriteValue(string value)
{
base.WriteValue(value);
AddValue(value, JsonToken.String);
}
///
/// Writes a value.
///
/// The value to write.
public override void WriteValue(int value)
{
base.WriteValue(value);
AddValue(value, JsonToken.Integer);
}
///
/// Writes a value.
///
/// The value to write.
[CLSCompliant(false)]
public override void WriteValue(uint value)
{
base.WriteValue(value);
AddValue(value, JsonToken.Integer);
}
///
/// Writes a value.
///
/// The value to write.
public override void WriteValue(long value)
{
base.WriteValue(value);
AddValue(value, JsonToken.Integer);
}
///
/// Writes a value.
///
/// The value to write.
[CLSCompliant(false)]
public override void WriteValue(ulong value)
{
base.WriteValue(value);
AddValue(value, JsonToken.Integer);
}
///
/// Writes a value.
///
/// The value to write.
public override void WriteValue(float value)
{
base.WriteValue(value);
AddValue(value, JsonToken.Float);
}
///
/// Writes a value.
///
/// The value to write.
public override void WriteValue(double value)
{
base.WriteValue(value);
AddValue(value, JsonToken.Float);
}
///
/// Writes a value.
///
/// The value to write.
public override void WriteValue(bool value)
{
base.WriteValue(value);
AddValue(value, JsonToken.Boolean);
}
///
/// Writes a value.
///
/// The value to write.
public override void WriteValue(short value)
{
base.WriteValue(value);
AddValue(value, JsonToken.Integer);
}
///
/// Writes a value.
///
/// The value to write.
[CLSCompliant(false)]
public override void WriteValue(ushort value)
{
base.WriteValue(value);
AddValue(value, JsonToken.Integer);
}
///
/// Writes a value.
///
/// The value to write.
public override void WriteValue(char value)
{
base.WriteValue(value);
string s = null;
#if !(DOTNET || PORTABLE40 || PORTABLE)
s = value.ToString(CultureInfo.InvariantCulture);
#else
s = value.ToString();
#endif
AddValue(s, JsonToken.String);
}
///
/// Writes a value.
///
/// The value to write.
public override void WriteValue(byte value)
{
base.WriteValue(value);
AddValue(value, JsonToken.Integer);
}
///
/// Writes a value.
///
/// The value to write.
[CLSCompliant(false)]
public override void WriteValue(sbyte value)
{
base.WriteValue(value);
AddValue(value, JsonToken.Integer);
}
///
/// Writes a value.
///
/// The value to write.
public override void WriteValue(decimal value)
{
base.WriteValue(value);
AddValue(value, JsonToken.Float);
}
///
/// Writes a value.
///
/// The value to write.
public override void WriteValue(DateTime value)
{
base.WriteValue(value);
value = DateTimeUtils.EnsureDateTime(value, DateTimeZoneHandling);
AddValue(value, JsonToken.Date);
}
#if !NET20
///
/// Writes a value.
///
/// The value to write.
public override void WriteValue(DateTimeOffset value)
{
base.WriteValue(value);
AddValue(value, JsonToken.Date);
}
#endif
///
/// Writes a [] value.
///
/// The [] value to write.
public override void WriteValue(byte[] value)
{
base.WriteValue(value);
AddValue(value, JsonToken.Bytes);
}
///
/// Writes a value.
///
/// The value to write.
public override void WriteValue(TimeSpan value)
{
base.WriteValue(value);
AddValue(value, JsonToken.String);
}
///
/// Writes a value.
///
/// The value to write.
public override void WriteValue(Guid value)
{
base.WriteValue(value);
AddValue(value, JsonToken.String);
}
///
/// Writes a value.
///
/// The value to write.
public override void WriteValue(Uri value)
{
base.WriteValue(value);
AddValue(value, JsonToken.String);
}
#endregion
}
}