-Added JValue.CreateNull and JValue.CreateUndefined

This commit is contained in:
James Newton-King
2014-04-29 12:20:44 +12:00
parent 7458cefe95
commit bbcc7e4d54
14 changed files with 71 additions and 38 deletions
@@ -41,7 +41,7 @@ namespace Newtonsoft.Json.Tests.Documentation.Samples.Linq
Console.WriteLine(u);
// http://www.bing.com/
JValue v6 = new JValue((object)null);
JValue v6 = JValue.CreateNull();
u = (Uri)v6;
Console.WriteLine((u != null) ? u.ToString() : "{null}");
@@ -167,7 +167,7 @@ namespace Newtonsoft.Json.Tests.Linq
public void JValueEquals()
{
JObject o = new JObject(
new JProperty("Null", new JValue(null, JTokenType.Null)),
new JProperty("Null", JValue.CreateNull()),
new JProperty("Integer", new JValue(1)),
new JProperty("Float", new JValue(1.1d)),
new JProperty("Decimal", new JValue(1.1m)),
@@ -187,7 +187,7 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.IsTrue(d.Null == d.Null);
Assert.IsTrue(d.Null == null);
Assert.IsTrue(d.Null == new JValue(null, JTokenType.Null));
Assert.IsTrue(d.Null == JValue.CreateNull());
Assert.IsFalse(d.Null == 1);
Assert.IsTrue(d.Integer == d.Integer);
@@ -276,7 +276,7 @@ namespace Newtonsoft.Json.Tests.Linq
public void JValueAddition()
{
JObject o = new JObject(
new JProperty("Null", new JValue(null, JTokenType.Null)),
new JProperty("Null", JValue.CreateNull()),
new JProperty("Integer", new JValue(1)),
new JProperty("Float", new JValue(1.1d)),
new JProperty("Decimal", new JValue(1.1m)),
@@ -611,7 +611,7 @@ namespace Newtonsoft.Json.Tests.Linq
public void JValueToString()
{
JObject o = new JObject(
new JProperty("Null", new JValue(null, JTokenType.Null)),
new JProperty("Null", JValue.CreateNull()),
new JProperty("Integer", new JValue(1)),
new JProperty("Float", new JValue(1.1)),
new JProperty("DateTime", new JValue(new DateTime(2000, 12, 29, 23, 51, 10, DateTimeKind.Utc))),
@@ -57,6 +57,7 @@ namespace Newtonsoft.Json.Tests.Linq
[TestFixture]
public class JObjectTests : TestFixtureBase
{
#if !(NET35 || NET20 || PORTABLE40)
[Test]
public void EmbedJValueStringInNewJObject()
{
@@ -73,6 +74,7 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.AreEqual(null, v.Value);
Assert.IsNull((string)o.title);
}
#endif
[Test]
public void JObjectWithComments()
@@ -156,7 +158,7 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.AreEqual(1, o.Children().Count());
Assert.AreEqual(true, o.TryGetValue("PropertyNameValue", out t));
Assert.AreEqual(true, JToken.DeepEquals(new JValue((object)null), t));
Assert.AreEqual(true, JToken.DeepEquals(JValue.CreateNull(), t));
}
[Test]
@@ -771,7 +773,7 @@ Parameter name: arrayIndex",
o["NullValue"] = null;
Assert.AreEqual(null, changing);
Assert.AreEqual(null, changed);
Assert.AreEqual(new JValue((object)null), o["NullValue"]);
Assert.AreEqual(JValue.CreateNull(), o["NullValue"]);
Assert.AreEqual(4, changingCount);
Assert.AreEqual(4, changedCount);
@@ -815,7 +817,7 @@ Parameter name: arrayIndex",
o["NullValue"] = null;
Assert.AreEqual(null, changed);
Assert.AreEqual(new JValue((object)null), o["NullValue"]);
Assert.AreEqual(JValue.CreateNull(), o["NullValue"]);
Assert.AreEqual(4, changedCount);
o["NullValue"] = null;
@@ -144,7 +144,7 @@ namespace Newtonsoft.Json.Tests.Linq
property = JProperty.Load(reader);
Assert.AreEqual("propertyname", property.Name);
Assert.IsTrue(JToken.DeepEquals(new JValue(null, JTokenType.Null), property.Value));
Assert.IsTrue(JToken.DeepEquals(JValue.CreateNull(), property.Value));
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
}
+15 -15
View File
@@ -247,7 +247,7 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.AreEqual(true, (bool)new JValue(true));
Assert.AreEqual(true, (bool?)new JValue(true));
Assert.AreEqual(null, (bool?)((JValue)null));
Assert.AreEqual(null, (bool?)new JValue((object)null));
Assert.AreEqual(null, (bool?)JValue.CreateNull());
Assert.AreEqual(10, (long)new JValue(10));
Assert.AreEqual(null, (long?)new JValue((long?)null));
Assert.AreEqual(null, (long?)(JValue)null);
@@ -287,13 +287,13 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.AreEqual(5f, (float?)(new JValue(5m)));
Assert.AreEqual(5, (byte)(new JValue(5)));
Assert.AreEqual(null, (sbyte?)new JValue((object)null));
Assert.AreEqual(null, (sbyte?)JValue.CreateNull());
Assert.AreEqual("1", (string)(new JValue(1)));
Assert.AreEqual("1", (string)(new JValue(1.0)));
Assert.AreEqual("1.0", (string)(new JValue(1.0m)));
Assert.AreEqual("True", (string)(new JValue(true)));
Assert.AreEqual(null, (string)(new JValue((object)null)));
Assert.AreEqual(null, (string)(JValue.CreateNull()));
Assert.AreEqual(null, (string)(JValue)null);
Assert.AreEqual("12/12/2000 12:12:12", (string)(new JValue(new DateTime(2000, 12, 12, 12, 12, 12, DateTimeKind.Utc))));
#if !NET20
@@ -320,7 +320,7 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.AreEqual(new Guid("46EFE013-B56A-4E83-99E4-4DCE7678A5BC"), (Guid)(new JValue(new Guid("46EFE013-B56A-4E83-99E4-4DCE7678A5BC"))));
Assert.AreEqual(new Uri("http://www.google.com"), (Uri)(new JValue("http://www.google.com")));
Assert.AreEqual(new Uri("http://www.google.com"), (Uri)(new JValue(new Uri("http://www.google.com"))));
Assert.AreEqual(null, (Uri)(new JValue((object)null)));
Assert.AreEqual(null, (Uri)(JValue.CreateNull()));
Assert.AreEqual(Convert.ToBase64String(Encoding.UTF8.GetBytes("hi")), (string)(new JValue(Encoding.UTF8.GetBytes("hi"))));
CollectionAssert.AreEquivalent((byte[])Encoding.UTF8.GetBytes("hi"), (byte[])(new JValue(Convert.ToBase64String(Encoding.UTF8.GetBytes("hi")))));
Assert.AreEqual(new Guid("46EFE013-B56A-4E83-99E4-4DCE7678A5BC"), (Guid)(new JValue(new Guid("46EFE013-B56A-4E83-99E4-4DCE7678A5BC").ToByteArray())));
@@ -367,7 +367,7 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.AreEqual(new BigInteger(byte.MaxValue), (new JValue(byte.MaxValue)).ToObject<BigInteger>());
Assert.AreEqual(new BigInteger(123), (new JValue(123)).ToObject<BigInteger>());
Assert.AreEqual(new BigInteger(123), (new JValue(123)).ToObject<BigInteger?>());
Assert.AreEqual(null, (new JValue((object)null)).ToObject<BigInteger?>());
Assert.AreEqual(null, (JValue.CreateNull()).ToObject<BigInteger?>());
byte[] intData = BigInteger.Parse(bigIntegerText).ToByteArray();
Assert.AreEqual(BigInteger.Parse(bigIntegerText), (new JValue(intData)).ToObject<BigInteger>());
@@ -392,7 +392,7 @@ namespace Newtonsoft.Json.Tests.Linq
ExceptionAssert.Throws<ArgumentException>("Can not convert Float to DateTime.", () => { var i = (DateTime)new JValue(1.1m); });
ExceptionAssert.Throws<ArgumentException>("Can not convert TimeSpan to DateTime.", () => { var i = (DateTime)new JValue(TimeSpan.Zero); });
ExceptionAssert.Throws<ArgumentException>("Can not convert Uri to DateTime.", () => { var i = (DateTime)new JValue(new Uri("http://www.google.com")); });
ExceptionAssert.Throws<ArgumentException>("Can not convert Null to DateTime.", () => { var i = (DateTime)new JValue((object)null); });
ExceptionAssert.Throws<ArgumentException>("Can not convert Null to DateTime.", () => { var i = (DateTime)JValue.CreateNull(); });
ExceptionAssert.Throws<ArgumentException>("Can not convert Guid to DateTime.", () => { var i = (DateTime)new JValue(Guid.NewGuid()); });
ExceptionAssert.Throws<ArgumentException>("Can not convert Boolean to Uri.", () => { var i = (Uri)new JValue(true); });
@@ -410,7 +410,7 @@ namespace Newtonsoft.Json.Tests.Linq
ExceptionAssert.Throws<ArgumentException>("Can not convert Integer to TimeSpan.", () => { var i = (TimeSpan)new JValue(1); });
ExceptionAssert.Throws<ArgumentException>("Can not convert Float to TimeSpan.", () => { var i = (TimeSpan)new JValue(1.1); });
ExceptionAssert.Throws<ArgumentException>("Can not convert Float to TimeSpan.", () => { var i = (TimeSpan)new JValue(1.1m); });
ExceptionAssert.Throws<ArgumentException>("Can not convert Null to TimeSpan.", () => { var i = (TimeSpan)new JValue((object)null); });
ExceptionAssert.Throws<ArgumentException>("Can not convert Null to TimeSpan.", () => { var i = (TimeSpan)JValue.CreateNull(); });
ExceptionAssert.Throws<ArgumentException>("Can not convert Guid to TimeSpan.", () => { var i = (TimeSpan)new JValue(Guid.NewGuid()); });
ExceptionAssert.Throws<ArgumentException>("Can not convert Date to TimeSpan.", () => { var i = (TimeSpan)new JValue(DateTime.Now); });
#if !NET20
@@ -422,7 +422,7 @@ namespace Newtonsoft.Json.Tests.Linq
ExceptionAssert.Throws<ArgumentException>("Can not convert Integer to Guid.", () => { var i = (Guid)new JValue(1); });
ExceptionAssert.Throws<ArgumentException>("Can not convert Float to Guid.", () => { var i = (Guid)new JValue(1.1); });
ExceptionAssert.Throws<ArgumentException>("Can not convert Float to Guid.", () => { var i = (Guid)new JValue(1.1m); });
ExceptionAssert.Throws<ArgumentException>("Can not convert Null to Guid.", () => { var i = (Guid)new JValue((object)null); });
ExceptionAssert.Throws<ArgumentException>("Can not convert Null to Guid.", () => { var i = (Guid)JValue.CreateNull(); });
ExceptionAssert.Throws<ArgumentException>("Can not convert Date to Guid.", () => { var i = (Guid)new JValue(DateTime.Now); });
#if !NET20
ExceptionAssert.Throws<ArgumentException>("Can not convert Date to Guid.", () => { var i = (Guid)new JValue(DateTimeOffset.Now); });
@@ -437,7 +437,7 @@ namespace Newtonsoft.Json.Tests.Linq
#if !(NET20 || NET35 || PORTABLE || PORTABLE40)
ExceptionAssert.Throws<ArgumentException>("Can not convert Uri to BigInteger.", () => { var i = (new JValue(new Uri("http://www.google.com"))).ToObject<BigInteger>(); });
ExceptionAssert.Throws<ArgumentException>("Can not convert Null to BigInteger.", () => { var i = (new JValue((object)null)).ToObject<BigInteger>(); });
ExceptionAssert.Throws<ArgumentException>("Can not convert Null to BigInteger.", () => { var i = (JValue.CreateNull()).ToObject<BigInteger>(); });
ExceptionAssert.Throws<ArgumentException>("Can not convert Guid to BigInteger.", () => { var i = (new JValue(Guid.NewGuid())).ToObject<BigInteger>(); });
ExceptionAssert.Throws<ArgumentException>("Can not convert Guid to BigInteger.", () => { var i = (new JValue(Guid.NewGuid())).ToObject<BigInteger?>(); });
#endif
@@ -452,7 +452,7 @@ namespace Newtonsoft.Json.Tests.Linq
#if !(NET20 || NET35 || PORTABLE)
Assert.AreEqual((BigInteger)1, (new JValue(1).ToObject(typeof(BigInteger))));
Assert.AreEqual((BigInteger)1, (new JValue(1).ToObject(typeof(BigInteger?))));
Assert.AreEqual((BigInteger?)null, (new JValue((object)null).ToObject(typeof(BigInteger?))));
Assert.AreEqual((BigInteger?)null, (JValue.CreateNull().ToObject(typeof(BigInteger?))));
#endif
Assert.AreEqual((ushort)1, (new JValue(1).ToObject(typeof(ushort))));
Assert.AreEqual((ushort)1, (new JValue(1).ToObject(typeof(ushort?))));
@@ -542,10 +542,10 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.IsTrue(JToken.DeepEquals(new JValue(decimal.MinValue), (JValue)decimal.MinValue));
Assert.IsTrue(JToken.DeepEquals(new JValue(float.MaxValue), (JValue)(float?)float.MaxValue));
Assert.IsTrue(JToken.DeepEquals(new JValue(double.MaxValue), (JValue)(double?)double.MaxValue));
Assert.IsTrue(JToken.DeepEquals(new JValue((object)null), (JValue)(double?)null));
Assert.IsTrue(JToken.DeepEquals(JValue.CreateNull(), (JValue)(double?)null));
Assert.IsFalse(JToken.DeepEquals(new JValue(true), (JValue)(bool?)null));
Assert.IsFalse(JToken.DeepEquals(new JValue((object)null), (JValue)(object)null));
Assert.IsFalse(JToken.DeepEquals(JValue.CreateNull(), (JValue)(object)null));
byte[] emptyData = new byte[0];
Assert.IsTrue(JToken.DeepEquals(new JValue(emptyData), (JValue)emptyData));
@@ -553,12 +553,12 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.IsTrue(JToken.DeepEquals(new JValue(Encoding.UTF8.GetBytes("Hi")), (JValue)Encoding.UTF8.GetBytes("Hi")));
Assert.IsTrue(JToken.DeepEquals(new JValue(TimeSpan.FromMinutes(1)), (JValue)TimeSpan.FromMinutes(1)));
Assert.IsTrue(JToken.DeepEquals(new JValue((object)null), (JValue)(TimeSpan?)null));
Assert.IsTrue(JToken.DeepEquals(JValue.CreateNull(), (JValue)(TimeSpan?)null));
Assert.IsTrue(JToken.DeepEquals(new JValue(TimeSpan.FromMinutes(1)), (JValue)(TimeSpan?)TimeSpan.FromMinutes(1)));
Assert.IsTrue(JToken.DeepEquals(new JValue(new Guid("46EFE013-B56A-4E83-99E4-4DCE7678A5BC")), (JValue)new Guid("46EFE013-B56A-4E83-99E4-4DCE7678A5BC")));
Assert.IsTrue(JToken.DeepEquals(new JValue(new Uri("http://www.google.com")), (JValue)new Uri("http://www.google.com")));
Assert.IsTrue(JToken.DeepEquals(new JValue((object)null), (JValue)(Uri)null));
Assert.IsTrue(JToken.DeepEquals(new JValue((object)null), (JValue)(Guid?)null));
Assert.IsTrue(JToken.DeepEquals(JValue.CreateNull(), (JValue)(Uri)null));
Assert.IsTrue(JToken.DeepEquals(JValue.CreateNull(), (JValue)(Guid?)null));
}
[Test]
+17 -4
View File
@@ -52,6 +52,19 @@ namespace Newtonsoft.Json.Tests.Linq
[TestFixture]
public class JValueTests : TestFixtureBase
{
[Test]
public void UndefinedTests()
{
JValue v = JValue.CreateUndefined();
Assert.AreEqual(JTokenType.Undefined, v.Type);
Assert.AreEqual(null, v.Value);
Assert.AreEqual("", v.ToString());
Assert.AreEqual("undefined", v.ToString(Formatting.None));
}
[Test]
public void FloatParseHandling()
{
@@ -183,10 +196,10 @@ namespace Newtonsoft.Json.Tests.Linq
v = new JValue("I am a string!");
Assert.AreEqual("I am a string!", v.ToString());
v = new JValue(null, JTokenType.Null);
v = JValue.CreateNull();
Assert.AreEqual("", v.ToString());
v = new JValue(null, JTokenType.Null);
v = JValue.CreateNull();
Assert.AreEqual("", v.ToString(null, CultureInfo.InvariantCulture));
v = new JValue(new DateTime(2000, 12, 12, 20, 59, 59, DateTimeKind.Utc), JTokenType.Date);
@@ -333,7 +346,7 @@ namespace Newtonsoft.Json.Tests.Linq
ExceptionAssert.Throws<ArgumentException>("Can not convert Null to Int32.",
() =>
{
JValue v = new JValue((object)null);
JValue v = JValue.CreateNull();
int i = (int)v;
});
}
@@ -577,7 +590,7 @@ namespace Newtonsoft.Json.Tests.Linq
[Test]
public void ConvertsToString_Null()
{
Assert.AreEqual(string.Empty, Convert.ToString(new JValue((object)null)));
Assert.AreEqual(string.Empty, Convert.ToString(JValue.CreateNull()));
}
[Test]
@@ -75,4 +75,4 @@ using System.Security;
// by using the '*' as shown below:
[assembly: AssemblyVersion("6.0.0.0")]
[assembly: AssemblyFileVersion("6.0.3.17227")]
[assembly: AssemblyFileVersion("6.0.3.17229")]
+3 -3
View File
@@ -292,7 +292,7 @@ namespace Newtonsoft.Json.Linq
internal JToken EnsureParentToken(JToken item, bool skipParentCheck)
{
if (item == null)
return new JValue((object)null);
return JValue.CreateNull();
if (skipParentCheck)
return item;
@@ -713,12 +713,12 @@ namespace Newtonsoft.Json.Linq
parent.Add(v);
break;
case JsonToken.Null:
v = new JValue(null, JTokenType.Null);
v = JValue.CreateNull();
v.SetLineInfo(lineInfo);
parent.Add(v);
break;
case JsonToken.Undefined:
v = new JValue(null, JTokenType.Undefined);
v = JValue.CreateUndefined();
v.SetLineInfo(lineInfo);
parent.Add(v);
break;
+1 -1
View File
@@ -70,7 +70,7 @@ namespace Newtonsoft.Json.Linq
{
CheckReentrancy();
JToken newValue = value ?? new JValue((object)null);
JToken newValue = value ?? JValue.CreateNull();
if (_content.Count == 0)
{
+2 -2
View File
@@ -1861,11 +1861,11 @@ namespace Newtonsoft.Json.Linq
v.SetLineInfo(lineInfo);
return v;
case JsonToken.Null:
v = new JValue(null, JTokenType.Null);
v = JValue.CreateNull();
v.SetLineInfo(lineInfo);
return v;
case JsonToken.Undefined:
v = new JValue(null, JTokenType.Undefined);
v = JValue.CreateUndefined();
v.SetLineInfo(lineInfo);
return v;
default:
+1 -1
View File
@@ -176,7 +176,7 @@ namespace Newtonsoft.Json.Linq
}
else
{
_value = value ?? new JValue((object)null);
_value = value ?? JValue.CreateNull();
}
}
+18
View File
@@ -541,6 +541,24 @@ namespace Newtonsoft.Json.Linq
return new JValue(value, JTokenType.String);
}
/// <summary>
/// Creates a <see cref="JValue"/> null value.
/// </summary>
/// <returns>A <see cref="JValue"/> null value.</returns>
public static JValue CreateNull()
{
return new JValue(null, JTokenType.Null);
}
/// <summary>
/// Creates a <see cref="JValue"/> null value.
/// </summary>
/// <returns>A <see cref="JValue"/> null value.</returns>
public static JValue CreateUndefined()
{
return new JValue(null, JTokenType.Undefined);
}
private static JTokenType GetValueType(JTokenType? current, object value)
{
if (value == null)
@@ -91,5 +91,5 @@ using System.Security;
// by using the '*' as shown below:
[assembly: AssemblyVersion("6.0.0.0")]
[assembly: AssemblyFileVersion("6.0.3.17227")]
[assembly: AssemblyFileVersion("6.0.3.17229")]
[assembly: CLSCompliant(true)]
@@ -485,7 +485,7 @@ namespace Newtonsoft.Json.Serialization
// convert object value to JToken so it is compatible with dictionary
// could happen because of primitive types, type name handling and references
if (isJTokenValueType && !(value is JToken))
value = (value != null) ? JToken.FromObject(value) : new JValue(null, JTokenType.Null);
value = (value != null) ? JToken.FromObject(value) : JValue.CreateNull();
setExtensionDataDictionaryValue(dictionary, key, value);
};