Specified CultureInfo when converting to and from strings across Json.NET.

Fixes:
Clone JToken when adding token with a parent to a new token.
This commit is contained in:
JamesNK
2008-03-16 03:04:28 +00:00
parent 010177a27d
commit 3d150fe2c3
32 changed files with 512 additions and 162 deletions
+88
View File
@@ -0,0 +1,88 @@
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);
}
}
}
@@ -66,5 +66,27 @@ now brown cow?", '"', true);
result = JavaScriptUtils.ToEscapedJavaScriptString(@"Fred's ""cat"".", '\'', true);
Assert.AreEqual(result, @"'Fred\'s ""cat"".'");
}
[Test]
[ExpectedException(typeof(ArgumentException), ExpectedMessage = "Unsupported type: System.Version. Use the JsonSerializer class to get the object's JSON representation.")]
public void ToStringInvalid()
{
JavaScriptConvert.ToString(new Version());
}
[Test]
public void GuidToString()
{
Guid guid = new Guid("BED7F4EA-1A96-11d2-8F08-00A0C9A6186D");
string json = JavaScriptConvert.ToString(guid);
Assert.AreEqual(@"""bed7f4ea-1a96-11d2-8f08-00a0c9a6186d""", json);
}
[Test]
public void EnumToString()
{
string json = JavaScriptConvert.ToString(StringComparison.CurrentCultureIgnoreCase);
Assert.AreEqual("1", json);
}
}
}
+46 -19
View File
@@ -118,6 +118,11 @@ namespace Newtonsoft.Json.Tests
White
}
public class ClassWithGuid
{
public Guid GuidField;
}
public class ClassWithArray
{
private readonly IList<long> bar;
@@ -135,13 +140,31 @@ namespace Newtonsoft.Json.Tests
set { foo = value; }
}
[JsonProperty("bar")]
[JsonProperty(PropertyName = "bar")]
public IList<long> Bar
{
get { return bar; }
}
}
public class ConverableMembers
{
public string String = "string";
public int Int32 = int.MaxValue;
public uint UInt32 = uint.MaxValue;
public byte Byte = byte.MaxValue;
public sbyte SByte = sbyte.MaxValue;
public short Short = short.MaxValue;
public ushort UShort = ushort.MaxValue;
public long Long = long.MaxValue;
public ulong ULong = long.MaxValue;
public double Double = double.MaxValue;
public float Float = float.MaxValue;
public DBNull DBNull = DBNull.Value;
public bool Bool = true;
public char Char = '\0';
}
public class JsonSerializerTest : TestFixtureBase
{
[Test]
@@ -872,24 +895,6 @@ keyword such as type of business.""
Assert.AreEqual(3, wibbleOut.Bar[4]);
}
public class ConverableMembers
{
public string String = "string";
public int Int32 = int.MaxValue;
public uint UInt32 = uint.MaxValue;
public byte Byte = byte.MaxValue;
public sbyte SByte = sbyte.MaxValue;
public short Short = short.MaxValue;
public ushort UShort = ushort.MaxValue;
public long Long = long.MaxValue;
public ulong ULong = long.MaxValue;
public double Double = double.MaxValue;
public float Float = float.MaxValue;
public DBNull DBNull = DBNull.Value;
public bool Bool = true;
public char Char = '\0';
}
[Test]
public void SerializeConverableObjects()
{
@@ -914,5 +919,27 @@ keyword such as type of business.""
string json = JavaScriptConvert.SerializeObject(s);
Assert.AreEqual("[3,2,1]", json);
}
[Test]
public void GuidTest()
{
Guid guid = new Guid("BED7F4EA-1A96-11d2-8F08-00A0C9A6186D");
string json = JavaScriptConvert.SerializeObject(new ClassWithGuid { GuidField = guid });
Assert.AreEqual(@"{""GuidField"":""bed7f4ea-1a96-11d2-8f08-00a0c9a6186d""}", json);
ClassWithGuid c = JavaScriptConvert.DeserializeObject<ClassWithGuid>(json);
Assert.AreEqual(guid, c.GuidField);
}
[Test]
public void EnumTest()
{
string json = JavaScriptConvert.SerializeObject(StringComparison.CurrentCultureIgnoreCase);
Assert.AreEqual(@"1", json);
StringComparison s = JavaScriptConvert.DeserializeObject<StringComparison>(json);
Assert.AreEqual(StringComparison.CurrentCultureIgnoreCase, s);
}
}
}
+3 -3
View File
@@ -75,7 +75,7 @@ namespace Newtonsoft.Json.Tests
string json = @"[0,1,2,3,4,5,6,7,8,9]";
JArray a = JArray.Parse(json);
List<int> list = a.Children<int>().ToList();
List<int> list = a.Values<int>().ToList();
List<int> expected = new List<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
@@ -345,7 +345,7 @@ keyword such as type of business.""
{
List<Post> posts = GetPosts();
JObject rss =
JObject rss =
new JObject(
new JProperty("channel",
new JObject(
@@ -549,7 +549,7 @@ keyword such as type of business.""
JObject o = JObject.FromObject(new
{
channel = new
channel = new
{
title = "James Newton-King",
link = "http://james.newtonking.com",
@@ -64,6 +64,9 @@
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="DateTimeTestClass.cs" />
@@ -75,6 +78,7 @@
<Compile Include="JsonTextWriterTest.cs" />
<Compile Include="JsonTokenReaderTest.cs" />
<Compile Include="JsonTokenWriterTest.cs" />
<Compile Include="JTokenTests.cs" />
<Compile Include="LinqToJsonTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TestFixtureBase.cs" />