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:
JamesNK
2008-04-04 22:45:52 +00:00
parent e469918596
commit ffe2d91279
34 changed files with 1104 additions and 262 deletions
@@ -1,4 +1,29 @@
using System;
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -7,7 +32,7 @@ using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Utilities;
using System.Globalization;
namespace Newtonsoft.Json.Tests
namespace Newtonsoft.Json.Tests.Converters
{
public class IsoDateTimeConverterTests : TestFixtureBase
{
@@ -1,11 +1,36 @@
using System;
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Newtonsoft.Json.Converters;
namespace Newtonsoft.Json.Tests
namespace Newtonsoft.Json.Tests.Converters
{
public class JavaScriptDateTimeConverterTests : TestFixtureBase
{
@@ -31,7 +31,7 @@ using System.Xml;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Utilities;
namespace Newtonsoft.Json.Tests
namespace Newtonsoft.Json.Tests.Converters
{
public class XmlNodeConverterTest : TestFixtureBase
{
+26 -1
View File
@@ -1,4 +1,29 @@
using System;
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
-88
View File
@@ -1,88 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Newtonsoft.Json.Linq;
using System.Xml.Linq;
namespace Newtonsoft.Json.Tests
{
public class JTokenTests : TestFixtureBase
{
[Test]
public void Parent()
{
JValue v = new JValue(new DateTime(2000, 12, 20));
Assert.AreEqual(null, v.Parent);
JObject o =
new JObject(
new JProperty("Test1", v),
new JProperty("Test2", "Test2Value"),
new JProperty("Test3", "Test3Value"),
new JProperty("Test4", null)
);
Assert.AreEqual(o.Property("Test1"), v.Parent);
JProperty p = new JProperty("NewProperty", v);
// existing value should still have same parent
Assert.AreEqual(o.Property("Test1"), v.Parent);
// new value should be cloned
Assert.AreNotEqual(p.Value, v);
Assert.AreEqual((DateTime)p.Value, (DateTime)v.Value);
Assert.AreEqual(v, o["Test1"]);
XText t = new XText("XText");
Assert.AreEqual(null, t.Parent);
Assert.AreEqual(null, o.Parent);
JProperty o1 = new JProperty("O1", o);
Assert.AreEqual(o, o1.Value);
Assert.AreNotEqual(null, o.Parent);
JProperty o2 = new JProperty("O2", o);
Assert.AreNotEqual(o1.Value, o2.Value);
Assert.AreEqual(o1.Value.Children().Count(), o2.Value.Children().Count());
Assert.AreEqual(false, JToken.DeepEquals(o1, o2));
Assert.AreEqual(true, JToken.DeepEquals(o1.Value, o2.Value));
}
[Test]
public void Children()
{
JArray a =
new JArray(
5,
new JArray(1, 2, 3),
new JArray(1, 2, 3),
new JArray(1, 2, 3)
);
Assert.AreEqual(4, a.Count());
Assert.AreEqual(3, a.Children<JArray>().Count());
}
[Test]
public void Casting()
{
//JToken t = new JValue(new DateTime(2000, 12, 20));
//DateTime d = (DateTime)t;
Assert.AreEqual(new DateTime(2000, 12, 20), (DateTime)new JValue(new DateTime(2000, 12, 20)));
Assert.AreEqual(new DateTimeOffset(2000, 12, 20, 23, 50, 10, TimeSpan.Zero), (DateTimeOffset)new JValue(new DateTimeOffset(2000, 12, 20, 23, 50, 10, TimeSpan.Zero)));
Assert.AreEqual(true, (bool)new JValue(true));
Assert.AreEqual(true, (bool?)new JValue(true));
Assert.AreEqual(null, (bool?)((JValue)null));
Assert.AreEqual(null, (bool?)JValue.Null);
Assert.AreEqual(null, (bool?)JValue.Undefined);
}
}
}
@@ -1,3 +1,28 @@
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.Text;
+26 -25
View File
@@ -147,6 +147,7 @@ namespace Newtonsoft.Json.Tests
}
}
[JsonObject]
public class ConverableMembers
{
public string String = "string";
@@ -165,6 +166,31 @@ namespace Newtonsoft.Json.Tests
public char Char = '\0';
}
[JsonObject(MemberSerialization.OptIn)]
public class JsonIgnoreAttributeOnClassTestClass
{
private int _property = 21;
private int _ignoredProperty = 12;
[JsonProperty("TheField")]
public int Field;
[JsonProperty]
public int Property
{
get { return _property; }
}
public int IgnoredField;
[JsonProperty]
[JsonIgnore] // JsonIgnore should take priority
public int IgnoredProperty
{
get { return _ignoredProperty; }
}
}
public class JsonSerializerTest : TestFixtureBase
{
[Test]
@@ -942,31 +968,6 @@ keyword such as type of business.""
Assert.AreEqual(StringComparison.CurrentCultureIgnoreCase, s);
}
[JsonObject(MemberSerialization.OptIn)]
public class JsonIgnoreAttributeOnClassTestClass
{
private int _property = 21;
private int _ignoredProperty = 12;
[JsonProperty("TheField")]
public int Field;
[JsonProperty]
public int Property
{
get { return _property; }
}
public int IgnoredField;
[JsonProperty]
[JsonIgnore] // JsonIgnore should take priority
public int IgnoredProperty
{
get { return _ignoredProperty; }
}
}
[Test]
public void JsonIgnoreAttributeOnClassTest()
{
@@ -207,7 +207,7 @@ namespace Newtonsoft.Json.Tests
{
string input = @"{
CPU: 'Intel',
Drives: [
Drives: [ /* Com*ment */
'DVD read/writer',
""500 gigabyte hard drive""
]
@@ -0,0 +1,467 @@
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Newtonsoft.Json.Linq;
using System.Xml.Linq;
namespace Newtonsoft.Json.Tests.Linq
{
public class JTokenTests : TestFixtureBase
{
[Test]
public void Parent()
{
JArray v = new JArray(new JConstructor("TestConstructor"), new JValue(new DateTime(2000, 12, 20)));
Assert.AreEqual(null, v.Parent);
JObject o =
new JObject(
new JProperty("Test1", v),
new JProperty("Test2", "Test2Value"),
new JProperty("Test3", "Test3Value"),
new JProperty("Test4", null)
);
Assert.AreEqual(o.Property("Test1"), v.Parent);
JProperty p = new JProperty("NewProperty", v);
// existing value should still have same parent
Assert.AreEqual(o.Property("Test1"), v.Parent);
// new value should be cloned
Assert.AreNotEqual(p.Value, v);
Assert.AreEqual((DateTime)((JValue)p.Value[1]).Value, (DateTime)((JValue)v[1]).Value);
Assert.AreEqual(v, o["Test1"]);
XText t = new XText("XText");
Assert.AreEqual(null, t.Parent);
Assert.AreEqual(null, o.Parent);
JProperty o1 = new JProperty("O1", o);
Assert.AreEqual(o, o1.Value);
Assert.AreNotEqual(null, o.Parent);
JProperty o2 = new JProperty("O2", o);
Assert.AreNotEqual(o1.Value, o2.Value);
Assert.AreEqual(o1.Value.Children().Count(), o2.Value.Children().Count());
Assert.AreEqual(false, JToken.DeepEquals(o1, o2));
Assert.AreEqual(true, JToken.DeepEquals(o1.Value, o2.Value));
}
[Test]
public void Next()
{
JArray a =
new JArray(
5,
6,
new JArray(7, 8),
new JArray(9, 10)
);
JToken next = a[0].Next;
Assert.AreEqual(6, (int)next);
next = next.Next;
Assert.IsTrue(JToken.DeepEquals(new JArray(7, 8), next));
next = next.Next;
Assert.IsTrue(JToken.DeepEquals(new JArray(9, 10), next));
next = next.Next;
Assert.IsNull(next);
}
[Test]
public void Previous()
{
JArray a =
new JArray(
5,
6,
new JArray(7, 8),
new JArray(9, 10)
);
JToken previous = a[3].Previous;
Assert.IsTrue(JToken.DeepEquals(new JArray(7, 8), previous));
previous = previous.Previous;
Assert.AreEqual(6, (int)previous);
previous = previous.Previous;
Assert.AreEqual(5, (int)previous);
previous = previous.Previous;
Assert.IsNull(previous);
}
[Test]
public void Children()
{
JArray a =
new JArray(
5,
new JArray(1),
new JArray(1, 2),
new JArray(1, 2, 3)
);
Assert.AreEqual(4, a.Count());
Assert.AreEqual(3, a.Children<JArray>().Count());
}
[Test]
public void BeforeAfter()
{
JArray a =
new JArray(
5,
new JArray(1, 2, 3),
new JArray(1, 2, 3),
new JArray(1, 2, 3)
);
Assert.AreEqual(5, (int)a[1].Previous);
Assert.AreEqual(2, a[2].BeforeSelf().Count());
//Assert.AreEqual(2, a[2].AfterSelf().Count());
}
[Test]
public void Casting()
{
Assert.AreEqual(new DateTime(2000, 12, 20), (DateTime)new JValue(new DateTime(2000, 12, 20)));
Assert.AreEqual(new DateTimeOffset(2000, 12, 20, 23, 50, 10, TimeSpan.Zero), (DateTimeOffset)new JValue(new DateTimeOffset(2000, 12, 20, 23, 50, 10, TimeSpan.Zero)));
Assert.AreEqual(true, (bool)new JValue(true));
Assert.AreEqual(true, (bool?)new JValue(true));
Assert.AreEqual(null, (bool?)((JValue)null));
Assert.AreEqual(null, (bool?)JValue.Null);
Assert.AreEqual(null, (bool?)JValue.Undefined);
Assert.AreEqual(10, (long)new JValue(10));
Assert.AreEqual(null, (long?)new JValue((long?)null));
Assert.AreEqual(null, (long?)(JValue)null);
Assert.AreEqual(null, (int?)new JValue((int?)null));
Assert.AreEqual(null, (int?)(JValue)null);
Assert.AreEqual(null, (DateTime?)new JValue((DateTime?)null));
Assert.AreEqual(null, (DateTimeOffset?)new JValue((DateTimeOffset?)null));
Assert.AreEqual(null, (DateTime?)(JValue)null);
Assert.AreEqual(null, (DateTimeOffset?)(JValue)null);
Assert.AreEqual(null, (short?)new JValue((short?)null));
Assert.AreEqual(null, (short?)(JValue)null);
Assert.AreEqual(null, (float?)new JValue((float?)null));
Assert.AreEqual(null, (float?)(JValue)null);
Assert.AreEqual(null, (double?)new JValue((double?)null));
Assert.AreEqual(null, (double?)(JValue)null);
Assert.AreEqual(null, (decimal?)new JValue((decimal?)null));
Assert.AreEqual(null, (decimal?)(JValue)null);
Assert.AreEqual(null, (uint?)new JValue((uint?)null));
Assert.AreEqual(null, (uint?)(JValue)null);
Assert.AreEqual(null, (sbyte?)new JValue((sbyte?)null));
Assert.AreEqual(null, (sbyte?)(JValue)null);
Assert.AreEqual(null, (ulong?)new JValue((ulong?)null));
Assert.AreEqual(null, (ulong?)(JValue)null);
Assert.AreEqual(null, (uint?)new JValue((uint?)null));
Assert.AreEqual(null, (uint?)(JValue)null);
Assert.AreEqual(11.1f, (float)new JValue(11.1));
Assert.AreEqual(float.MinValue, (float)new JValue(float.MinValue));
Assert.AreEqual(1.1, (double)new JValue(1.1));
Assert.AreEqual(uint.MaxValue, (uint)new JValue(uint.MaxValue));
Assert.AreEqual(ulong.MaxValue, (ulong)new JValue(ulong.MaxValue));
Assert.AreEqual(ulong.MaxValue, (ulong)new JProperty("Test", new JValue(ulong.MaxValue)));
Assert.AreEqual(null, (string)new JValue((string)null));
}
[Test]
public void Root()
{
JArray a =
new JArray(
5,
6,
new JArray(7, 8),
new JArray(9, 10)
);
Assert.AreEqual(a, a.Root);
Assert.AreEqual(a, a[0].Root);
Assert.AreEqual(a, ((JArray)a[2])[0].Root);
}
[Test]
public void Remove()
{
JToken t;
JArray a =
new JArray(
5,
6,
new JArray(7, 8),
new JArray(9, 10)
);
a[0].Remove();
Assert.AreEqual(6, (int)a[0]);
a[1].Remove();
Assert.AreEqual(6, (int)a[0]);
Assert.IsTrue(JToken.DeepEquals(new JArray(9, 10), a[1]));
Assert.AreEqual(2, a.Count());
t = a[1];
t.Remove();
Assert.AreEqual(6, (int)a[0]);
Assert.IsNull(t.Next);
Assert.IsNull(t.Previous);
Assert.IsNull(t.Parent);
t = a[0];
t.Remove();
Assert.AreEqual(0, a.Count());
Assert.IsNull(t.Next);
Assert.IsNull(t.Previous);
Assert.IsNull(t.Parent);
}
[Test]
public void AfterSelf()
{
JArray a =
new JArray(
5,
new JArray(1),
new JArray(1, 2),
new JArray(1, 2, 3)
);
JToken t = a[1];
List<JToken> afterTokens = t.AfterSelf().ToList();
Assert.AreEqual(2, afterTokens.Count);
Assert.IsTrue(JToken.DeepEquals(new JArray(1, 2), afterTokens[0]));
Assert.IsTrue(JToken.DeepEquals(new JArray(1, 2, 3), afterTokens[1]));
}
[Test]
public void BeforeSelf()
{
JArray a =
new JArray(
5,
new JArray(1),
new JArray(1, 2),
new JArray(1, 2, 3)
);
JToken t = a[2];
List<JToken> beforeTokens = t.BeforeSelf().ToList();
Assert.AreEqual(2, beforeTokens.Count);
Assert.IsTrue(JToken.DeepEquals(new JValue(5), beforeTokens[0]));
Assert.IsTrue(JToken.DeepEquals(new JArray(1), beforeTokens[1]));
}
[Test]
public void HasValues()
{
JArray a =
new JArray(
5,
new JArray(1),
new JArray(1, 2),
new JArray(1, 2, 3)
);
Assert.IsTrue(a.HasValues);
}
[Test]
public void Ancestors()
{
JArray a =
new JArray(
5,
new JArray(1),
new JArray(1, 2),
new JArray(1, 2, 3)
);
JToken t = a[1][0];
List<JToken> ancestors = t.Ancestors().ToList();
Assert.AreEqual(2, ancestors.Count());
Assert.AreEqual(a[1], ancestors[0]);
Assert.AreEqual(a, ancestors[1]);
}
[Test]
public void Descendants()
{
JArray a =
new JArray(
5,
new JArray(1),
new JArray(1, 2),
new JArray(1, 2, 3)
);
List<JToken> descendants = a.Descendants().ToList();
Assert.AreEqual(10, descendants.Count());
Assert.AreEqual(5, (int)descendants[0]);
Assert.IsTrue(JToken.DeepEquals(new JArray(1, 2, 3), descendants[descendants.Count - 4]));
Assert.AreEqual(1, (int)descendants[descendants.Count - 3]);
Assert.AreEqual(2, (int)descendants[descendants.Count - 2]);
Assert.AreEqual(3, (int)descendants[descendants.Count - 1]);
}
[Test]
public void CreateWriter()
{
JArray a =
new JArray(
5,
new JArray(1),
new JArray(1, 2),
new JArray(1, 2, 3)
);
JsonWriter writer = a.CreateWriter();
Assert.IsNotNull(writer);
Assert.AreEqual(4, a.Count());
writer.WriteValue("String");
Assert.AreEqual(5, a.Count());
Assert.AreEqual("String", (string)a[4]);
writer.WriteStartObject();
writer.WritePropertyName("Property");
writer.WriteValue("PropertyValue");
writer.WriteEnd();
Assert.AreEqual(6, a.Count());
Assert.IsTrue(JToken.DeepEquals(new JObject(new JProperty("Property", "PropertyValue")), a[5]));
}
[Test]
public void AddFirst()
{
JArray a =
new JArray(
5,
new JArray(1),
new JArray(1, 2),
new JArray(1, 2, 3)
);
a.AddFirst("First");
Assert.AreEqual("First", (string)a[0]);
Assert.AreEqual(a, a[0].Parent);
Assert.AreEqual(a[1], a[0].Next);
Assert.AreEqual(5, a.Count());
a.AddFirst("NewFirst");
Assert.AreEqual("NewFirst", (string)a[0]);
Assert.AreEqual(a, a[0].Parent);
Assert.AreEqual(a[1], a[0].Next);
Assert.AreEqual(6, a.Count());
Assert.AreEqual(a[0], a[0].Next.Previous);
}
[Test]
public void RemoveAll()
{
JArray a =
new JArray(
5,
new JArray(1),
new JArray(1, 2),
new JArray(1, 2, 3)
);
JToken first = a.First;
Assert.AreEqual(5, (int)first);
a.RemoveAll();
Assert.AreEqual(0, a.Count());
Assert.IsNull(first.Parent);
Assert.IsNull(first.Next);
}
[Test]
[ExpectedException(typeof(Exception), ExpectedMessage = "Can not add Newtonsoft.Json.Linq.JProperty to Newtonsoft.Json.Linq.JArray")]
public void AddPropertyToArray()
{
JArray a = new JArray();
a.Add(new JProperty("PropertyName"));
}
[Test]
[ExpectedException(typeof(Exception), ExpectedMessage = "Can not add Newtonsoft.Json.Linq.JValue to Newtonsoft.Json.Linq.JObject")]
public void AddValueToObject()
{
JObject o = new JObject();
o.Add(5);
}
[Test]
public void Replace()
{
JArray a =
new JArray(
5,
new JArray(1),
new JArray(1, 2),
new JArray(1, 2, 3)
);
a[0].Replace(new JValue(int.MaxValue));
Assert.AreEqual(int.MaxValue, (int)a[0]);
Assert.AreEqual(4, a.Count());
a[1][0].Replace(new JValue("Test"));
Assert.AreEqual("Test", (string)a[1][0]);
a[2].Replace(new JValue(int.MaxValue));
Assert.AreEqual(int.MaxValue, (int)a[2]);
Assert.AreEqual(4, a.Count());
Assert.IsTrue(JToken.DeepEquals(new JArray(int.MaxValue, new JArray("Test"), int.MaxValue, new JArray(1, 2, 3)), a));
}
}
}
@@ -0,0 +1,164 @@
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Newtonsoft.Json.Linq;
namespace Newtonsoft.Json.Tests.Linq
{
public class JValueTests : TestFixtureBase
{
[Test]
public void ChangeValue()
{
JValue v = new JValue(true);
Assert.AreEqual(true, v.Value);
Assert.AreEqual(JsonTokenType.Boolean, v.Type);
v.Value = "Pie";
Assert.AreEqual("Pie", v.Value);
Assert.AreEqual(JsonTokenType.String, v.Type);
v.Value = null;
Assert.AreEqual(null, v.Value);
Assert.AreEqual(JsonTokenType.Null, v.Type);
v.Value = (int?)null;
Assert.AreEqual(null, v.Value);
Assert.AreEqual(JsonTokenType.Null, v.Type);
v.Value = "Pie";
Assert.AreEqual("Pie", v.Value);
Assert.AreEqual(JsonTokenType.String, v.Type);
}
[Test]
public void CreateComment()
{
JValue commentValue = JValue.CreateComment(null);
Assert.AreEqual(null, commentValue.Value);
Assert.AreEqual(JsonTokenType.Comment, commentValue.Type);
commentValue.Value = "Comment";
Assert.AreEqual("Comment", commentValue.Value);
Assert.AreEqual(JsonTokenType.Comment, commentValue.Type);
}
[Test]
public void CreateString()
{
JValue stringValue = JValue.CreateString(null);
Assert.AreEqual(null, stringValue.Value);
Assert.AreEqual(JsonTokenType.String, stringValue.Type);
}
[Test]
[ExpectedException(typeof(InvalidOperationException), ExpectedMessage = "Cannot access child value on Newtonsoft.Json.Linq.JValue.")]
public void Last()
{
JValue v = new JValue(true);
JToken last = v.Last;
}
[Test]
[ExpectedException(typeof(InvalidOperationException), ExpectedMessage = "Cannot access child value on Newtonsoft.Json.Linq.JValue.")]
public void Children()
{
JValue v = new JValue(true);
var c = v.Children();
}
[Test]
[ExpectedException(typeof(InvalidOperationException), ExpectedMessage = "Cannot access child value on Newtonsoft.Json.Linq.JValue.")]
public void First()
{
JValue v = new JValue(true);
JToken first = v.First;
}
[Test]
[ExpectedException(typeof(InvalidOperationException), ExpectedMessage = "Cannot access child value on Newtonsoft.Json.Linq.JValue.")]
public void Item()
{
JValue v = new JValue(true);
JToken first = v[0];
}
[Test]
[ExpectedException(typeof(InvalidOperationException), ExpectedMessage = "Cannot access child value on Newtonsoft.Json.Linq.JValue.")]
public void Values()
{
JValue v = new JValue(true);
v.Values<int>();
}
[Test]
[ExpectedException(typeof(InvalidOperationException), ExpectedMessage = "The parent is missing.")]
public void RemoveParentNull()
{
JValue v = new JValue(true);
v.Remove();
}
[Test]
public void Root()
{
JValue v = new JValue(true);
Assert.AreEqual(v, v.Root);
}
[Test]
public void Previous()
{
JValue v = new JValue(true);
Assert.IsNull(v.Previous);
}
[Test]
public void Next()
{
JValue v = new JValue(true);
Assert.IsNull(v.Next);
}
[Test]
public void DeepEquals()
{
Assert.IsTrue(JToken.DeepEquals(new JValue(5L), new JValue(5)));
Assert.IsFalse(JToken.DeepEquals(new JValue(5M), new JValue(5)));
Assert.IsTrue(JToken.DeepEquals(new JValue((ulong)long.MaxValue), new JValue(long.MaxValue)));
}
[Test]
public void HasValues()
{
Assert.IsFalse((new JValue(5L)).HasValues);
}
}
}
@@ -33,7 +33,7 @@ using System.Xml;
using System.IO;
using Newtonsoft.Json.Converters;
namespace Newtonsoft.Json.Tests
namespace Newtonsoft.Json.Tests.Linq
{
public class LinqToJsonTest : TestFixtureBase
{
@@ -170,6 +170,13 @@ keyword such as type of business.""
List<JObject> resultObjects = o["results"].Children<JObject>().ToList();
Assert.AreEqual(32, resultObjects.Properties().Count());
Assert.AreEqual(32, resultObjects.Cast<JToken>().Values().Count());
Assert.AreEqual(4, resultObjects.Cast<JToken>().Values("GsearchResultClass").Count());
Assert.AreEqual(5, o.PropertyValues().Cast<JArray>().Children().Count());
List<string> resultUrls = o["results"].Children().Values<string>("url").ToList();
@@ -577,5 +584,42 @@ keyword such as type of business.""
Assert.IsInstanceOfType(typeof(JArray), a);
Assert.AreEqual(5, a.Count());
}
[Test]
public void FromAnonDictionary()
{
List<Post> posts = GetPosts();
JObject o = JObject.FromObject(new
{
channel = new Dictionary<string, object>
{
{ "title", "James Newton-King" },
{ "link", "http://james.newtonking.com" },
{ "description", "James Newton-King's blog." },
{ "item",
(from p in posts
orderby p.Title
select new
{
title = p.Title,
description = p.Description,
link = p.Link,
category = p.Categories
})
}
}
});
Console.WriteLine(o.ToString());
Assert.IsInstanceOfType(typeof(JObject), o);
Assert.IsInstanceOfType(typeof(JObject), o["channel"]);
Assert.AreEqual("James Newton-King", (string)o["channel"]["title"]);
Assert.AreEqual(2, o["channel"]["item"].Children().Count());
JArray a = JArray.FromObject(new List<int>() { 0, 1, 2, 3, 4 });
Assert.IsInstanceOfType(typeof(JArray), a);
Assert.AreEqual(5, a.Count());
}
}
}
@@ -70,19 +70,20 @@
</ItemGroup>
<ItemGroup>
<Compile Include="DateTimeTestClass.cs" />
<Compile Include="IsoDateTimeConverterTests.cs" />
<Compile Include="Converters\IsoDateTimeConverterTests.cs" />
<Compile Include="JavaScriptConvertTest.cs" />
<Compile Include="JavaScriptDateTimeConverterTests.cs" />
<Compile Include="Converters\JavaScriptDateTimeConverterTests.cs" />
<Compile Include="JsonSerializerTest.cs" />
<Compile Include="JsonTextReaderTest.cs" />
<Compile Include="JsonTextWriterTest.cs" />
<Compile Include="JsonTokenReaderTest.cs" />
<Compile Include="JsonTokenWriterTest.cs" />
<Compile Include="JTokenTests.cs" />
<Compile Include="LinqToJsonTest.cs" />
<Compile Include="Linq\JTokenTests.cs" />
<Compile Include="Linq\JValueTests.cs" />
<Compile Include="Linq\LinqToJsonTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TestFixtureBase.cs" />
<Compile Include="XmlNodeConverterTest.cs" />
<Compile Include="Converters\XmlNodeConverterTest.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Newtonsoft.Json\Newtonsoft.Json.csproj">
+26 -1
View File
@@ -1,4 +1,29 @@
using System;
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;