Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/Newtonsoft.Json.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJamesNK <james@newtonking.com>2010-07-03 13:53:57 +0400
committerJamesNK <james@newtonking.com>2010-07-03 13:53:57 +0400
commitc2800930ab4a66710c56d5aa6c3fb32269af0446 (patch)
tree1942e2b998c3e4be0c7dc55254a333e324f0e007 /Src/Newtonsoft.Json.Tests
parentc9ec2289421792fc28a6a37e700e8da633a3fe92 (diff)
-Added DateTimeKindHandling to BsonWriter to control how a DateTime is converted prior to being serialized
-Added Required.Always attribute validation when writing JSON as well reading -Added TypeNameHandling.Auto to automatically write the type name when a value doesn't match the declared type -Fixed reading multi-byte strings in BSON
Diffstat (limited to 'Src/Newtonsoft.Json.Tests')
-rw-r--r--Src/Newtonsoft.Json.Tests/Bson/BsonWriterTests.cs40
-rw-r--r--Src/Newtonsoft.Json.Tests/PerformanceTests.cs8
-rw-r--r--Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs20
-rw-r--r--Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs132
-rw-r--r--Src/Newtonsoft.Json.Tests/TestObjects/HolderClass.cs2
5 files changed, 140 insertions, 62 deletions
diff --git a/Src/Newtonsoft.Json.Tests/Bson/BsonWriterTests.cs b/Src/Newtonsoft.Json.Tests/Bson/BsonWriterTests.cs
index ca64132..b90e74a 100644
--- a/Src/Newtonsoft.Json.Tests/Bson/BsonWriterTests.cs
+++ b/Src/Newtonsoft.Json.Tests/Bson/BsonWriterTests.cs
@@ -558,5 +558,45 @@ namespace Newtonsoft.Json.Tests.Bson
Assert.IsFalse(reader.Read());
}
+
+ [Test]
+ public void WriteDateTimes()
+ {
+ MemoryStream ms = new MemoryStream();
+ BsonWriter writer = new BsonWriter(ms);
+ writer.DateTimeKindHandling = DateTimeKind.Unspecified;
+
+ writer.WriteStartArray();
+ writer.WriteValue(new DateTime(2000, 10, 12, 20, 55, 0, DateTimeKind.Utc));
+ writer.WriteValue(new DateTime(2000, 10, 12, 20, 55, 0, DateTimeKind.Local));
+ writer.WriteValue(new DateTime(2000, 10, 12, 20, 55, 0, DateTimeKind.Unspecified));
+ writer.WriteEndArray();
+
+ ms.Seek(0, SeekOrigin.Begin);
+
+ BsonReader reader = new BsonReader(ms);
+ reader.ReadRootValueAsArray = true;
+ reader.DateTimeKindHandling = DateTimeKind.Utc;
+
+ Assert.IsTrue(reader.Read());
+ Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
+
+ Assert.IsTrue(reader.Read());
+ Assert.AreEqual(JsonToken.Date, reader.TokenType);
+ Assert.AreEqual(new DateTime(2000, 10, 12, 20, 55, 0, DateTimeKind.Utc), reader.Value);
+
+ Assert.IsTrue(reader.Read());
+ Assert.AreEqual(JsonToken.Date, reader.TokenType);
+ Assert.AreEqual(new DateTime(2000, 10, 12, 20, 55, 0, DateTimeKind.Utc), reader.Value);
+
+ Assert.IsTrue(reader.Read());
+ Assert.AreEqual(JsonToken.Date, reader.TokenType);
+ Assert.AreEqual(new DateTime(2000, 10, 12, 20, 55, 0, DateTimeKind.Utc), reader.Value);
+
+ Assert.IsTrue(reader.Read());
+ Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
+
+ Assert.IsFalse(reader.Read());
+ }
}
} \ No newline at end of file
diff --git a/Src/Newtonsoft.Json.Tests/PerformanceTests.cs b/Src/Newtonsoft.Json.Tests/PerformanceTests.cs
index 4555ec7..d31ab1d 100644
--- a/Src/Newtonsoft.Json.Tests/PerformanceTests.cs
+++ b/Src/Newtonsoft.Json.Tests/PerformanceTests.cs
@@ -34,8 +34,8 @@ namespace Newtonsoft.Json.Tests
public class PerformanceTests : TestFixtureBase
{
- //private const int Iterations = 100;
- private const int Iterations = 5000;
+ private const int Iterations = 100;
+ //private const int Iterations = 5000;
#region Data
private const string BsonHex =
@@ -508,7 +508,7 @@ namespace Newtonsoft.Json.Tests
TimeOperation<object>(() =>
{
- for (int i = 0; i < 5000; i++)
+ for (int i = 0; i < Iterations; i++)
{
test["dummy"] = new JValue(i);
Encoding.UTF8.GetBytes(test.ToString(Formatting.None));
@@ -526,7 +526,7 @@ namespace Newtonsoft.Json.Tests
TimeOperation<object>(() =>
{
- for (int i = 0; i < 5000; i++)
+ for (int i = 0; i < Iterations; i++)
{
test["dummy"] = new JValue(i);
ms.Seek(0, SeekOrigin.Begin);
diff --git a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs
index 91d6067..5c5a84f 100644
--- a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs
+++ b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs
@@ -1012,7 +1012,7 @@ keyword such as type of business.""
}
[Test]
- public void RequiredMembersClassWithNullValues()
+ public void DeserializeRequiredMembersClassWithNullValues()
{
string json = @"{
""FirstName"": ""I can't be null bro!"",
@@ -1030,7 +1030,7 @@ keyword such as type of business.""
[Test]
[ExpectedException(typeof(JsonSerializationException), ExpectedMessage = "Required property 'FirstName' expects a value but got null.")]
- public void RequiredMembersClassNullRequiredValueProperty()
+ public void DeserializeRequiredMembersClassNullRequiredValueProperty()
{
string json = @"{
""FirstName"": null,
@@ -1043,6 +1043,22 @@ keyword such as type of business.""
}
[Test]
+ [ExpectedException(typeof(JsonSerializationException), ExpectedMessage = "Cannot write a null value for property 'FirstName'. Property requires a value.")]
+ public void SerializeRequiredMembersClassNullRequiredValueProperty()
+ {
+ RequiredMembersClass requiredMembersClass = new RequiredMembersClass
+ {
+ FirstName = null,
+ BirthDate = new DateTime(2000, 10, 10, 10, 10, 10, DateTimeKind.Utc),
+ LastName = null,
+ MiddleName = null
+ };
+
+ string json = JsonConvert.SerializeObject(requiredMembersClass);
+ Console.WriteLine(json);
+ }
+
+ [Test]
[ExpectedException(typeof(JsonSerializationException), ExpectedMessage = "Required property 'LastName' not found in JSON.")]
public void RequiredMembersClassMissingRequiredProperty()
{
diff --git a/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs
index 382e0d2..06b0315 100644
--- a/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs
+++ b/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs
@@ -461,60 +461,82 @@ namespace Newtonsoft.Json.Tests.Serialization
}
#endif
- //[Test]
- //[Ignore("TODO")]
- //public void CollectionWithAbstractItems()
- //{
- // string Text = "";
-
- // HolderClass testObject = new HolderClass();
- // testObject.TestMember = new ContentSubClass("First One");
- // testObject.AnotherTestMember = new Dictionary<int, List<ContentBaseClass>>();
- // testObject.AnotherTestMember.Add(1, new List<ContentBaseClass>());
- // testObject.AnotherTestMember[1].Add(new ContentSubClass("Second One"));
- // testObject.AThirdTestMember = new ContentSubClass("Third One");
-
- // HolderClass anotherTestObject = null;
-
- // Newtonsoft.Json.JsonSerializer serializingTester = new Newtonsoft.Json.JsonSerializer();
- // serializingTester.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
-
- // StringWriter sw = new StringWriter();
- // using (Newtonsoft.Json.JsonTextWriter jsonWriter = new Newtonsoft.Json.JsonTextWriter(sw))
- // {
- // jsonWriter.Formatting = Newtonsoft.Json.Formatting.Indented;
-
- // if (Text.Equals("Properties All, Serializer All"))
- // serializingTester.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.All;
- // else if (Text.Equals("Properties All, Serializer Objects"))
- // serializingTester.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Objects;
- // else if (Text.Equals("Properties All, Serializer Arrays"))
- // serializingTester.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Arrays;
- // //otherwise leave default, which looks like it's "None"
-
- // serializingTester.Serialize(jsonWriter, testObject);
- // }
-
- // string json = sw.ToString();
-
- // Console.WriteLine(json);
-
- // StringReader sr = new StringReader(json);
-
- // Newtonsoft.Json.JsonSerializer deserializingTester = new Newtonsoft.Json.JsonSerializer();
-
- // using (Newtonsoft.Json.JsonTextReader jsonReader = new Newtonsoft.Json.JsonTextReader(sr))
- // {
- // if (Text.Equals("Properties All, Serializer All"))
- // deserializingTester.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.All;
- // else if (Text.Equals("Properties All, Serializer Objects"))
- // deserializingTester.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Objects;
- // else if (Text.Equals("Properties All, Serializer Arrays"))
- // deserializingTester.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Arrays;
- // //otherwise leave default, which looks like it's "None"
-
- // anotherTestObject = deserializingTester.Deserialize<HolderClass>(jsonReader);
- // }
- //}
+ [Test]
+ public void CollectionWithAbstractItems()
+ {
+ HolderClass testObject = new HolderClass();
+ testObject.TestMember = new ContentSubClass("First One");
+ testObject.AnotherTestMember = new Dictionary<int, IList<ContentBaseClass>>();
+ testObject.AnotherTestMember.Add(1, new List<ContentBaseClass>());
+ testObject.AnotherTestMember[1].Add(new ContentSubClass("Second One"));
+ testObject.AThirdTestMember = new ContentSubClass("Third One");
+
+
+ JsonSerializer serializingTester = new JsonSerializer();
+ serializingTester.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
+
+ StringWriter sw = new StringWriter();
+ using (JsonTextWriter jsonWriter = new JsonTextWriter(sw))
+ {
+ jsonWriter.Formatting = Formatting.Indented;
+ serializingTester.TypeNameHandling = TypeNameHandling.Auto;
+ serializingTester.Serialize(jsonWriter, testObject);
+ }
+
+ string json = sw.ToString();
+
+ string contentSubClassRef = ReflectionUtils.GetTypeName(typeof(ContentSubClass), FormatterAssemblyStyle.Simple);
+ string dictionaryRef = ReflectionUtils.GetTypeName(typeof(Dictionary<int, IList<ContentBaseClass>>), FormatterAssemblyStyle.Simple);
+ string listRef = ReflectionUtils.GetTypeName(typeof(List<ContentBaseClass>), FormatterAssemblyStyle.Simple);
+
+
+ Assert.AreEqual(@"{
+ ""TestMember"": {
+ ""$type"": """ + contentSubClassRef + @""",
+ ""SomeString"": ""First One""
+ },
+ ""AnotherTestMember"": {
+ ""$type"": """ + dictionaryRef + @""",
+ ""1"": {
+ ""$type"": """ + listRef + @""",
+ ""$values"": [
+ {
+ ""$type"": """ + contentSubClassRef + @""",
+ ""SomeString"": ""Second One""
+ }
+ ]
+ }
+ },
+ ""AThirdTestMember"": {
+ ""$type"": """ + contentSubClassRef + @""",
+ ""SomeString"": ""Third One""
+ }
+}", json);
+ Console.WriteLine(json);
+
+ StringReader sr = new StringReader(json);
+
+ JsonSerializer deserializingTester = new JsonSerializer();
+
+ HolderClass anotherTestObject;
+
+ using (JsonTextReader jsonReader = new JsonTextReader(sr))
+ {
+ deserializingTester.TypeNameHandling = TypeNameHandling.Auto;
+
+ anotherTestObject = deserializingTester.Deserialize<HolderClass>(jsonReader);
+ }
+
+ Assert.IsNotNull(anotherTestObject);
+ Assert.IsInstanceOfType(typeof(ContentSubClass), anotherTestObject.TestMember);
+ Assert.IsInstanceOfType(typeof(Dictionary<int, IList<ContentBaseClass>>), anotherTestObject.AnotherTestMember);
+ Assert.AreEqual(1, anotherTestObject.AnotherTestMember.Count);
+
+ IList<ContentBaseClass> list = anotherTestObject.AnotherTestMember[1];
+
+ Assert.IsInstanceOfType(typeof(List<ContentBaseClass>), list);
+ Assert.AreEqual(1, list.Count);
+ Assert.IsInstanceOfType(typeof(ContentSubClass), list[0]);
+ }
}
} \ No newline at end of file
diff --git a/Src/Newtonsoft.Json.Tests/TestObjects/HolderClass.cs b/Src/Newtonsoft.Json.Tests/TestObjects/HolderClass.cs
index 27b26f4..14bcd5e 100644
--- a/Src/Newtonsoft.Json.Tests/TestObjects/HolderClass.cs
+++ b/Src/Newtonsoft.Json.Tests/TestObjects/HolderClass.cs
@@ -13,7 +13,7 @@ namespace Newtonsoft.Json.Tests.TestObjects
public ContentBaseClass TestMember { get; set; }
[Newtonsoft.Json.JsonProperty(TypeNameHandling = Newtonsoft.Json.TypeNameHandling.All)]
- public Dictionary<int, List<ContentBaseClass>> AnotherTestMember { get; set; }
+ public Dictionary<int, IList<ContentBaseClass>> AnotherTestMember { get; set; }
public ContentBaseClass AThirdTestMember { get; set; }