-Added WriteValue(object) to JsonWriter

-Added JTokenEqualityComparer
-Added additional validation to new IDictionary/ICollection methods on JObject/JArray
-Changed JObject so that properties must have a unique name
-Changed remaining IConvertible calls to use ToXXX methods instead of casting
-Fixed many bugs in new IDictionary/ICollection methods on JObject/JArray
-Fixed AddFirst when no existing children
This commit is contained in:
JamesNK
2008-08-12 11:13:27 +00:00
parent 12ca14261c
commit f9772626b8
22 changed files with 925 additions and 102 deletions
+58 -19
View File
@@ -341,49 +341,88 @@ namespace Newtonsoft.Json
switch (convertible.GetTypeCode())
{
case TypeCode.String:
return ToString((string)convertible);
return ToString(convertible.ToString(CultureInfo.InvariantCulture));
case TypeCode.Char:
return ToString((char)convertible);
return ToString(convertible.ToChar(CultureInfo.InvariantCulture));
case TypeCode.Boolean:
return ToString((bool)convertible);
return ToString(convertible.ToBoolean(CultureInfo.InvariantCulture));
case TypeCode.SByte:
return ToString((sbyte)convertible);
return ToString(convertible.ToSByte(CultureInfo.InvariantCulture));
case TypeCode.Int16:
return ToString((short)convertible);
return ToString(convertible.ToInt16(CultureInfo.InvariantCulture));
case TypeCode.UInt16:
return ToString((ushort)convertible);
return ToString(convertible.ToUInt16(CultureInfo.InvariantCulture));
case TypeCode.Int32:
return ToString((int)convertible);
return ToString(convertible.ToInt32(CultureInfo.InvariantCulture));
case TypeCode.Byte:
return ToString((byte)convertible);
return ToString(convertible.ToByte(CultureInfo.InvariantCulture));
case TypeCode.UInt32:
return ToString((uint)convertible);
return ToString(convertible.ToUInt32(CultureInfo.InvariantCulture));
case TypeCode.Int64:
return ToString((long)convertible);
return ToString(convertible.ToInt64(CultureInfo.InvariantCulture));
case TypeCode.UInt64:
return ToString((ulong)convertible);
return ToString(convertible.ToUInt64(CultureInfo.InvariantCulture));
case TypeCode.Single:
return ToString((float)convertible);
return ToString(convertible.ToSingle(CultureInfo.InvariantCulture));
case TypeCode.Double:
return ToString((double)convertible);
return ToString(convertible.ToDouble(CultureInfo.InvariantCulture));
case TypeCode.DateTime:
return ToString((DateTime)convertible);
return ToString(convertible.ToDateTime(CultureInfo.InvariantCulture));
case TypeCode.Decimal:
return ToString((decimal)convertible);
return ToString(convertible.ToDecimal(CultureInfo.InvariantCulture));
case TypeCode.DBNull:
return Null;
}
}
else if (value is DateTimeOffset)
{
return ToString((DateTimeOffset)value);
}
else if (value is Guid)
{
return ToString((Guid)value);
}
throw new ArgumentException("Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation.".FormatWith(CultureInfo.InvariantCulture, value.GetType()));
}
internal static bool IsJsonPrimitive(object value)
{
if (value == null)
{
return true;
}
else if (value is IConvertible)
{
IConvertible convertible = value as IConvertible;
switch (convertible.GetTypeCode())
{
case TypeCode.String:
case TypeCode.Char:
case TypeCode.Boolean:
case TypeCode.SByte:
case TypeCode.Int16:
case TypeCode.UInt16:
case TypeCode.Int32:
case TypeCode.Byte:
case TypeCode.UInt32:
case TypeCode.Int64:
case TypeCode.UInt64:
case TypeCode.Single:
case TypeCode.Double:
case TypeCode.DateTime:
case TypeCode.Decimal:
case TypeCode.DBNull:
return true;
default:
return false;
}
}
else if (value is DateTimeOffset)
{
return true;
}
return false;
}
/// <summary>
/// Serializes the specified object to a JSON string.
/// </summary>
+2 -61
View File
@@ -645,68 +645,9 @@ namespace Newtonsoft.Json
{
converter.WriteJson(writer, value);
}
else if (value is IConvertible)
else if (JavaScriptConvert.IsJsonPrimitive(value))
{
IConvertible convertible = value as IConvertible;
switch (convertible.GetTypeCode())
{
case TypeCode.String:
writer.WriteValue(convertible.ToString(CultureInfo.InvariantCulture));
break;
case TypeCode.Char:
writer.WriteValue(convertible.ToChar(CultureInfo.InvariantCulture));
break;
case TypeCode.Boolean:
writer.WriteValue(convertible.ToBoolean(CultureInfo.InvariantCulture));
break;
case TypeCode.SByte:
writer.WriteValue(convertible.ToSByte(CultureInfo.InvariantCulture));
break;
case TypeCode.Int16:
writer.WriteValue(convertible.ToInt16(CultureInfo.InvariantCulture));
break;
case TypeCode.UInt16:
writer.WriteValue(convertible.ToUInt16(CultureInfo.InvariantCulture));
break;
case TypeCode.Int32:
writer.WriteValue(convertible.ToInt32(CultureInfo.InvariantCulture));
break;
case TypeCode.Byte:
writer.WriteValue(convertible.ToByte(CultureInfo.InvariantCulture));
break;
case TypeCode.UInt32:
writer.WriteValue(convertible.ToUInt32(CultureInfo.InvariantCulture));
break;
case TypeCode.Int64:
writer.WriteValue(convertible.ToInt64(CultureInfo.InvariantCulture));
break;
case TypeCode.UInt64:
writer.WriteValue(convertible.ToUInt64(CultureInfo.InvariantCulture));
break;
case TypeCode.Single:
writer.WriteValue(convertible.ToSingle(CultureInfo.InvariantCulture));
break;
case TypeCode.Double:
writer.WriteValue(convertible.ToDouble(CultureInfo.InvariantCulture));
break;
case TypeCode.DateTime:
writer.WriteValue(convertible.ToDateTime(CultureInfo.InvariantCulture));
break;
case TypeCode.Decimal:
writer.WriteValue(convertible.ToDecimal(CultureInfo.InvariantCulture));
break;
case TypeCode.DBNull:
writer.WriteNull();
break;
default:
SerializeObject(writer, value);
break;
}
}
else if (value is DateTimeOffset)
{
writer.WriteValue((DateTimeOffset)value);
writer.WriteValue(value);
}
else if (value is IList)
{
+76
View File
@@ -736,6 +736,82 @@ namespace Newtonsoft.Json
{
AutoComplete(JsonToken.Date);
}
/// <summary>
/// Writes a <see cref="Object"/> value.
/// An error will raised if the value cannot be written as a single JSON token.
/// </summary>
/// <param name="value">The <see cref="Object"/> value to write.</param>
public virtual void WriteValue(object value)
{
if (value == null)
{
WriteNull();
}
else if (value is IConvertible)
{
IConvertible convertible = value as IConvertible;
switch (convertible.GetTypeCode())
{
case TypeCode.String:
WriteValue(convertible.ToString(CultureInfo.InvariantCulture));
return;
case TypeCode.Char:
WriteValue(convertible.ToChar(CultureInfo.InvariantCulture));
return;
case TypeCode.Boolean:
WriteValue(convertible.ToBoolean(CultureInfo.InvariantCulture));
return;
case TypeCode.SByte:
WriteValue(convertible.ToSByte(CultureInfo.InvariantCulture));
return;
case TypeCode.Int16:
WriteValue(convertible.ToInt16(CultureInfo.InvariantCulture));
return;
case TypeCode.UInt16:
WriteValue(convertible.ToUInt16(CultureInfo.InvariantCulture));
return;
case TypeCode.Int32:
WriteValue(convertible.ToInt32(CultureInfo.InvariantCulture));
return;
case TypeCode.Byte:
WriteValue(convertible.ToByte(CultureInfo.InvariantCulture));
return;
case TypeCode.UInt32:
WriteValue(convertible.ToUInt32(CultureInfo.InvariantCulture));
return;
case TypeCode.Int64:
WriteValue(convertible.ToInt64(CultureInfo.InvariantCulture));
return;
case TypeCode.UInt64:
WriteValue(convertible.ToUInt64(CultureInfo.InvariantCulture));
return;
case TypeCode.Single:
WriteValue(convertible.ToSingle(CultureInfo.InvariantCulture));
return;
case TypeCode.Double:
WriteValue(convertible.ToDouble(CultureInfo.InvariantCulture));
return;
case TypeCode.DateTime:
WriteValue(convertible.ToDateTime(CultureInfo.InvariantCulture));
return;
case TypeCode.Decimal:
WriteValue(convertible.ToDecimal(CultureInfo.InvariantCulture));
return;
case TypeCode.DBNull:
WriteNull();
return;
}
}
else if (value is DateTimeOffset)
{
WriteValue((DateTimeOffset)value);
return;
}
throw new ArgumentException("Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation.".FormatWith(CultureInfo.InvariantCulture, value.GetType()));
}
#endregion
/// <summary>
+31 -5
View File
@@ -220,12 +220,22 @@ namespace Newtonsoft.Json.Linq
public void Insert(int index, JToken item)
{
JToken token = GetIndex(this, index);
AddInternal(false, token.Previous, item);
if (index == 0)
{
AddFirst(item);
}
else
{
JToken token = GetIndex(this, index);
AddInternal(false, token.Previous, item);
}
}
public void RemoveAt(int index)
{
if (index < 0)
throw new ArgumentOutOfRangeException("index", "index is less than 0.");
int currentIndex = 0;
foreach (JToken token in Children())
{
@@ -237,18 +247,20 @@ namespace Newtonsoft.Json.Linq
currentIndex++;
}
throw new ArgumentOutOfRangeException("index", "index is equal to or greater than Count.");
}
#endregion
#region ICollection<JToken> Members
void ICollection<JToken>.Add(JToken item)
public void Add(JToken item)
{
Add((object)item);
}
void ICollection<JToken>.Clear()
public void Clear()
{
RemoveAll();
}
@@ -260,6 +272,15 @@ namespace Newtonsoft.Json.Linq
void ICollection<JToken>.CopyTo(JToken[] array, int arrayIndex)
{
if (array == null)
throw new ArgumentNullException("array");
if (arrayIndex < 0)
throw new ArgumentOutOfRangeException("arrayIndex", "arrayIndex is less than 0.");
if (arrayIndex >= array.Length)
throw new ArgumentException("arrayIndex is equal to or greater than the length of array.");
if (Count > array.Length - arrayIndex)
throw new ArgumentException("The number of elements in the source JObject is greater than the available space from arrayIndex to the end of the destination array.");
int index = 0;
foreach (JToken token in Children())
{
@@ -278,7 +299,7 @@ namespace Newtonsoft.Json.Linq
get { return false; }
}
bool ICollection<JToken>.Remove(JToken item)
public bool Remove(JToken item)
{
if (!((ICollection<JToken>)this).Contains(item))
return false;
@@ -288,5 +309,10 @@ namespace Newtonsoft.Json.Linq
}
#endregion
internal override int GetDeepHashCode()
{
return ContentsHashCode();
}
}
}
+7 -7
View File
@@ -58,13 +58,6 @@ namespace Newtonsoft.Json.Linq
get { return JsonTokenType.Constructor; }
}
/// <summary>
/// Initializes a new instance of the <see cref="JConstructor"/> class.
/// </summary>
public JConstructor()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="JConstructor"/> class from another <see cref="JConstructor"/> object.
/// </summary>
@@ -102,6 +95,8 @@ namespace Newtonsoft.Json.Linq
/// <param name="name">The constructor name.</param>
public JConstructor(string name)
{
ValidationUtils.ArgumentNotNullOrEmpty(name, "name");
_name = name;
}
@@ -160,5 +155,10 @@ namespace Newtonsoft.Json.Linq
return GetIndex(this, (int)key);
}
}
internal override int GetDeepHashCode()
{
return _name.GetHashCode() ^ ContentsHashCode();
}
}
}
+13 -4
View File
@@ -261,7 +261,7 @@ namespace Newtonsoft.Json.Linq
if (previous != null)
previous.Next = o;
if (isLast)
if (isLast || previous == null)
_content = o;
}
}
@@ -271,7 +271,7 @@ namespace Newtonsoft.Json.Linq
ValidationUtils.ArgumentNotNull(o, "o");
if (o.Type == JsonTokenType.Property)
throw new Exception("Can not add {0} to {1}".FormatWith(CultureInfo.InvariantCulture, o.GetType(), GetType()));
throw new Exception("Can not add {0} to {1}.".FormatWith(CultureInfo.InvariantCulture, o.GetType(), GetType()));
}
/// <summary>
@@ -422,8 +422,7 @@ namespace Newtonsoft.Json.Linq
parent = parent.Parent;
break;
case JsonToken.StartConstructor:
JConstructor constructor = new JConstructor();
constructor.Name = r.Value.ToString();
JConstructor constructor = new JConstructor(r.Value.ToString());
parent.AddObjectSkipNotify(constructor);
parent = constructor;
break;
@@ -460,5 +459,15 @@ namespace Newtonsoft.Json.Linq
}
while (r.Read());
}
internal int ContentsHashCode()
{
int hashCode = 0;
foreach (JToken item in Children())
{
hashCode ^= item.GetDeepHashCode();
}
return hashCode;
}
}
}
+21 -1
View File
@@ -83,7 +83,12 @@ namespace Newtonsoft.Json.Linq
ValidationUtils.ArgumentNotNull(o, "o");
if (o.Type != JsonTokenType.Property)
throw new Exception("Can not add {0} to {1}".FormatWith(CultureInfo.InvariantCulture, o.GetType(), GetType()));
throw new Exception("Can not add {0} to {1}.".FormatWith(CultureInfo.InvariantCulture, o.GetType(), GetType()));
JProperty property = (JProperty)o;
bool matchingProperty = (Properties().Where(p => string.Compare(p.Name, property.Name, StringComparison.Ordinal) == 0).Count() > 0);
if (matchingProperty)
throw new Exception("Can not add property {0} to {1}. Property with the same name already exists on object.".FormatWith(CultureInfo.InvariantCulture, property.Name, GetType()));
}
internal override JToken CloneNode()
@@ -319,10 +324,20 @@ namespace Newtonsoft.Json.Linq
void ICollection<KeyValuePair<string,JToken>>.CopyTo(KeyValuePair<string, JToken>[] array, int arrayIndex)
{
if (array == null)
throw new ArgumentNullException("array");
if (arrayIndex < 0)
throw new ArgumentOutOfRangeException("arrayIndex", "arrayIndex is less than 0.");
if (arrayIndex >= array.Length)
throw new ArgumentException("arrayIndex is equal to or greater than the length of array.");
if (Count > array.Length - arrayIndex)
throw new ArgumentException("The number of elements in the source JObject is greater than the available space from arrayIndex to the end of the destination array.");
int index = 0;
foreach (JProperty property in Properties())
{
array[arrayIndex + index] = new KeyValuePair<string, JToken>(property.Name, property.Value);
index++;
}
}
@@ -358,5 +373,10 @@ namespace Newtonsoft.Json.Linq
}
#endregion
internal override int GetDeepHashCode()
{
return ContentsHashCode();
}
}
}
+6 -1
View File
@@ -110,7 +110,7 @@ namespace Newtonsoft.Json.Linq
/// <param name="value">The property value.</param>
public JProperty(string name, object value)
{
ValidationUtils.ArgumentNotNullOrEmpty(name, "name");
ValidationUtils.ArgumentNotNull(name, "name");
_name = name;
Value = CreateFromContent(value);
@@ -149,5 +149,10 @@ namespace Newtonsoft.Json.Linq
// return (JValue)value;
//}
internal override int GetDeepHashCode()
{
return _name.GetHashCode() ^ ((Value != null) ? Value.GetDeepHashCode() : 0);
}
}
}
+14
View File
@@ -42,6 +42,18 @@ namespace Newtonsoft.Json.Linq
{
private JContainer _parent;
internal JToken _next;
private static JTokenEqualityComparer _equalityComparer;
public static JTokenEqualityComparer EqualityComparer
{
get
{
if (_equalityComparer == null)
_equalityComparer = new JTokenEqualityComparer();
return _equalityComparer;
}
}
/// <summary>
/// Gets or sets the parent.
@@ -705,5 +717,7 @@ namespace Newtonsoft.Json.Linq
{
return Children().GetEnumerator();
}
internal abstract int GetDeepHashCode();
}
}
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Newtonsoft.Json.Linq
{
public class JTokenEqualityComparer : IEqualityComparer<JToken>
{
public bool Equals(JToken x, JToken y)
{
return JToken.DeepEquals(x, y);
}
public int GetHashCode(JToken obj)
{
if (obj == null)
return 0;
return obj.GetDeepHashCode();
}
}
}
+7
View File
@@ -289,5 +289,12 @@ namespace Newtonsoft.Json.Linq
throw MiscellaneousUtils.CreateArgumentOutOfRangeException("TokenType", _valueType, "Unexpected token type.");
}
}
internal override int GetDeepHashCode()
{
int valueHashCode = (_value != null) ? _value.GetHashCode() : 0;
return _valueType.GetHashCode() ^ valueHashCode;
}
}
}
+1 -1
View File
@@ -103,7 +103,7 @@ namespace Newtonsoft.Json.Linq
{
base.WriteStartConstructor(name);
AddParent(new JConstructor());
AddParent(new JConstructor(name));
}
/// <summary>
@@ -76,6 +76,7 @@
<ItemGroup>
<Compile Include="JsonConverterAttribute.cs" />
<Compile Include="JsonObjectAttribute.cs" />
<Compile Include="Linq\JTokenEqualityComparer.cs" />
<Compile Include="MemberSerialization.cs" />
<Compile Include="ObjectCreationHandling.cs" />
<Compile Include="Converters\IsoDateTimeConverter.cs" />