-Added ICloneable and DeepClone to JToken
-Changed DeepEquals to take into account possible floating point errors
This commit is contained in:
@@ -43,10 +43,13 @@ namespace Newtonsoft.Json.Linq
|
||||
/// Represents an abstract JSON token.
|
||||
/// </summary>
|
||||
public abstract class JToken : IJEnumerable<JToken>, IJsonLineInfo
|
||||
#if !(NET35 || NET20 || SILVERLIGHT)
|
||||
, IDynamicMetaObjectProvider
|
||||
#if !SILVERLIGHT
|
||||
, ICloneable
|
||||
#endif
|
||||
{
|
||||
#if !(NET35 || NET20 || SILVERLIGHT)
|
||||
, IDynamicMetaObjectProvider
|
||||
#endif
|
||||
{
|
||||
private JContainer _parent;
|
||||
internal JToken _next;
|
||||
private static JTokenEqualityComparer _equalityComparer;
|
||||
@@ -1298,5 +1301,21 @@ namespace Newtonsoft.Json.Linq
|
||||
return GetMetaObject(parameter);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !SILVERLIGHT
|
||||
object ICloneable.Clone()
|
||||
{
|
||||
return DeepClone();
|
||||
}
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of the <see cref="JToken"/>. All child tokens are recursively cloned.
|
||||
/// </summary>
|
||||
/// <returns>A new instance of the <see cref="JToken"/>.</returns>
|
||||
public JToken DeepClone()
|
||||
{
|
||||
return CloneToken();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -159,7 +159,13 @@ namespace Newtonsoft.Json.Linq
|
||||
else
|
||||
return Convert.ToInt64(objA, CultureInfo.InvariantCulture).Equals(Convert.ToInt64(objB, CultureInfo.InvariantCulture));
|
||||
case JTokenType.Float:
|
||||
return Convert.ToDouble(objA, CultureInfo.InvariantCulture).Equals(Convert.ToDouble(objB, CultureInfo.InvariantCulture));
|
||||
double d1 = Convert.ToDouble(objA, CultureInfo.InvariantCulture);
|
||||
double d2 = Convert.ToDouble(objB, CultureInfo.InvariantCulture);
|
||||
if (d1.Equals(d2))
|
||||
return true;
|
||||
|
||||
// take into account possible floating point errors
|
||||
return MathUtils.ApproxEquals(d1, d2);
|
||||
case JTokenType.Comment:
|
||||
case JTokenType.String:
|
||||
case JTokenType.Boolean:
|
||||
|
||||
@@ -124,5 +124,11 @@ namespace Newtonsoft.Json.Utilities
|
||||
|
||||
return Math.Max(val1.Value, val2.Value);
|
||||
}
|
||||
|
||||
public static bool ApproxEquals(double d1, double d2)
|
||||
{
|
||||
// are values equal to within 12 (or so) digits of precision?
|
||||
return Math.Abs(d1 - d2) < (Math.Abs(d1) * 1e-12);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user