-Added GetValue and TryGetValue with StringComparison to JObject
-Fixed SecAnnotate warnings -Fixed error when comparing empty JObjects -Fixed error when comparing DateTime JValue with a DateTimeOffset JValue
This commit is contained in:
@@ -2533,6 +2533,34 @@ bye", reader.Value);
|
|||||||
//Assert.IsFalse(jsonTextReader.Read());
|
//Assert.IsFalse(jsonTextReader.Read());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void WriteReadBoundaryDecimals()
|
||||||
|
{
|
||||||
|
StringWriter sw = new StringWriter();
|
||||||
|
JsonTextWriter writer = new JsonTextWriter(sw);
|
||||||
|
|
||||||
|
writer.WriteStartArray();
|
||||||
|
writer.WriteValue(decimal.MaxValue);
|
||||||
|
writer.WriteValue(decimal.MinValue);
|
||||||
|
writer.WriteEndArray();
|
||||||
|
|
||||||
|
string json = sw.ToString();
|
||||||
|
|
||||||
|
StringReader sr = new StringReader(json);
|
||||||
|
JsonTextReader reader = new JsonTextReader(sr);
|
||||||
|
|
||||||
|
Assert.IsTrue(reader.Read());
|
||||||
|
|
||||||
|
decimal? max = reader.ReadAsDecimal();
|
||||||
|
Assert.AreEqual(decimal.MaxValue, max);
|
||||||
|
|
||||||
|
decimal? min = reader.ReadAsDecimal();
|
||||||
|
Assert.AreEqual(decimal.MinValue, min);
|
||||||
|
|
||||||
|
Assert.IsTrue(reader.Read());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public class ToggleReaderError : TextReader
|
public class ToggleReaderError : TextReader
|
||||||
{
|
{
|
||||||
private readonly TextReader _inner;
|
private readonly TextReader _inner;
|
||||||
@@ -2552,7 +2580,6 @@ bye", reader.Value);
|
|||||||
return _inner.Read(buffer, index, 1);
|
return _inner.Read(buffer, index, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public class SlowStream : Stream
|
public class SlowStream : Stream
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1865,5 +1865,52 @@ Parameter name: arrayIndex",
|
|||||||
l2 = o.ToList<KeyValuePair<string, JToken>>();
|
l2 = o.ToList<KeyValuePair<string, JToken>>();
|
||||||
Assert.AreEqual(1, l2.Count);
|
Assert.AreEqual(1, l2.Count);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void EmptyObjectDeepEquals()
|
||||||
|
{
|
||||||
|
Assert.IsTrue(JToken.DeepEquals(new JObject(), new JObject()));
|
||||||
|
|
||||||
|
JObject a = new JObject();
|
||||||
|
JObject b = new JObject();
|
||||||
|
|
||||||
|
b.Add("hi", "bye");
|
||||||
|
b.Remove("hi");
|
||||||
|
|
||||||
|
Assert.IsTrue(JToken.DeepEquals(a, b));
|
||||||
|
Assert.IsTrue(JToken.DeepEquals(b, a));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetValue()
|
||||||
|
{
|
||||||
|
JObject a = new JObject();
|
||||||
|
a["Name"] = "Name!";
|
||||||
|
a["name"] = "name!";
|
||||||
|
a["title"] = "Title!";
|
||||||
|
|
||||||
|
Assert.AreEqual(null, a.GetValue("NAME", StringComparison.Ordinal));
|
||||||
|
Assert.AreEqual(null, a.GetValue("NAME"));
|
||||||
|
Assert.AreEqual(null, a.GetValue("TITLE"));
|
||||||
|
Assert.AreEqual("Name!", (string)a.GetValue("NAME", StringComparison.OrdinalIgnoreCase));
|
||||||
|
Assert.AreEqual("name!", (string)a.GetValue("name", StringComparison.Ordinal));
|
||||||
|
Assert.AreEqual(null, a.GetValue(null, StringComparison.Ordinal));
|
||||||
|
Assert.AreEqual(null, a.GetValue(null));
|
||||||
|
|
||||||
|
JToken v;
|
||||||
|
Assert.IsFalse(a.TryGetValue("NAME", StringComparison.Ordinal, out v));
|
||||||
|
Assert.AreEqual(null, v);
|
||||||
|
|
||||||
|
Assert.IsFalse(a.TryGetValue("NAME", out v));
|
||||||
|
Assert.IsFalse(a.TryGetValue("TITLE", out v));
|
||||||
|
|
||||||
|
Assert.IsTrue(a.TryGetValue("NAME", StringComparison.OrdinalIgnoreCase, out v));
|
||||||
|
Assert.AreEqual("Name!", (string)v);
|
||||||
|
|
||||||
|
Assert.IsTrue(a.TryGetValue("name", StringComparison.Ordinal, out v));
|
||||||
|
Assert.AreEqual("name!", (string)v);
|
||||||
|
|
||||||
|
Assert.IsFalse(a.TryGetValue(null, StringComparison.Ordinal, out v));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -342,5 +342,26 @@ namespace Newtonsoft.Json.Tests.Linq
|
|||||||
|
|
||||||
Assert.AreEqual(rate.Compoundings, rate2.Compoundings);
|
Assert.AreEqual(rate.Compoundings, rate2.Compoundings);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if !NET20
|
||||||
|
public class ObjectWithDateTimeOffset
|
||||||
|
{
|
||||||
|
public DateTimeOffset DateTimeOffset { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void SetDateTimeOffsetProperty()
|
||||||
|
{
|
||||||
|
var dateTimeOffset = new DateTimeOffset(new DateTime(2000, 1, 1), TimeSpan.FromHours(3));
|
||||||
|
var json = JsonConvert.SerializeObject(
|
||||||
|
new ObjectWithDateTimeOffset
|
||||||
|
{
|
||||||
|
DateTimeOffset = dateTimeOffset
|
||||||
|
});
|
||||||
|
|
||||||
|
var o = JObject.Parse(json);
|
||||||
|
o.Property("DateTimeOffset").Value = dateTimeOffset;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -76,5 +76,5 @@ using System.Security;
|
|||||||
// by using the '*' as shown below:
|
// by using the '*' as shown below:
|
||||||
[assembly: AssemblyVersion("4.5.0.0")]
|
[assembly: AssemblyVersion("4.5.0.0")]
|
||||||
#if !PocketPC
|
#if !PocketPC
|
||||||
[assembly: AssemblyFileVersion("4.5.9.15307")]
|
[assembly: AssemblyFileVersion("4.5.9.15313")]
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -378,6 +378,59 @@ namespace Newtonsoft.Json.Linq
|
|||||||
writer.WriteEndObject();
|
writer.WriteEndObject();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the <see cref="Newtonsoft.Json.Linq.JToken"/> with the specified property name.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>The <see cref="Newtonsoft.Json.Linq.JToken"/> with the specified property name.</value>
|
||||||
|
public JToken GetValue(string propertyName)
|
||||||
|
{
|
||||||
|
return GetValue(propertyName, StringComparison.Ordinal);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the <see cref="Newtonsoft.Json.Linq.JToken"/> with the specified property name.
|
||||||
|
/// The exact property name will be searched for first and if no matching property is found then
|
||||||
|
/// the <see cref="StringComparison"/> will be used to match a property.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>The <see cref="Newtonsoft.Json.Linq.JToken"/> with the specified property name.</value>
|
||||||
|
public JToken GetValue(string propertyName, StringComparison comparison)
|
||||||
|
{
|
||||||
|
if (propertyName == null)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
// attempt to get value via dictionary first for performance
|
||||||
|
JProperty property = Property(propertyName);
|
||||||
|
if (property != null)
|
||||||
|
return property.Value;
|
||||||
|
|
||||||
|
// test above already uses this comparison so no need to repeat
|
||||||
|
if (comparison != StringComparison.Ordinal)
|
||||||
|
{
|
||||||
|
foreach (JProperty p in _properties)
|
||||||
|
{
|
||||||
|
if (string.Equals(p.Name, propertyName, comparison))
|
||||||
|
return p.Value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tries to get the <see cref="Newtonsoft.Json.Linq.JToken"/> with the specified property name.
|
||||||
|
/// The exact property name will be searched for first and if no matching property is found then
|
||||||
|
/// the <see cref="StringComparison"/> will be used to match a property.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="propertyName">Name of the property.</param>
|
||||||
|
/// <param name="value">The value.</param>
|
||||||
|
/// <param name="comparison">One of the enumeration values that specifies how the strings will be compared.</param>
|
||||||
|
/// <returns>true if a value was successfully retrieved; otherwise, false.</returns>
|
||||||
|
public bool TryGetValue(string propertyName, StringComparison comparison, out JToken value)
|
||||||
|
{
|
||||||
|
value = GetValue(propertyName, comparison);
|
||||||
|
return (value != null);
|
||||||
|
}
|
||||||
|
|
||||||
#region IDictionary<string,JToken> Members
|
#region IDictionary<string,JToken> Members
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Adds the specified property name.
|
/// Adds the specified property name.
|
||||||
|
|||||||
@@ -200,6 +200,15 @@ namespace Newtonsoft.Json.Linq
|
|||||||
Dictionary<string, JToken> d1 = _dictionary;
|
Dictionary<string, JToken> d1 = _dictionary;
|
||||||
Dictionary<string, JToken> d2 = other._dictionary;
|
Dictionary<string, JToken> d2 = other._dictionary;
|
||||||
|
|
||||||
|
if (d1 == null && d2 == null)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (d1 == null)
|
||||||
|
return (d2.Count == 0);
|
||||||
|
|
||||||
|
if (d2 == null)
|
||||||
|
return (d1.Count == 0);
|
||||||
|
|
||||||
if (d1.Count != d2.Count)
|
if (d1.Count != d2.Count)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
|||||||
@@ -216,19 +216,26 @@ namespace Newtonsoft.Json.Linq
|
|||||||
if (objA is DateTime)
|
if (objA is DateTime)
|
||||||
{
|
{
|
||||||
#endif
|
#endif
|
||||||
DateTime date1 = Convert.ToDateTime(objA, CultureInfo.InvariantCulture);
|
DateTime date1 = (DateTime)objA;
|
||||||
DateTime date2 = Convert.ToDateTime(objB, CultureInfo.InvariantCulture);
|
DateTime date2;
|
||||||
|
|
||||||
|
if (objB is DateTimeOffset)
|
||||||
|
date2 = ((DateTimeOffset)objB).DateTime;
|
||||||
|
else
|
||||||
|
date2 = Convert.ToDateTime(objB, CultureInfo.InvariantCulture);
|
||||||
|
|
||||||
return date1.CompareTo(date2);
|
return date1.CompareTo(date2);
|
||||||
#if !NET20
|
#if !NET20
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (!(objB is DateTimeOffset))
|
|
||||||
throw new ArgumentException("Object must be of type DateTimeOffset.");
|
|
||||||
|
|
||||||
DateTimeOffset date1 = (DateTimeOffset) objA;
|
DateTimeOffset date1 = (DateTimeOffset) objA;
|
||||||
DateTimeOffset date2 = (DateTimeOffset) objB;
|
DateTimeOffset date2;
|
||||||
|
|
||||||
|
if (objB is DateTimeOffset)
|
||||||
|
date2 = (DateTimeOffset)objB;
|
||||||
|
else
|
||||||
|
date2 = new DateTimeOffset(Convert.ToDateTime(objB, CultureInfo.InvariantCulture));
|
||||||
|
|
||||||
return date1.CompareTo(date2);
|
return date1.CompareTo(date2);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -90,7 +90,7 @@ using System.Security;
|
|||||||
// by using the '*' as shown below:
|
// by using the '*' as shown below:
|
||||||
[assembly: AssemblyVersion("4.5.0.0")]
|
[assembly: AssemblyVersion("4.5.0.0")]
|
||||||
#if !PocketPC
|
#if !PocketPC
|
||||||
[assembly: AssemblyFileVersion("4.5.9.15307")]
|
[assembly: AssemblyFileVersion("4.5.9.15313")]
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
[assembly: CLSCompliant(true)]
|
[assembly: CLSCompliant(true)]
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ using System;
|
|||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using System.Security;
|
||||||
#if !(NETFX_CORE || PORTABLE)
|
#if !(NETFX_CORE || PORTABLE)
|
||||||
using System.Security.Permissions;
|
using System.Security.Permissions;
|
||||||
#endif
|
#endif
|
||||||
@@ -388,6 +389,9 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
|
|
||||||
public static bool DynamicCodeGeneration
|
public static bool DynamicCodeGeneration
|
||||||
{
|
{
|
||||||
|
#if !(NET20 || NET35 || SILVERLIGHT || NETFX_CORE || PORTABLE)
|
||||||
|
[SecuritySafeCritical]
|
||||||
|
#endif
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
if (_dynamicCodeGeneration == null)
|
if (_dynamicCodeGeneration == null)
|
||||||
|
|||||||
Reference in New Issue
Block a user