Change - Heaps of small changes and bug fixes around LINQ to JSON. Many unit tests added.
Change - CultureInfo is now always passed into FormatWith
This commit is contained in:
@@ -49,7 +49,7 @@ namespace Newtonsoft.Json.Converters
|
||||
public override object ReadJson(JsonReader reader, Type objectType)
|
||||
{
|
||||
if (reader.TokenType != JsonToken.String)
|
||||
throw new Exception("Unexpected token parsing date. Expected String, got {0}.".FormatWith(reader.TokenType));
|
||||
throw new Exception("Unexpected token parsing date. Expected String, got {0}.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
|
||||
|
||||
string dateText = reader.Value.ToString();
|
||||
|
||||
|
||||
@@ -34,12 +34,12 @@ namespace Newtonsoft.Json.Converters
|
||||
public override object ReadJson(JsonReader reader, Type objectType)
|
||||
{
|
||||
if (reader.TokenType != JsonToken.StartConstructor || string.Compare(reader.Value.ToString(), "Date", StringComparison.Ordinal) != 0)
|
||||
throw new Exception("Unexpected token or value when parsing date. Token: {0}, Value: {1}".FormatWith(reader.TokenType, reader.Value));
|
||||
throw new Exception("Unexpected token or value when parsing date. Token: {0}, Value: {1}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType, reader.Value));
|
||||
|
||||
reader.Read();
|
||||
|
||||
if (reader.TokenType != JsonToken.Integer)
|
||||
throw new Exception("Unexpected token parsing date. Expected Integer, got {0}.".FormatWith(reader.TokenType));
|
||||
throw new Exception("Unexpected token parsing date. Expected Integer, got {0}.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
|
||||
|
||||
long ticks = (long)reader.Value;
|
||||
|
||||
@@ -48,7 +48,7 @@ namespace Newtonsoft.Json.Converters
|
||||
reader.Read();
|
||||
|
||||
if (reader.TokenType != JsonToken.EndConstructor)
|
||||
throw new Exception("Unexpected token parsing date. Expected EndConstructor, got {0}.".FormatWith(reader.TokenType));
|
||||
throw new Exception("Unexpected token parsing date. Expected EndConstructor, got {0}.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
|
||||
|
||||
if (objectType == typeof(DateTimeOffset))
|
||||
return new DateTimeOffset(d);
|
||||
|
||||
@@ -384,7 +384,7 @@ namespace Newtonsoft.Json
|
||||
return ToString((Guid)value);
|
||||
}
|
||||
|
||||
throw new ArgumentException("Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation.".FormatWith(value.GetType()));
|
||||
throw new ArgumentException("Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation.".FormatWith(CultureInfo.InvariantCulture, value.GetType()));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -239,7 +239,7 @@ namespace Newtonsoft.Json
|
||||
JsonTokenType currentObject = Pop();
|
||||
|
||||
if (GetTypeForCloseToken(endToken) != currentObject)
|
||||
throw new JsonReaderException("JsonToken {0} is not valid for closing JsonType {1}.".FormatWith(endToken, currentObject));
|
||||
throw new JsonReaderException("JsonToken {0} is not valid for closing JsonType {1}.".FormatWith(CultureInfo.InvariantCulture, endToken, currentObject));
|
||||
}
|
||||
|
||||
protected void SetStateBasedOnCurrent()
|
||||
|
||||
@@ -290,7 +290,7 @@ namespace Newtonsoft.Json
|
||||
return new DateTimeOffset((DateTime)value);
|
||||
|
||||
if (!targetType.IsAssignableFrom(valueType))
|
||||
throw new InvalidOperationException("Cannot convert object of type '{0}' to type '{1}'".FormatWith(value.GetType(), targetType));
|
||||
throw new InvalidOperationException("Cannot convert object of type '{0}' to type '{1}'".FormatWith(CultureInfo.InvariantCulture, value.GetType(), targetType));
|
||||
|
||||
return value;
|
||||
}
|
||||
@@ -369,7 +369,7 @@ namespace Newtonsoft.Json
|
||||
private void SetObjectMember(JsonReader reader, object target, Type targetType, string memberName)
|
||||
{
|
||||
if (!reader.Read())
|
||||
throw new JsonSerializationException("Unexpected end when setting {0}'s value.".FormatWith(memberName));
|
||||
throw new JsonSerializationException("Unexpected end when setting {0}'s value.".FormatWith(CultureInfo.InvariantCulture, memberName));
|
||||
|
||||
MemberMappingCollection memberMappings = GetMemberMappings(targetType);
|
||||
Type memberType;
|
||||
@@ -413,7 +413,7 @@ namespace Newtonsoft.Json
|
||||
else
|
||||
{
|
||||
if (_missingMemberHandling == MissingMemberHandling.Error)
|
||||
throw new JsonSerializationException("Could not find member '{0}' on object of type '{1}'".FormatWith(memberName, targetType.GetType().Name));
|
||||
throw new JsonSerializationException("Could not find member '{0}' on object of type '{1}'".FormatWith(CultureInfo.InvariantCulture, memberName, targetType.GetType().Name));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -511,7 +511,7 @@ namespace Newtonsoft.Json
|
||||
.SingleOrDefault();
|
||||
|
||||
if (!reader.Read())
|
||||
throw new JsonSerializationException("Unexpected end when setting {0}'s value.".FormatWith(memberName));
|
||||
throw new JsonSerializationException("Unexpected end when setting {0}'s value.".FormatWith(CultureInfo.InvariantCulture, memberName));
|
||||
|
||||
if (matchingConstructorParameter != null)
|
||||
constructorParameters[matchingConstructorParameter] = CreateObject(reader, matchingConstructorParameter.ParameterType, null);
|
||||
@@ -731,7 +731,7 @@ namespace Newtonsoft.Json
|
||||
// continue
|
||||
break;
|
||||
default:
|
||||
throw new InvalidOperationException("Unexpected ReferenceLoopHandling value: '{0}'".FormatWith(_referenceLoopHandling));
|
||||
throw new InvalidOperationException("Unexpected ReferenceLoopHandling value: '{0}'".FormatWith(CultureInfo.InvariantCulture, _referenceLoopHandling));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -30,6 +30,7 @@ using System.IO;
|
||||
using System.Xml;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Newtonsoft.Json
|
||||
{
|
||||
@@ -513,7 +514,7 @@ namespace Newtonsoft.Json
|
||||
State newState = stateArray[token, (int)_currentState];
|
||||
|
||||
if (newState == State.Error)
|
||||
throw new JsonWriterException("Token {0} in state {1} would result in an invalid JavaScript object.".FormatWith(tokenBeingWritten.ToString(), _currentState.ToString()));
|
||||
throw new JsonWriterException("Token {0} in state {1} would result in an invalid JavaScript object.".FormatWith(CultureInfo.InvariantCulture, tokenBeingWritten.ToString(), _currentState.ToString()));
|
||||
|
||||
if ((_currentState == State.Object || _currentState == State.Array || _currentState == State.Constructor) && tokenBeingWritten != JsonToken.Comment)
|
||||
{
|
||||
|
||||
@@ -142,6 +142,11 @@ namespace Newtonsoft.Json.Linq
|
||||
// return source.SelectMany(c => c.Children());
|
||||
//}
|
||||
|
||||
public static IEnumerable<JToken> Children(this IEnumerable<JArray> source)
|
||||
{
|
||||
return Children<JToken>(source);
|
||||
}
|
||||
|
||||
public static IEnumerable<U> Children<U>(this IEnumerable<JArray> source)
|
||||
{
|
||||
ValidationUtils.ArgumentNotNull(source, "source");
|
||||
@@ -182,7 +187,7 @@ namespace Newtonsoft.Json.Linq
|
||||
|
||||
JValue value = token as JValue;
|
||||
if (value == null)
|
||||
throw new InvalidCastException("Cannot cast {0} to {1}.".FormatWith(token.GetType(), typeof(T)));
|
||||
throw new InvalidCastException("Cannot cast {0} to {1}.".FormatWith(CultureInfo.InvariantCulture, token.GetType(), typeof(T)));
|
||||
|
||||
return (U)System.Convert.ChangeType(value.Value, typeof(U), CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
using System.IO;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Newtonsoft.Json.Linq
|
||||
{
|
||||
@@ -109,7 +110,7 @@ namespace Newtonsoft.Json.Linq
|
||||
ValidationUtils.ArgumentNotNull(o, "o");
|
||||
|
||||
if (o.Type == JsonTokenType.Property)
|
||||
throw new ArgumentException("An item of type {0} cannot be added to content.".FormatWith(o.Type));
|
||||
throw new ArgumentException("An item of type {0} cannot be added to content.".FormatWith(CultureInfo.InvariantCulture, o.Type));
|
||||
}
|
||||
|
||||
public static JArray FromObject(object o)
|
||||
@@ -117,7 +118,7 @@ namespace Newtonsoft.Json.Linq
|
||||
JToken token = FromObjectInternal(o);
|
||||
|
||||
if (token.Type != JsonTokenType.Array)
|
||||
throw new ArgumentException("Object serialized to {0}. JArray instance expected.".FormatWith(token.Type));
|
||||
throw new ArgumentException("Object serialized to {0}. JArray instance expected.".FormatWith(CultureInfo.InvariantCulture, token.Type));
|
||||
|
||||
return (JArray)token;
|
||||
}
|
||||
@@ -141,7 +142,7 @@ namespace Newtonsoft.Json.Linq
|
||||
ValidationUtils.ArgumentNotNull(key, "o");
|
||||
|
||||
if (!(key is int))
|
||||
throw new ArgumentException("Accessed JArray values with invalid key value: {0}. Array position index expected.".FormatWith(MiscellaneousUtils.ToString(key)));
|
||||
throw new ArgumentException("Accessed JArray values with invalid key value: {0}. Array position index expected.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(key)));
|
||||
|
||||
return GetIndex(this, (int)key);
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Newtonsoft.Json.Linq
|
||||
{
|
||||
@@ -53,6 +54,7 @@ namespace Newtonsoft.Json.Linq
|
||||
public JConstructor(JConstructor other)
|
||||
: base(other)
|
||||
{
|
||||
_name = other.Name;
|
||||
}
|
||||
|
||||
public JConstructor(string name, params object[] content)
|
||||
@@ -89,7 +91,7 @@ namespace Newtonsoft.Json.Linq
|
||||
switch (o.Type)
|
||||
{
|
||||
case JsonTokenType.Property:
|
||||
throw new ArgumentException("An item of type {0} cannot be added to content.".FormatWith(o.Type));
|
||||
throw new ArgumentException("An item of type {0} cannot be added to content.".FormatWith(CultureInfo.InvariantCulture, o.Type));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,7 +114,7 @@ namespace Newtonsoft.Json.Linq
|
||||
ValidationUtils.ArgumentNotNull(key, "o");
|
||||
|
||||
if (!(key is int))
|
||||
throw new ArgumentException("Accessed JConstructor values with invalid key value: {0}. Argument position index expected.".FormatWith(MiscellaneousUtils.ToString(key)));
|
||||
throw new ArgumentException("Accessed JConstructor values with invalid key value: {0}. Argument position index expected.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(key)));
|
||||
|
||||
return GetIndex(this, (int)key);
|
||||
}
|
||||
|
||||
@@ -30,6 +30,7 @@ using System.Text;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
using System.Collections;
|
||||
using System.Diagnostics;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Newtonsoft.Json.Linq
|
||||
{
|
||||
@@ -56,13 +57,18 @@ namespace Newtonsoft.Json.Linq
|
||||
{
|
||||
do
|
||||
{
|
||||
content = content.Next;
|
||||
content = content._next;
|
||||
Add(content.CloneNode());
|
||||
}
|
||||
while (content != other.Last);
|
||||
}
|
||||
}
|
||||
|
||||
public override bool HasValues
|
||||
{
|
||||
get { return (_content != null); }
|
||||
}
|
||||
|
||||
internal bool ContentsEqual(JContainer container)
|
||||
{
|
||||
JToken t1 = First;
|
||||
@@ -96,7 +102,7 @@ namespace Newtonsoft.Json.Linq
|
||||
if (Last == null)
|
||||
return null;
|
||||
|
||||
return Last.Next;
|
||||
return Last._next;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,7 +127,9 @@ namespace Newtonsoft.Json.Linq
|
||||
do
|
||||
{
|
||||
yield return current;
|
||||
} while ((current = current.Next) != first);
|
||||
}
|
||||
while (current != null
|
||||
&& ((current = current.Next) != null));
|
||||
}
|
||||
|
||||
public override IEnumerable<T> Values<T>()
|
||||
@@ -178,14 +186,17 @@ namespace Newtonsoft.Json.Linq
|
||||
return (content is IEnumerable && !(content is string));
|
||||
}
|
||||
|
||||
public virtual void Add(object content)
|
||||
internal void AddInternal(bool isLast, JToken previous, object content)
|
||||
{
|
||||
if (IsMultiContent(content))
|
||||
{
|
||||
IEnumerable enumerable = content as IEnumerable;
|
||||
|
||||
JToken multiPrevious = previous;
|
||||
foreach (object c in enumerable)
|
||||
{
|
||||
Add(c);
|
||||
AddInternal(isLast, multiPrevious, c);
|
||||
multiPrevious = (previous != null) ? previous._next : Last;
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -211,46 +222,48 @@ namespace Newtonsoft.Json.Linq
|
||||
}
|
||||
}
|
||||
|
||||
JToken last = Last;
|
||||
JToken next = First ?? o;
|
||||
JToken next = (previous != null) ? previous._next : o;
|
||||
|
||||
o.Parent = this;
|
||||
o.Next = next;
|
||||
|
||||
if (last != null)
|
||||
last.Next = o;
|
||||
_content = o;
|
||||
if (previous != null)
|
||||
previous.Next = o;
|
||||
|
||||
if (isLast)
|
||||
_content = o;
|
||||
}
|
||||
}
|
||||
|
||||
internal virtual void ValidateToken(JToken o)
|
||||
{
|
||||
//throw new NotImplementedException();
|
||||
ValidationUtils.ArgumentNotNull(o, "o");
|
||||
|
||||
if (o.Type == JsonTokenType.Property)
|
||||
throw new Exception("Can not add {0} to {1}".FormatWith(CultureInfo.InvariantCulture, o.GetType(), GetType()));
|
||||
}
|
||||
|
||||
public virtual void Add(object content)
|
||||
{
|
||||
AddInternal(true, Last, content);
|
||||
}
|
||||
|
||||
public void AddFirst(object content)
|
||||
{
|
||||
JToken o = CreateFromContent(content);
|
||||
|
||||
JToken last = Parent.Last;
|
||||
|
||||
o.Parent = Parent;
|
||||
o.Next = last.Next;
|
||||
|
||||
last.Next = o;
|
||||
AddInternal(false, Last, content);
|
||||
}
|
||||
|
||||
protected JToken CreateFromContent(object content)
|
||||
{
|
||||
if (content is JToken)
|
||||
return (JToken)content;
|
||||
|
||||
return new JValue(content);
|
||||
else
|
||||
return new JValue(content);
|
||||
}
|
||||
|
||||
public JsonWriter CreateWriter()
|
||||
{
|
||||
return null;
|
||||
return new JsonTokenWriter(this);
|
||||
}
|
||||
|
||||
public void ReplaceAll(object content)
|
||||
@@ -265,17 +278,17 @@ namespace Newtonsoft.Json.Linq
|
||||
{
|
||||
JToken o = _content;
|
||||
|
||||
JToken next = o.Next;
|
||||
if (o != _content || next != o.Next)
|
||||
JToken next = o._next;
|
||||
if (o != _content || next != o._next)
|
||||
throw new InvalidOperationException("This operation was corrupted by external code.");
|
||||
|
||||
if (next != o)
|
||||
o.Next = next.Next;
|
||||
o._next = next._next;
|
||||
else
|
||||
_content = null;
|
||||
|
||||
next.Parent = null;
|
||||
next.Next = null;
|
||||
next._next = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -286,12 +299,13 @@ namespace Newtonsoft.Json.Linq
|
||||
|
||||
JToken content = _content;
|
||||
|
||||
while (_content.Next != o)
|
||||
while (content._next != o)
|
||||
{
|
||||
content = content.Next;
|
||||
content = content._next;
|
||||
}
|
||||
if (content == o)
|
||||
{
|
||||
// token is containers last child
|
||||
_content = null;
|
||||
}
|
||||
else
|
||||
@@ -300,7 +314,7 @@ namespace Newtonsoft.Json.Linq
|
||||
{
|
||||
_content = content;
|
||||
}
|
||||
content.Next = o.Next;
|
||||
content._next = o._next;
|
||||
}
|
||||
o.Parent = null;
|
||||
o.Next = null;
|
||||
@@ -391,7 +405,7 @@ namespace Newtonsoft.Json.Linq
|
||||
parent = property;
|
||||
break;
|
||||
default:
|
||||
throw new InvalidOperationException("The JsonReader should not be on a token of type {0}.".FormatWith(r.TokenType));
|
||||
throw new InvalidOperationException("The JsonReader should not be on a token of type {0}.".FormatWith(CultureInfo.InvariantCulture, r.TokenType));
|
||||
}
|
||||
}
|
||||
while (r.Read());
|
||||
|
||||
@@ -29,6 +29,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Newtonsoft.Json.Linq
|
||||
{
|
||||
@@ -59,6 +60,14 @@ namespace Newtonsoft.Json.Linq
|
||||
return (t != null && ContentsEqual(t));
|
||||
}
|
||||
|
||||
internal override void ValidateToken(JToken o)
|
||||
{
|
||||
ValidationUtils.ArgumentNotNull(o, "o");
|
||||
|
||||
if (o.Type != JsonTokenType.Property)
|
||||
throw new Exception("Can not add {0} to {1}".FormatWith(CultureInfo.InvariantCulture, o.GetType(), GetType()));
|
||||
}
|
||||
|
||||
internal override JToken CloneNode()
|
||||
{
|
||||
return new JObject(this);
|
||||
@@ -86,15 +95,15 @@ namespace Newtonsoft.Json.Linq
|
||||
return new JEnumerable<JToken>(Properties().Select(p => p.Value));
|
||||
}
|
||||
|
||||
public override void Add(object content)
|
||||
{
|
||||
ValidationUtils.ArgumentNotNull(content, "content");
|
||||
//public override void Add(object content)
|
||||
//{
|
||||
// ValidationUtils.ArgumentNotNull(content, "content");
|
||||
|
||||
if (!(content is JProperty) && !IsMultiContent(content))
|
||||
throw new ArgumentException("Error adding {0} to JObject. JObject only supports JProperty content.".FormatWith(content.GetType().Name));
|
||||
// if (!(content is JProperty) && !IsMultiContent(content))
|
||||
// throw new ArgumentException("Error adding {0} to JObject. JObject only supports JProperty content.".FormatWith(CultureInfo.InvariantCulture, content.GetType().Name));
|
||||
|
||||
base.Add(content);
|
||||
}
|
||||
// base.Add(content);
|
||||
//}
|
||||
|
||||
public override JToken this[object key]
|
||||
{
|
||||
@@ -104,7 +113,7 @@ namespace Newtonsoft.Json.Linq
|
||||
|
||||
string propertyName = key as string;
|
||||
if (propertyName == null)
|
||||
throw new ArgumentException("Accessed JObject values with invalid key value: {0}. Object property name expected.".FormatWith(MiscellaneousUtils.ToString(key)));
|
||||
throw new ArgumentException("Accessed JObject values with invalid key value: {0}. Object property name expected.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(key)));
|
||||
|
||||
JProperty property = Property(propertyName);
|
||||
|
||||
@@ -149,7 +158,7 @@ namespace Newtonsoft.Json.Linq
|
||||
JToken token = FromObjectInternal(o);
|
||||
|
||||
if (token.Type != JsonTokenType.Object)
|
||||
throw new ArgumentException("Object serialized to {0}. JObject instance expected.".FormatWith(token.Type));
|
||||
throw new ArgumentException("Object serialized to {0}. JObject instance expected.".FormatWith(CultureInfo.InvariantCulture, token.Type));
|
||||
|
||||
return (JObject)token;
|
||||
}
|
||||
@@ -159,7 +168,7 @@ namespace Newtonsoft.Json.Linq
|
||||
ValidationUtils.ArgumentNotNull(o, "o");
|
||||
|
||||
if (o.Type != JsonTokenType.Property)
|
||||
throw new ArgumentException("An item of type {0} cannot be added to content.".FormatWith(o.Type));
|
||||
throw new ArgumentException("An item of type {0} cannot be added to content.".FormatWith(CultureInfo.InvariantCulture, o.Type));
|
||||
}
|
||||
|
||||
public override void WriteTo(JsonWriter writer, params JsonConverter[] converters)
|
||||
|
||||
@@ -29,6 +29,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
using System.Diagnostics;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Newtonsoft.Json.Linq
|
||||
{
|
||||
@@ -95,7 +96,7 @@ namespace Newtonsoft.Json.Linq
|
||||
ValidationUtils.ArgumentNotNull(o, "o");
|
||||
|
||||
if (o.Type == JsonTokenType.Property)
|
||||
throw new ArgumentException("An item of type {0} cannot be added to content.".FormatWith(o.Type));
|
||||
throw new ArgumentException("An item of type {0} cannot be added to content.".FormatWith(CultureInfo.InvariantCulture, o.Type));
|
||||
}
|
||||
|
||||
public override void WriteTo(JsonWriter writer, params JsonConverter[] converters)
|
||||
@@ -103,5 +104,20 @@ namespace Newtonsoft.Json.Linq
|
||||
writer.WritePropertyName(_name);
|
||||
Value.WriteTo(writer, converters);
|
||||
}
|
||||
|
||||
//public static explicit operator JValue(JProperty property)
|
||||
//{
|
||||
// if (property == null)
|
||||
// return null;
|
||||
|
||||
// JToken value = property.Value;
|
||||
// if (value == null)
|
||||
// return null;
|
||||
|
||||
// if (!(value is JValue))
|
||||
// throw new Exception("Could not cast {0} to JValue".FormatWith(CultureInfo.InvariantCulture, value.GetType()));
|
||||
|
||||
// return (JValue)value;
|
||||
//}
|
||||
}
|
||||
}
|
||||
@@ -37,7 +37,7 @@ namespace Newtonsoft.Json.Linq
|
||||
public abstract class JToken
|
||||
{
|
||||
private JContainer _parent;
|
||||
private JToken _next;
|
||||
internal JToken _next;
|
||||
|
||||
public JContainer Parent
|
||||
{
|
||||
@@ -48,13 +48,26 @@ namespace Newtonsoft.Json.Linq
|
||||
|
||||
public JToken Root
|
||||
{
|
||||
get { return null; }
|
||||
get
|
||||
{
|
||||
JContainer parent = Parent;
|
||||
if (parent == null)
|
||||
return this;
|
||||
|
||||
while (parent.Parent != null)
|
||||
{
|
||||
parent = parent.Parent;
|
||||
}
|
||||
|
||||
return parent;
|
||||
}
|
||||
}
|
||||
|
||||
internal abstract JToken CloneNode();
|
||||
internal abstract bool DeepEquals(JToken node);
|
||||
|
||||
public abstract JsonTokenType Type { get; }
|
||||
public abstract bool HasValues { get; }
|
||||
|
||||
public static bool DeepEquals(JToken t1, JToken t2)
|
||||
{
|
||||
@@ -65,10 +78,10 @@ namespace Newtonsoft.Json.Linq
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_parent == null)
|
||||
return null;
|
||||
if (_parent != null && _next != _parent.First)
|
||||
return _next;
|
||||
|
||||
return _next;
|
||||
return null;
|
||||
}
|
||||
internal set { _next = value; }
|
||||
}
|
||||
@@ -80,7 +93,7 @@ namespace Newtonsoft.Json.Linq
|
||||
if (_parent == null)
|
||||
return null;
|
||||
|
||||
JToken parentNext = ((JToken)_parent.Content).Next;
|
||||
JToken parentNext = ((JToken)_parent.Content)._next;
|
||||
JToken parentNextBefore = null;
|
||||
while (parentNext != this)
|
||||
{
|
||||
@@ -91,11 +104,6 @@ namespace Newtonsoft.Json.Linq
|
||||
}
|
||||
}
|
||||
|
||||
public bool HasValues
|
||||
{
|
||||
get { return false; }
|
||||
}
|
||||
|
||||
public IEnumerable<JToken> Ancestors()
|
||||
{
|
||||
for (JToken parent = Parent; parent != null; parent = parent.Parent)
|
||||
@@ -121,7 +129,7 @@ namespace Newtonsoft.Json.Linq
|
||||
|
||||
public virtual JToken this[object key]
|
||||
{
|
||||
get { throw new InvalidOperationException("Cannot access child value on {0}.".FormatWith(GetType())); }
|
||||
get { throw new InvalidOperationException("Cannot access child value on {0}.".FormatWith(CultureInfo.InvariantCulture, GetType())); }
|
||||
}
|
||||
|
||||
public virtual T Value<T>(object key)
|
||||
@@ -133,19 +141,19 @@ namespace Newtonsoft.Json.Linq
|
||||
|
||||
public virtual JToken First
|
||||
{
|
||||
get { throw new InvalidOperationException("Cannot access child value on {0}.".FormatWith(GetType())); }
|
||||
get { throw new InvalidOperationException("Cannot access child value on {0}.".FormatWith(CultureInfo.InvariantCulture, GetType())); }
|
||||
//get { return null; }
|
||||
}
|
||||
|
||||
public virtual JToken Last
|
||||
{
|
||||
get { throw new InvalidOperationException("Cannot access child value on {0}.".FormatWith(GetType())); }
|
||||
get { throw new InvalidOperationException("Cannot access child value on {0}.".FormatWith(CultureInfo.InvariantCulture, GetType())); }
|
||||
//get { return null; }
|
||||
}
|
||||
|
||||
public virtual JEnumerable<JToken> Children()
|
||||
{
|
||||
throw new InvalidOperationException("Cannot access child value on {0}.".FormatWith(GetType()));
|
||||
throw new InvalidOperationException("Cannot access child value on {0}.".FormatWith(CultureInfo.InvariantCulture, GetType()));
|
||||
//return JEnumerable<JToken>.Empty;
|
||||
}
|
||||
|
||||
@@ -156,12 +164,12 @@ namespace Newtonsoft.Json.Linq
|
||||
|
||||
public virtual IEnumerable<T> Values<T>()
|
||||
{
|
||||
throw new InvalidOperationException("Cannot access child value on {0}.".FormatWith(GetType()));
|
||||
throw new InvalidOperationException("Cannot access child value on {0}.".FormatWith(CultureInfo.InvariantCulture, GetType()));
|
||||
}
|
||||
|
||||
//public virtual IEnumerable<T> Children<T>()
|
||||
// {
|
||||
// throw new InvalidOperationException("Cannot access child value on {0}.".FormatWith(GetType()));
|
||||
// throw new InvalidOperationException("Cannot access child value on {0}.".FormatWith(CultureInfo.InvariantCulture, GetType()));
|
||||
// //return Enumerable.Empty<T>();
|
||||
// }
|
||||
|
||||
@@ -175,6 +183,23 @@ namespace Newtonsoft.Json.Linq
|
||||
|
||||
public void Replace(JToken value)
|
||||
{
|
||||
if (_parent == null)
|
||||
throw new InvalidOperationException("The parent is missing.");
|
||||
|
||||
JContainer parent = _parent;
|
||||
|
||||
JToken previous = this;
|
||||
while (previous._next != this)
|
||||
{
|
||||
previous = previous._next;
|
||||
}
|
||||
if (previous == this)
|
||||
previous = null;
|
||||
|
||||
bool isLast = (this == _parent.Last);
|
||||
|
||||
Remove();
|
||||
parent.AddInternal(isLast, previous, value);
|
||||
}
|
||||
|
||||
public abstract void WriteTo(JsonWriter writer, params JsonConverter[] converters);
|
||||
@@ -240,12 +265,17 @@ namespace Newtonsoft.Json.Linq
|
||||
return (o.Type == JsonTokenType.String || IsNullable(o));
|
||||
}
|
||||
|
||||
private static string GetType(JToken t)
|
||||
{
|
||||
return (t != null) ? t.Type.ToString() : "{null}";
|
||||
}
|
||||
|
||||
#region Cast operators
|
||||
public static explicit operator bool(JToken value)
|
||||
{
|
||||
JValue v = EnsureValue(value);
|
||||
if (v == null || !ValidateBoolean(v, false))
|
||||
throw new ArgumentException("Can not convert {0} to Boolean.".FormatWith(v.Type));
|
||||
throw new ArgumentException("Can not convert {0} to Boolean.".FormatWith(CultureInfo.InvariantCulture, GetType(v)));
|
||||
|
||||
return (bool)v.Value;
|
||||
}
|
||||
@@ -254,7 +284,7 @@ namespace Newtonsoft.Json.Linq
|
||||
{
|
||||
JValue v = EnsureValue(value);
|
||||
if (v == null || !ValidateDate(v, false))
|
||||
throw new ArgumentException("Can not convert {0} to DateTimeOffset.".FormatWith(v.Type));
|
||||
throw new ArgumentException("Can not convert {0} to DateTimeOffset.".FormatWith(CultureInfo.InvariantCulture, GetType(v)));
|
||||
|
||||
return (DateTimeOffset)v.Value;
|
||||
}
|
||||
@@ -266,7 +296,7 @@ namespace Newtonsoft.Json.Linq
|
||||
|
||||
JValue v = EnsureValue(value);
|
||||
if (v == null || !ValidateBoolean(v, true))
|
||||
throw new ArgumentException("Can not convert {0} to Boolean.".FormatWith(v.Type));
|
||||
throw new ArgumentException("Can not convert {0} to Boolean.".FormatWith(CultureInfo.InvariantCulture, GetType(v)));
|
||||
|
||||
return (bool?)v.Value;
|
||||
}
|
||||
@@ -275,7 +305,7 @@ namespace Newtonsoft.Json.Linq
|
||||
{
|
||||
JValue v = EnsureValue(value);
|
||||
if (v == null || !ValidateInteger(v, false))
|
||||
throw new ArgumentException("Can not convert {0} to Int64.".FormatWith(v.Type));
|
||||
throw new ArgumentException("Can not convert {0} to Int64.".FormatWith(CultureInfo.InvariantCulture, GetType(v)));
|
||||
|
||||
return (long)v.Value;
|
||||
}
|
||||
@@ -287,7 +317,7 @@ namespace Newtonsoft.Json.Linq
|
||||
|
||||
JValue v = EnsureValue(value);
|
||||
if (v == null || !ValidateDate(v, true))
|
||||
throw new ArgumentException("Can not convert {0} to DateTime.".FormatWith(v.Type));
|
||||
throw new ArgumentException("Can not convert {0} to DateTime.".FormatWith(CultureInfo.InvariantCulture, GetType(v)));
|
||||
|
||||
return (DateTime?)v.Value;
|
||||
}
|
||||
@@ -299,7 +329,7 @@ namespace Newtonsoft.Json.Linq
|
||||
|
||||
JValue v = EnsureValue(value);
|
||||
if (v == null || !ValidateDate(v, true))
|
||||
throw new ArgumentException("Can not convert {0} to DateTimeOffset.".FormatWith(v.Type));
|
||||
throw new ArgumentException("Can not convert {0} to DateTimeOffset.".FormatWith(CultureInfo.InvariantCulture, GetType(v)));
|
||||
|
||||
return (DateTimeOffset?)v.Value;
|
||||
}
|
||||
@@ -311,7 +341,7 @@ namespace Newtonsoft.Json.Linq
|
||||
|
||||
JValue v = EnsureValue(value);
|
||||
if (v == null || !ValidateFloat(v, true))
|
||||
throw new ArgumentException("Can not convert {0} to Decimal.".FormatWith(v.Type));
|
||||
throw new ArgumentException("Can not convert {0} to Decimal.".FormatWith(CultureInfo.InvariantCulture, GetType(v)));
|
||||
|
||||
return (decimal?)v.Value;
|
||||
}
|
||||
@@ -323,7 +353,7 @@ namespace Newtonsoft.Json.Linq
|
||||
|
||||
JValue v = EnsureValue(value);
|
||||
if (v == null || !ValidateFloat(v, true))
|
||||
throw new ArgumentException("Can not convert {0} to Double.".FormatWith(v.Type));
|
||||
throw new ArgumentException("Can not convert {0} to Double.".FormatWith(CultureInfo.InvariantCulture, GetType(v)));
|
||||
|
||||
return (double?)v.Value;
|
||||
}
|
||||
@@ -332,7 +362,7 @@ namespace Newtonsoft.Json.Linq
|
||||
{
|
||||
JValue v = EnsureValue(value);
|
||||
if (v == null || !ValidateInteger(v, false))
|
||||
throw new ArgumentException("Can not convert {0} to Int32.".FormatWith(v.Type));
|
||||
throw new ArgumentException("Can not convert {0} to Int32.".FormatWith(CultureInfo.InvariantCulture, GetType(v)));
|
||||
|
||||
return Convert.ToInt32(v.Value, CultureInfo.InvariantCulture);
|
||||
}
|
||||
@@ -344,7 +374,7 @@ namespace Newtonsoft.Json.Linq
|
||||
|
||||
JValue v = EnsureValue(value);
|
||||
if (v == null || !ValidateInteger(v, true))
|
||||
throw new ArgumentException("Can not convert {0} to Int32.".FormatWith(v.Type));
|
||||
throw new ArgumentException("Can not convert {0} to Int32.".FormatWith(CultureInfo.InvariantCulture, GetType(v)));
|
||||
|
||||
return (v.Value != null) ? (int?)Convert.ToInt32(v.Value, CultureInfo.InvariantCulture) : null;
|
||||
}
|
||||
@@ -353,7 +383,7 @@ namespace Newtonsoft.Json.Linq
|
||||
{
|
||||
JValue v = EnsureValue(value);
|
||||
if (v == null || !ValidateDate(v, false))
|
||||
throw new ArgumentException("Can not convert {0} to DateTime.".FormatWith(v.Type));
|
||||
throw new ArgumentException("Can not convert {0} to DateTime.".FormatWith(CultureInfo.InvariantCulture, GetType(v)));
|
||||
|
||||
return (DateTime)v.Value;
|
||||
}
|
||||
@@ -365,7 +395,7 @@ namespace Newtonsoft.Json.Linq
|
||||
|
||||
JValue v = EnsureValue(value);
|
||||
if (v == null || !ValidateInteger(v, true))
|
||||
throw new ArgumentException("Can not convert {0} to Int64.".FormatWith(v.Type));
|
||||
throw new ArgumentException("Can not convert {0} to Int64.".FormatWith(CultureInfo.InvariantCulture, GetType(v)));
|
||||
|
||||
return (long?)v.Value;
|
||||
}
|
||||
@@ -377,7 +407,7 @@ namespace Newtonsoft.Json.Linq
|
||||
|
||||
JValue v = EnsureValue(value);
|
||||
if (v == null || !ValidateFloat(v, true))
|
||||
throw new ArgumentException("Can not convert {0} to Single.".FormatWith(v.Type));
|
||||
throw new ArgumentException("Can not convert {0} to Single.".FormatWith(CultureInfo.InvariantCulture, GetType(v)));
|
||||
|
||||
return (float?)v.Value;
|
||||
}
|
||||
@@ -386,7 +416,7 @@ namespace Newtonsoft.Json.Linq
|
||||
{
|
||||
JValue v = EnsureValue(value);
|
||||
if (v == null || !ValidateFloat(v, false))
|
||||
throw new ArgumentException("Can not convert {0} to Decimal.".FormatWith(v.Type));
|
||||
throw new ArgumentException("Can not convert {0} to Decimal.".FormatWith(CultureInfo.InvariantCulture, GetType(v)));
|
||||
|
||||
return (decimal)v.Value;
|
||||
}
|
||||
@@ -398,7 +428,7 @@ namespace Newtonsoft.Json.Linq
|
||||
|
||||
JValue v = EnsureValue(value);
|
||||
if (v == null || !ValidateInteger(v, true))
|
||||
throw new ArgumentException("Can not convert {0} to UInt32.".FormatWith(v.Type));
|
||||
throw new ArgumentException("Can not convert {0} to UInt32.".FormatWith(CultureInfo.InvariantCulture, GetType(v)));
|
||||
|
||||
return (uint?)v.Value;
|
||||
}
|
||||
@@ -410,7 +440,7 @@ namespace Newtonsoft.Json.Linq
|
||||
|
||||
JValue v = EnsureValue(value);
|
||||
if (v == null || !ValidateInteger(v, true))
|
||||
throw new ArgumentException("Can not convert {0} to UInt64.".FormatWith(v.Type));
|
||||
throw new ArgumentException("Can not convert {0} to UInt64.".FormatWith(CultureInfo.InvariantCulture, GetType(v)));
|
||||
|
||||
return (ulong?)v.Value;
|
||||
}
|
||||
@@ -419,7 +449,7 @@ namespace Newtonsoft.Json.Linq
|
||||
{
|
||||
JValue v = EnsureValue(value);
|
||||
if (v == null || !ValidateFloat(v, false))
|
||||
throw new ArgumentException("Can not convert {0} to Double.".FormatWith(v.Type));
|
||||
throw new ArgumentException("Can not convert {0} to Double.".FormatWith(CultureInfo.InvariantCulture, GetType(v)));
|
||||
|
||||
return (double)v.Value;
|
||||
}
|
||||
@@ -428,9 +458,9 @@ namespace Newtonsoft.Json.Linq
|
||||
{
|
||||
JValue v = EnsureValue(value);
|
||||
if (v == null || !ValidateFloat(v, false))
|
||||
throw new ArgumentException("Can not convert {0} to Single.".FormatWith(v.Type));
|
||||
throw new ArgumentException("Can not convert {0} to Single.".FormatWith(CultureInfo.InvariantCulture, GetType(v)));
|
||||
|
||||
return (float)v.Value;
|
||||
return Convert.ToSingle(v.Value, CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
public static explicit operator string(JToken value)
|
||||
@@ -440,7 +470,7 @@ namespace Newtonsoft.Json.Linq
|
||||
|
||||
JValue v = EnsureValue(value);
|
||||
if (v == null || !ValidateString(v))
|
||||
throw new ArgumentException("Can not convert {0} to String.".FormatWith(v.Type));
|
||||
throw new ArgumentException("Can not convert {0} to String.".FormatWith(CultureInfo.InvariantCulture, GetType(v)));
|
||||
|
||||
return (string)v.Value;
|
||||
}
|
||||
@@ -449,18 +479,18 @@ namespace Newtonsoft.Json.Linq
|
||||
{
|
||||
JValue v = EnsureValue(value);
|
||||
if (v == null || !ValidateInteger(v, false))
|
||||
throw new ArgumentException("Can not convert {0} to UInt32.".FormatWith(v.Type));
|
||||
throw new ArgumentException("Can not convert {0} to UInt32.".FormatWith(CultureInfo.InvariantCulture, GetType(v)));
|
||||
|
||||
return (uint)v.Value;
|
||||
return Convert.ToUInt32(v.Value, CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
public static explicit operator ulong(JToken value)
|
||||
{
|
||||
JValue v = EnsureValue(value);
|
||||
if (v == null || !ValidateInteger(v, false))
|
||||
throw new ArgumentException("Can not convert {0} to UInt64.".FormatWith(v.Type));
|
||||
throw new ArgumentException("Can not convert {0} to UInt64.".FormatWith(CultureInfo.InvariantCulture, GetType(v)));
|
||||
|
||||
return (ulong)v.Value;
|
||||
return Convert.ToUInt64(v.Value, CultureInfo.InvariantCulture);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
@@ -56,6 +56,11 @@ namespace Newtonsoft.Json.Linq
|
||||
{
|
||||
}
|
||||
|
||||
public JValue(ulong value)
|
||||
: this(value, JsonTokenType.Integer)
|
||||
{
|
||||
}
|
||||
|
||||
public JValue(double value)
|
||||
: this(value, JsonTokenType.Float)
|
||||
{
|
||||
@@ -77,7 +82,7 @@ namespace Newtonsoft.Json.Linq
|
||||
}
|
||||
|
||||
public JValue(object value)
|
||||
: this(value, GetValueType(value))
|
||||
: this(value, GetValueType(null, value))
|
||||
{
|
||||
}
|
||||
|
||||
@@ -87,7 +92,37 @@ namespace Newtonsoft.Json.Linq
|
||||
if (other == null)
|
||||
return false;
|
||||
|
||||
return (this == other || (_valueType == other.Type && _value == other.Value));
|
||||
return (this == other || (_valueType == other.Type && Compare(_value, other.Value)));
|
||||
}
|
||||
|
||||
public override bool HasValues
|
||||
{
|
||||
get { return false; }
|
||||
}
|
||||
|
||||
public bool Compare(object objA, object objB)
|
||||
{
|
||||
if (objA == null && objB == null)
|
||||
return true;
|
||||
|
||||
switch (_valueType)
|
||||
{
|
||||
case JsonTokenType.Integer:
|
||||
if (objA is ulong || objB is ulong)
|
||||
return Convert.ToDecimal(objA).Equals(Convert.ToDecimal(objB));
|
||||
else
|
||||
return Convert.ToInt64(objA).Equals(Convert.ToInt64(objB));
|
||||
case JsonTokenType.Float:
|
||||
return Convert.ToDouble(objA).Equals(Convert.ToDouble(objB));
|
||||
case JsonTokenType.Comment:
|
||||
case JsonTokenType.String:
|
||||
case JsonTokenType.Boolean:
|
||||
return objA.Equals(objB);
|
||||
case JsonTokenType.Date:
|
||||
return objA.Equals(objB);
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException("valueType", _valueType, "Unexpected value type: {0}".FormatWith(CultureInfo.InvariantCulture, _valueType));
|
||||
}
|
||||
}
|
||||
|
||||
internal override JToken CloneNode()
|
||||
@@ -100,12 +135,12 @@ namespace Newtonsoft.Json.Linq
|
||||
return new JValue(value, JsonTokenType.Comment);
|
||||
}
|
||||
|
||||
private static JsonTokenType GetValueType(object value)
|
||||
private static JsonTokenType GetValueType(JsonTokenType? current, object value)
|
||||
{
|
||||
if (value == null)
|
||||
return JsonTokenType.Null;
|
||||
else if (value is string)
|
||||
return JsonTokenType.String;
|
||||
return (current == JsonTokenType.Comment) ? JsonTokenType.Comment : JsonTokenType.String;
|
||||
else if (value is long || value is int || value is short || value is sbyte
|
||||
|| value is ulong || value is uint || value is ushort || value is byte)
|
||||
return JsonTokenType.Integer;
|
||||
@@ -116,7 +151,7 @@ namespace Newtonsoft.Json.Linq
|
||||
else if (value is bool)
|
||||
return JsonTokenType.Boolean;
|
||||
|
||||
throw new ArgumentException("Could not determin JSON object type for type {0}.".FormatWith(value.GetType()));
|
||||
throw new ArgumentException("Could not determin JSON object type for type {0}.".FormatWith(CultureInfo.InvariantCulture, value.GetType()));
|
||||
}
|
||||
|
||||
public override JsonTokenType Type
|
||||
@@ -127,10 +162,19 @@ namespace Newtonsoft.Json.Linq
|
||||
public object Value
|
||||
{
|
||||
get { return _value; }
|
||||
set { _value = value; }
|
||||
set
|
||||
{
|
||||
Type currentType = (_value != null) ? _value.GetType() : null;
|
||||
Type newType = (value != null) ? value.GetType() : null;
|
||||
|
||||
if (currentType != newType)
|
||||
_valueType = GetValueType(_valueType, value);
|
||||
|
||||
_value = value;
|
||||
}
|
||||
}
|
||||
|
||||
public static JToken CreateString(string value)
|
||||
public static JValue CreateString(string value)
|
||||
{
|
||||
return new JValue(value, JsonTokenType.String);
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Collections.ObjectModel;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Newtonsoft.Json
|
||||
{
|
||||
@@ -51,7 +52,7 @@ namespace Newtonsoft.Json
|
||||
if (!existingMemberMapping.Ignored)
|
||||
{
|
||||
throw new JsonSerializationException(
|
||||
"A member with the name '{0}' already exists on {1}. Use the JsonPropertyAttribute to specify another name.".FormatWith(memberMapping.MappingName, memberMapping.Member.DeclaringType));
|
||||
"A member with the name '{0}' already exists on {1}. Use the JsonPropertyAttribute to specify another name.".FormatWith(CultureInfo.InvariantCulture, memberMapping.MappingName, memberMapping.Member.DeclaringType));
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -30,6 +30,7 @@ using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Collections;
|
||||
using System.Linq;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Newtonsoft.Json.Utilities
|
||||
{
|
||||
@@ -315,7 +316,7 @@ namespace Newtonsoft.Json.Utilities
|
||||
else if (returnDefaultIfEmpty && list.Count == 0)
|
||||
return default(T);
|
||||
else
|
||||
throw new Exception("Expected single {0} in list but got {1}.".FormatWith(typeof(T), list.Count));
|
||||
throw new Exception("Expected single {0} in list but got {1}.".FormatWith(CultureInfo.InvariantCulture, typeof(T), list.Count));
|
||||
}
|
||||
#endregion
|
||||
|
||||
@@ -389,7 +390,7 @@ namespace Newtonsoft.Json.Utilities
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Can not create ListWrapper for type {0}.".FormatWith(list.GetType()));
|
||||
throw new Exception("Can not create ListWrapper for type {0}.".FormatWith(CultureInfo.InvariantCulture, list.GetType()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -418,7 +419,7 @@ namespace Newtonsoft.Json.Utilities
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Can not create DictionaryWrapper for type {0}.".FormatWith(dictionary.GetType()));
|
||||
throw new Exception("Can not create DictionaryWrapper for type {0}.".FormatWith(CultureInfo.InvariantCulture, dictionary.GetType()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -459,7 +460,7 @@ namespace Newtonsoft.Json.Utilities
|
||||
}
|
||||
|
||||
if (!suitableConstructor)
|
||||
throw new Exception("Read-only type {0} does not have a public constructor that takes a type that implements {1}.".FormatWith(listType, genericEnumerable));
|
||||
throw new Exception("Read-only type {0} does not have a public constructor that takes a type that implements {1}.".FormatWith(CultureInfo.InvariantCulture, listType, genericEnumerable));
|
||||
|
||||
// can't add or modify a readonly list
|
||||
// use List<T> and convert once populated
|
||||
@@ -485,7 +486,7 @@ namespace Newtonsoft.Json.Utilities
|
||||
}
|
||||
|
||||
if (list == null)
|
||||
throw new Exception("Cannot create and populate list type {0}.".FormatWith(listType));
|
||||
throw new Exception("Cannot create and populate list type {0}.".FormatWith(CultureInfo.InvariantCulture, listType));
|
||||
|
||||
populateList(list);
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@ using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
using System.Linq;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Newtonsoft.Json.Utilities
|
||||
{
|
||||
@@ -261,7 +262,7 @@ namespace Newtonsoft.Json.Utilities
|
||||
private static void VerifyValueType(object value)
|
||||
{
|
||||
if (!IsCompatibleObject(value))
|
||||
throw new ArgumentException("The value '{0}' is not of type '{1}' and cannot be used in this generic collection.".FormatWith(value, typeof(T)), "value");
|
||||
throw new ArgumentException("The value '{0}' is not of type '{1}' and cannot be used in this generic collection.".FormatWith(CultureInfo.InvariantCulture, value, typeof(T)), "value");
|
||||
}
|
||||
|
||||
private static bool IsCompatibleObject(object value)
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Newtonsoft.Json.Utilities
|
||||
{
|
||||
@@ -55,7 +56,7 @@ namespace Newtonsoft.Json.Utilities
|
||||
if (descriptionAttribute != null)
|
||||
return descriptionAttribute.Description;
|
||||
else
|
||||
throw new Exception("No DescriptionAttribute on '{0}'.".FormatWith(o.GetType()));
|
||||
throw new Exception("No DescriptionAttribute on '{0}'.".FormatWith(CultureInfo.InvariantCulture, o.GetType()));
|
||||
}
|
||||
|
||||
public static IList<string> GetDescriptions(IList values)
|
||||
|
||||
@@ -30,6 +30,7 @@ using System.Reflection;
|
||||
using System.Collections;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Newtonsoft.Json.Utilities
|
||||
{
|
||||
@@ -106,14 +107,14 @@ namespace Newtonsoft.Json.Utilities
|
||||
ValidationUtils.ArgumentNotNull(type, "type");
|
||||
|
||||
if (type.IsGenericTypeDefinition)
|
||||
throw new ArgumentException("Type {0} is a generic type definition and cannot be instantiated.".FormatWith(type), "type");
|
||||
throw new ArgumentException("Type {0} is a generic type definition and cannot be instantiated.".FormatWith(CultureInfo.InvariantCulture, type), "type");
|
||||
|
||||
if (type.IsClass || type.IsInterface || type == typeof(void))
|
||||
return null;
|
||||
else if (type.IsValueType)
|
||||
return Activator.CreateInstance(type);
|
||||
else
|
||||
throw new ArgumentException("Type {0} cannot be instantiated.".FormatWith(type), "type");
|
||||
throw new ArgumentException("Type {0} cannot be instantiated.".FormatWith(CultureInfo.InvariantCulture, type), "type");
|
||||
}
|
||||
|
||||
public static bool IsPropertyIndexed(PropertyInfo property)
|
||||
@@ -196,7 +197,7 @@ namespace Newtonsoft.Json.Utilities
|
||||
else if (IsSubClass(type, typeof(IList<>), out genericListType))
|
||||
{
|
||||
if (genericListType.IsGenericTypeDefinition)
|
||||
throw new Exception("Type {0} is not a list.".FormatWith(type));
|
||||
throw new Exception("Type {0} is not a list.".FormatWith(CultureInfo.InvariantCulture, type));
|
||||
|
||||
return genericListType.GetGenericArguments()[0];
|
||||
}
|
||||
@@ -206,7 +207,7 @@ namespace Newtonsoft.Json.Utilities
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Type {0} is not a list.".FormatWith(type));
|
||||
throw new Exception("Type {0} is not a list.".FormatWith(CultureInfo.InvariantCulture, type));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -218,7 +219,7 @@ namespace Newtonsoft.Json.Utilities
|
||||
if (IsSubClass(dictionaryType, typeof(IDictionary<,>), out genericDictionaryType))
|
||||
{
|
||||
if (genericDictionaryType.IsGenericTypeDefinition)
|
||||
throw new Exception("Type {0} is not a dictionary.".FormatWith(dictionaryType));
|
||||
throw new Exception("Type {0} is not a dictionary.".FormatWith(CultureInfo.InvariantCulture, dictionaryType));
|
||||
|
||||
Type[] dictionaryGenericArguments = genericDictionaryType.GetGenericArguments();
|
||||
|
||||
@@ -234,7 +235,7 @@ namespace Newtonsoft.Json.Utilities
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Type {0} is not a dictionary.".FormatWith(dictionaryType));
|
||||
throw new Exception("Type {0} is not a dictionary.".FormatWith(CultureInfo.InvariantCulture, dictionaryType));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -289,7 +290,7 @@ namespace Newtonsoft.Json.Utilities
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Type {0} is neither a ValueType or a Class.".FormatWith(elementType));
|
||||
throw new Exception("Type {0} is neither a ValueType or a Class.".FormatWith(CultureInfo.InvariantCulture, elementType));
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -387,10 +388,10 @@ namespace Newtonsoft.Json.Utilities
|
||||
}
|
||||
catch (TargetParameterCountException e)
|
||||
{
|
||||
throw new ArgumentException("MemberInfo '{0}' has index parameters".FormatWith(member.Name), "member", e);
|
||||
throw new ArgumentException("MemberInfo '{0}' has index parameters".FormatWith(CultureInfo.InvariantCulture, member.Name), "member", e);
|
||||
}
|
||||
default:
|
||||
throw new ArgumentException("MemberInfo '{0}' is not of type FieldInfo or PropertyInfo".FormatWith(member.Name), "member");
|
||||
throw new ArgumentException("MemberInfo '{0}' is not of type FieldInfo or PropertyInfo".FormatWith(CultureInfo.InvariantCulture, CultureInfo.InvariantCulture, member.Name), "member");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -414,7 +415,7 @@ namespace Newtonsoft.Json.Utilities
|
||||
((PropertyInfo)member).SetValue(target, value, null);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentException("MemberInfo '{0}' must be of type FieldInfo or PropertyInfo".FormatWith(member.Name), "member");
|
||||
throw new ArgumentException("MemberInfo '{0}' must be of type FieldInfo or PropertyInfo".FormatWith(CultureInfo.InvariantCulture, member.Name), "member");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -526,7 +527,7 @@ namespace Newtonsoft.Json.Utilities
|
||||
{
|
||||
ValidationUtils.ArgumentNotNull(genericTypeDefinition, "genericTypeDefinition");
|
||||
ValidationUtils.ArgumentNotNullOrEmpty<Type>(innerTypes, "innerTypes");
|
||||
ValidationUtils.ArgumentConditionTrue(genericTypeDefinition.IsGenericTypeDefinition, "genericTypeDefinition", "Type {0} is not a generic type definition.".FormatWith(genericTypeDefinition));
|
||||
ValidationUtils.ArgumentConditionTrue(genericTypeDefinition.IsGenericTypeDefinition, "genericTypeDefinition", "Type {0} is not a generic type definition.".FormatWith(CultureInfo.InvariantCulture, genericTypeDefinition));
|
||||
|
||||
return genericTypeDefinition.MakeGenericType(innerTypes);
|
||||
}
|
||||
|
||||
@@ -42,10 +42,10 @@ namespace Newtonsoft.Json.Utilities
|
||||
public const char LineFeed = '\n';
|
||||
public const char Tab = '\t';
|
||||
|
||||
public static string FormatWith(this string format, params object[] args)
|
||||
{
|
||||
return FormatWith(format, CultureInfo.InvariantCulture, args);
|
||||
}
|
||||
//public static string FormatWith(this string format, params object[] args)
|
||||
//{
|
||||
// return FormatWith(format, null, args);
|
||||
//}
|
||||
|
||||
public static string FormatWith(this string format, IFormatProvider provider, params object[] args)
|
||||
{
|
||||
|
||||
@@ -27,6 +27,7 @@ using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Newtonsoft.Json.Utilities
|
||||
{
|
||||
@@ -44,7 +45,7 @@ namespace Newtonsoft.Json.Utilities
|
||||
throw new ArgumentNullException(parameterName);
|
||||
|
||||
if (value.Length == 0)
|
||||
throw new ArgumentException("'{0}' cannot be empty.".FormatWith(parameterName), parameterName);
|
||||
throw new ArgumentException("'{0}' cannot be empty.".FormatWith(CultureInfo.InvariantCulture, parameterName), parameterName);
|
||||
}
|
||||
|
||||
public static void ArgumentNotNullOrEmptyOrWhitespace(string value, string parameterName)
|
||||
@@ -52,7 +53,7 @@ namespace Newtonsoft.Json.Utilities
|
||||
ArgumentNotNullOrEmpty(value, parameterName);
|
||||
|
||||
if (StringUtils.IsWhiteSpace(value))
|
||||
throw new ArgumentException("'{0}' cannot only be whitespace.".FormatWith(parameterName), parameterName);
|
||||
throw new ArgumentException("'{0}' cannot only be whitespace.".FormatWith(CultureInfo.InvariantCulture, parameterName), parameterName);
|
||||
}
|
||||
|
||||
public static void ArgumentTypeIsEnum(Type enumType, string parameterName)
|
||||
@@ -60,12 +61,12 @@ namespace Newtonsoft.Json.Utilities
|
||||
ArgumentNotNull(enumType, "enumType");
|
||||
|
||||
if (!enumType.IsEnum)
|
||||
throw new ArgumentException("Type {0} is not an Enum.".FormatWith(enumType), parameterName);
|
||||
throw new ArgumentException("Type {0} is not an Enum.".FormatWith(CultureInfo.InvariantCulture, enumType), parameterName);
|
||||
}
|
||||
|
||||
public static void ArgumentNotNullOrEmpty<T>(ICollection<T> collection, string parameterName)
|
||||
{
|
||||
ArgumentNotNullOrEmpty<T>(collection, parameterName, "Collection '{0}' cannot be empty.".FormatWith(parameterName));
|
||||
ArgumentNotNullOrEmpty<T>(collection, parameterName, "Collection '{0}' cannot be empty.".FormatWith(CultureInfo.InvariantCulture, parameterName));
|
||||
}
|
||||
|
||||
public static void ArgumentNotNullOrEmpty<T>(ICollection<T> collection, string parameterName, string message)
|
||||
@@ -79,7 +80,7 @@ namespace Newtonsoft.Json.Utilities
|
||||
|
||||
public static void ArgumentNotNullOrEmpty(ICollection collection, string parameterName)
|
||||
{
|
||||
ArgumentNotNullOrEmpty(collection, parameterName, "Collection '{0}' cannot be empty.".FormatWith(parameterName));
|
||||
ArgumentNotNullOrEmpty(collection, parameterName, "Collection '{0}' cannot be empty.".FormatWith(CultureInfo.InvariantCulture, parameterName));
|
||||
}
|
||||
|
||||
public static void ArgumentNotNullOrEmpty(ICollection collection, string parameterName, string message)
|
||||
|
||||
Reference in New Issue
Block a user