/desktop_clients/Visual Studio/Crear beneficiarios/Json100r3/Source/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerCollectionsTests.cs
C# | 2583 lines | 2244 code | 243 blank | 96 comment | 42 complexity | 8db323d978316227d17d73cd5f69840c MD5 | raw file
Possible License(s): GPL-2.0
Large files files are truncated, but you can click here to view the full file
- #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;
- using System.Collections.Concurrent;
- using System.Collections.Specialized;
- using System.Runtime.Serialization;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Globalization;
- using System.IO;
- #if NET20
- using Newtonsoft.Json.Utilities.LinqBridge;
- #else
- using System.Linq;
- #endif
- using System.Text;
- using System.Xml;
- using Newtonsoft.Json.Linq;
- using Newtonsoft.Json.Serialization;
- using Newtonsoft.Json.Tests.TestObjects;
- using Newtonsoft.Json.Tests.TestObjects.Events;
- using Newtonsoft.Json.Tests.TestObjects.Organization;
- using Newtonsoft.Json.Utilities;
- #if DNXCORE50
- using Xunit;
- using Test = Xunit.FactAttribute;
- using Assert = Newtonsoft.Json.Tests.XUnitAssert;
- #else
- using NUnit.Framework;
- #endif
- #if !NET20 && !PORTABLE40
- using System.Xml.Linq;
- #endif
-
- namespace Newtonsoft.Json.Tests.Serialization
- {
- [TestFixture]
- public class JsonSerializerCollectionsTests : TestFixtureBase
- {
- #if !(NET20 || NET35)
- [Test]
- public void SerializeConcurrentQueue()
- {
- ConcurrentQueue<int> queue1 = new ConcurrentQueue<int>();
- queue1.Enqueue(1);
-
- string output = JsonConvert.SerializeObject(queue1);
- Assert.AreEqual(@"[1]", output);
-
- ConcurrentQueue<int> queue2 = JsonConvert.DeserializeObject<ConcurrentQueue<int>>(output);
- int i;
- Assert.IsTrue(queue2.TryDequeue(out i));
- Assert.AreEqual(1, i);
- }
-
- [Test]
- public void SerializeConcurrentBag()
- {
- ConcurrentBag<int> bag1 = new ConcurrentBag<int>();
- bag1.Add(1);
-
- string output = JsonConvert.SerializeObject(bag1);
- Assert.AreEqual(@"[1]", output);
-
- ConcurrentBag<int> bag2 = JsonConvert.DeserializeObject<ConcurrentBag<int>>(output);
- int i;
- Assert.IsTrue(bag2.TryTake(out i));
- Assert.AreEqual(1, i);
- }
-
- [Test]
- public void SerializeConcurrentStack()
- {
- ConcurrentStack<int> stack1 = new ConcurrentStack<int>();
- stack1.Push(1);
-
- string output = JsonConvert.SerializeObject(stack1);
- Assert.AreEqual(@"[1]", output);
-
- ConcurrentStack<int> stack2 = JsonConvert.DeserializeObject<ConcurrentStack<int>>(output);
- int i;
- Assert.IsTrue(stack2.TryPop(out i));
- Assert.AreEqual(1, i);
- }
- #endif
-
- [Test]
- public void DoubleKey_WholeValue()
- {
- Dictionary<double, int> dictionary = new Dictionary<double, int> { { 1d, 1 } };
- string output = JsonConvert.SerializeObject(dictionary);
- Assert.AreEqual(@"{""1"":1}", output);
-
- Dictionary<double, int> deserializedValue = JsonConvert.DeserializeObject<Dictionary<double, int>>(output);
- Assert.AreEqual(1d, deserializedValue.First().Key);
- }
-
- [Test]
- public void DoubleKey_MaxValue()
- {
- Dictionary<double, int> dictionary = new Dictionary<double, int> { { double.MaxValue, 1 } };
- string output = JsonConvert.SerializeObject(dictionary);
- Assert.AreEqual(@"{""1.7976931348623157E+308"":1}", output);
-
- Dictionary<double, int> deserializedValue = JsonConvert.DeserializeObject<Dictionary<double, int>>(output);
- Assert.AreEqual(double.MaxValue, deserializedValue.First().Key);
- }
-
- [Test]
- public void FloatKey_MaxValue()
- {
- Dictionary<float, int> dictionary = new Dictionary<float, int> { { float.MaxValue, 1 } };
- string output = JsonConvert.SerializeObject(dictionary);
- Assert.AreEqual(@"{""3.40282347E+38"":1}", output);
-
- Dictionary<float, int> deserializedValue = JsonConvert.DeserializeObject<Dictionary<float, int>>(output);
- Assert.AreEqual(float.MaxValue, deserializedValue.First().Key);
- }
-
- public class TestCollectionPrivateParameterized : IEnumerable<int>
- {
- private readonly List<int> _bars;
-
- public TestCollectionPrivateParameterized()
- {
- _bars = new List<int>();
- }
-
- [JsonConstructor]
- private TestCollectionPrivateParameterized(IEnumerable<int> bars)
- {
- _bars = new List<int>(bars);
- }
-
- public void Add(int bar)
- {
- _bars.Add(bar);
- }
-
- public IEnumerator<int> GetEnumerator() => _bars.GetEnumerator();
-
- IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
- }
-
- [Test]
- public void CollectionJsonConstructorPrivateParameterized()
- {
- TestCollectionPrivateParameterized c1 = new TestCollectionPrivateParameterized();
- c1.Add(0);
- c1.Add(1);
- c1.Add(2);
- string json = JsonConvert.SerializeObject(c1);
- TestCollectionPrivateParameterized c2 = JsonConvert.DeserializeObject<TestCollectionPrivateParameterized>(json);
-
- List<int> values = c2.ToList();
-
- Assert.AreEqual(3, values.Count);
- Assert.AreEqual(0, values[0]);
- Assert.AreEqual(1, values[1]);
- Assert.AreEqual(2, values[2]);
- }
-
- public class TestCollectionPrivate : List<int>
- {
- [JsonConstructor]
- private TestCollectionPrivate()
- {
- }
-
- public static TestCollectionPrivate Create()
- {
- return new TestCollectionPrivate();
- }
- }
-
- [Test]
- public void CollectionJsonConstructorPrivate()
- {
- TestCollectionPrivate c1 = TestCollectionPrivate.Create();
- c1.Add(0);
- c1.Add(1);
- c1.Add(2);
- string json = JsonConvert.SerializeObject(c1);
- TestCollectionPrivate c2 = JsonConvert.DeserializeObject<TestCollectionPrivate>(json);
-
- List<int> values = c2.ToList();
-
- Assert.AreEqual(3, values.Count);
- Assert.AreEqual(0, values[0]);
- Assert.AreEqual(1, values[1]);
- Assert.AreEqual(2, values[2]);
- }
-
- public class TestCollectionMultipleParameters : List<int>
- {
- [JsonConstructor]
- public TestCollectionMultipleParameters(string s1, string s2)
- {
- }
- }
-
- [Test]
- public void CollectionJsonConstructorMultipleParameters()
- {
- ExceptionAssert.Throws<JsonException>(
- () => JsonConvert.SerializeObject(new TestCollectionMultipleParameters(null, null)),
- "Constructor for 'Newtonsoft.Json.Tests.Serialization.JsonSerializerCollectionsTests+TestCollectionMultipleParameters' must have no parameters or a single parameter that implements 'System.Collections.Generic.IEnumerable`1[System.Int32]'.");
- }
-
- public class TestCollectionBadIEnumerableParameter : List<int>
- {
- [JsonConstructor]
- public TestCollectionBadIEnumerableParameter(List<string> l)
- {
- }
- }
-
- [Test]
- public void CollectionJsonConstructorBadIEnumerableParameter()
- {
- ExceptionAssert.Throws<JsonException>(
- () => JsonConvert.SerializeObject(new TestCollectionBadIEnumerableParameter(null)),
- "Constructor for 'Newtonsoft.Json.Tests.Serialization.JsonSerializerCollectionsTests+TestCollectionBadIEnumerableParameter' must have no parameters or a single parameter that implements 'System.Collections.Generic.IEnumerable`1[System.Int32]'.");
- }
-
- #if !(DNXCORE50 || PORTABLE)
- public class TestCollectionNonGeneric : ArrayList
- {
- [JsonConstructor]
- public TestCollectionNonGeneric(IEnumerable l)
- : base(l.Cast<object>().ToList())
- {
- }
- }
-
- [Test]
- public void CollectionJsonConstructorNonGeneric()
- {
- string json = @"[1,2,3]";
- TestCollectionNonGeneric l = JsonConvert.DeserializeObject<TestCollectionNonGeneric>(json);
-
- Assert.AreEqual(3, l.Count);
- Assert.AreEqual(1, l[0]);
- Assert.AreEqual(2, l[1]);
- Assert.AreEqual(3, l[2]);
- }
- #endif
-
- public class TestDictionaryPrivateParameterized : Dictionary<string, int>
- {
- public TestDictionaryPrivateParameterized()
- {
- }
-
- [JsonConstructor]
- private TestDictionaryPrivateParameterized(IEnumerable<KeyValuePair<string, int>> bars)
- : base(bars.ToDictionary(k => k.Key, k => k.Value))
- {
- }
- }
-
- [Test]
- public void DictionaryJsonConstructorPrivateParameterized()
- {
- TestDictionaryPrivateParameterized c1 = new TestDictionaryPrivateParameterized();
- c1.Add("zero", 0);
- c1.Add("one", 1);
- c1.Add("two", 2);
- string json = JsonConvert.SerializeObject(c1);
- TestDictionaryPrivateParameterized c2 = JsonConvert.DeserializeObject<TestDictionaryPrivateParameterized>(json);
-
- Assert.AreEqual(3, c2.Count);
- Assert.AreEqual(0, c2["zero"]);
- Assert.AreEqual(1, c2["one"]);
- Assert.AreEqual(2, c2["two"]);
- }
-
- public class TestDictionaryPrivate : Dictionary<string, int>
- {
- [JsonConstructor]
- private TestDictionaryPrivate()
- {
- }
-
- public static TestDictionaryPrivate Create()
- {
- return new TestDictionaryPrivate();
- }
- }
-
- [Test]
- public void DictionaryJsonConstructorPrivate()
- {
- TestDictionaryPrivate c1 = TestDictionaryPrivate.Create();
- c1.Add("zero", 0);
- c1.Add("one", 1);
- c1.Add("two", 2);
- string json = JsonConvert.SerializeObject(c1);
- TestDictionaryPrivate c2 = JsonConvert.DeserializeObject<TestDictionaryPrivate>(json);
-
- Assert.AreEqual(3, c2.Count);
- Assert.AreEqual(0, c2["zero"]);
- Assert.AreEqual(1, c2["one"]);
- Assert.AreEqual(2, c2["two"]);
- }
-
- public class TestDictionaryMultipleParameters : Dictionary<string, int>
- {
- [JsonConstructor]
- public TestDictionaryMultipleParameters(string s1, string s2)
- {
- }
- }
-
- [Test]
- public void DictionaryJsonConstructorMultipleParameters()
- {
- ExceptionAssert.Throws<JsonException>(
- () => JsonConvert.SerializeObject(new TestDictionaryMultipleParameters(null, null)),
- "Constructor for 'Newtonsoft.Json.Tests.Serialization.JsonSerializerCollectionsTests+TestDictionaryMultipleParameters' must have no parameters or a single parameter that implements 'System.Collections.Generic.IEnumerable`1[System.Collections.Generic.KeyValuePair`2[System.String,System.Int32]]'.");
- }
-
- public class TestDictionaryBadIEnumerableParameter : Dictionary<string, int>
- {
- [JsonConstructor]
- public TestDictionaryBadIEnumerableParameter(Dictionary<string, string> l)
- {
- }
- }
-
- [Test]
- public void DictionaryJsonConstructorBadIEnumerableParameter()
- {
- ExceptionAssert.Throws<JsonException>(
- () => JsonConvert.SerializeObject(new TestDictionaryBadIEnumerableParameter(null)),
- "Constructor for 'Newtonsoft.Json.Tests.Serialization.JsonSerializerCollectionsTests+TestDictionaryBadIEnumerableParameter' must have no parameters or a single parameter that implements 'System.Collections.Generic.IEnumerable`1[System.Collections.Generic.KeyValuePair`2[System.String,System.Int32]]'.");
- }
-
- #if !(DNXCORE50 || PORTABLE)
- public class TestDictionaryNonGeneric : Hashtable
- {
- [JsonConstructor]
- public TestDictionaryNonGeneric(IDictionary d)
- : base(d)
- {
- }
- }
-
- [Test]
- public void DictionaryJsonConstructorNonGeneric()
- {
- string json = @"{'zero':0,'one':1,'two':2}";
- TestDictionaryNonGeneric d = JsonConvert.DeserializeObject<TestDictionaryNonGeneric>(json);
-
- Assert.AreEqual(3, d.Count);
- Assert.AreEqual(0, d["zero"]);
- Assert.AreEqual(1, d["one"]);
- Assert.AreEqual(2, d["two"]);
- }
- #endif
-
- #if !(DNXCORE50)
- public class NameValueCollectionTestClass
- {
- public NameValueCollection Collection { get; set; }
- }
-
- [Test]
- public void DeserializeNameValueCollection()
- {
- ExceptionAssert.Throws<JsonSerializationException>(
- () => JsonConvert.DeserializeObject<NameValueCollectionTestClass>("{Collection:[]}"),
- "Cannot create and populate list type System.Collections.Specialized.NameValueCollection. Path 'Collection', line 1, position 13.");
- }
- #endif
-
- #if !(NET35 || NET20 || PORTABLE || PORTABLE40)
- public class SomeObject
- {
- public string Text1 { get; set; }
- }
-
- public class CustomConcurrentDictionary : ConcurrentDictionary<string, List<SomeObject>>
- {
- [OnDeserialized]
- internal void OnDeserializedMethod(StreamingContext context)
- {
- ((IDictionary)this).Add("key2", new List<SomeObject>
- {
- new SomeObject
- {
- Text1 = "value2"
- }
- });
- }
- }
-
- [Test]
- public void SerializeCustomConcurrentDictionary()
- {
- IDictionary d = new CustomConcurrentDictionary();
- d.Add("key", new List<SomeObject>
- {
- new SomeObject
- {
- Text1 = "value1"
- }
- });
-
- string json = JsonConvert.SerializeObject(d, Formatting.Indented);
-
- Assert.AreEqual(@"{
- ""key"": [
- {
- ""Text1"": ""value1""
- }
- ]
- }", json);
-
- CustomConcurrentDictionary d2 = JsonConvert.DeserializeObject<CustomConcurrentDictionary>(json);
-
- Assert.AreEqual(2, d2.Count);
- Assert.AreEqual("value1", d2["key"][0].Text1);
- Assert.AreEqual("value2", d2["key2"][0].Text1);
- }
- #endif
-
- [Test]
- public void NonZeroBasedArray()
- {
- var onebasedArray = Array.CreateInstance(typeof(string), new[] { 3 }, new[] { 2 });
-
- for (var i = onebasedArray.GetLowerBound(0); i <= onebasedArray.GetUpperBound(0); i++)
- {
- onebasedArray.SetValue(i.ToString(CultureInfo.InvariantCulture), new[] { i, });
- }
-
- string output = JsonConvert.SerializeObject(onebasedArray, Formatting.Indented);
-
- StringAssert.AreEqual(@"[
- ""2"",
- ""3"",
- ""4""
- ]", output);
- }
-
- [Test]
- public void NonZeroBasedMultiArray()
- {
- // lets create a two dimensional array, each rank is 1-based of with a capacity of 4.
- var onebasedArray = Array.CreateInstance(typeof(string), new[] { 3, 3 }, new[] { 1, 2 });
-
- // Iterate of the array elements and assign a random double
- for (var i = onebasedArray.GetLowerBound(0); i <= onebasedArray.GetUpperBound(0); i++)
- {
- for (var j = onebasedArray.GetLowerBound(1); j <= onebasedArray.GetUpperBound(1); j++)
- {
- onebasedArray.SetValue(i + "_" + j, new[] { i, j });
- }
- }
-
- // Now lets try and serialize the Array
- string output = JsonConvert.SerializeObject(onebasedArray, Formatting.Indented);
-
- StringAssert.AreEqual(@"[
- [
- ""1_2"",
- ""1_3"",
- ""1_4""
- ],
- [
- ""2_2"",
- ""2_3"",
- ""2_4""
- ],
- [
- ""3_2"",
- ""3_3"",
- ""3_4""
- ]
- ]", output);
- }
-
- [Test]
- public void MultiDObjectArray()
- {
- object[,] myOtherArray =
- {
- { new KeyValuePair<string, double>("my value", 0.8), "foobar" },
- { true, 0.4d },
- { 0.05f, 6 }
- };
-
- string myOtherArrayAsString = JsonConvert.SerializeObject(myOtherArray, Formatting.Indented);
-
- StringAssert.AreEqual(@"[
- [
- {
- ""Key"": ""my value"",
- ""Value"": 0.8
- },
- ""foobar""
- ],
- [
- true,
- 0.4
- ],
- [
- 0.05,
- 6
- ]
- ]", myOtherArrayAsString);
-
- JObject o = JObject.Parse(@"{
- ""Key"": ""my value"",
- ""Value"": 0.8
- }");
-
- object[,] myOtherResult = JsonConvert.DeserializeObject<object[,]>(myOtherArrayAsString);
- Assert.IsTrue(JToken.DeepEquals(o, (JToken)myOtherResult[0, 0]));
- Assert.AreEqual("foobar", myOtherResult[0, 1]);
-
- Assert.AreEqual(true, myOtherResult[1, 0]);
- Assert.AreEqual(0.4, myOtherResult[1, 1]);
-
- Assert.AreEqual(0.05, myOtherResult[2, 0]);
- Assert.AreEqual(6L, myOtherResult[2, 1]);
- }
-
- public class EnumerableClass<T> : IEnumerable<T>
- {
- private readonly IList<T> _values;
-
- public EnumerableClass(IEnumerable<T> values)
- {
- _values = new List<T>(values);
- }
-
- public IEnumerator<T> GetEnumerator()
- {
- return _values.GetEnumerator();
- }
-
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
- }
-
- [Test]
- public void DeserializeIEnumerableFromConstructor()
- {
- string json = @"[
- 1,
- 2,
- null
- ]";
-
- var result = JsonConvert.DeserializeObject<EnumerableClass<int?>>(json);
-
- Assert.AreEqual(3, result.Count());
- Assert.AreEqual(1, result.ElementAt(0));
- Assert.AreEqual(2, result.ElementAt(1));
- Assert.AreEqual(null, result.ElementAt(2));
- }
-
- public class EnumerableClassFailure<T> : IEnumerable<T>
- {
- private readonly IList<T> _values;
-
- public EnumerableClassFailure()
- {
- _values = new List<T>();
- }
-
- public IEnumerator<T> GetEnumerator()
- {
- return _values.GetEnumerator();
- }
-
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
- }
-
- [Test]
- public void DeserializeIEnumerableFromConstructor_Failure()
- {
- string json = @"[
- ""One"",
- ""II"",
- ""3""
- ]";
-
- ExceptionAssert.Throws<JsonSerializationException>(() => JsonConvert.DeserializeObject<EnumerableClassFailure<string>>(json), "Cannot create and populate list type Newtonsoft.Json.Tests.Serialization.JsonSerializerCollectionsTests+EnumerableClassFailure`1[System.String]. Path '', line 1, position 1.");
- }
-
- public class PrivateDefaultCtorList<T> : List<T>
- {
- private PrivateDefaultCtorList()
- {
- }
- }
-
- [Test]
- public void DeserializePrivateListCtor()
- {
- ExceptionAssert.Throws<JsonSerializationException>(() => JsonConvert.DeserializeObject<PrivateDefaultCtorList<int>>("[1,2]"), "Unable to find a constructor to use for type Newtonsoft.Json.Tests.Serialization.JsonSerializerCollectionsTests+PrivateDefaultCtorList`1[System.Int32]. Path '', line 1, position 1.");
-
- var list = JsonConvert.DeserializeObject<PrivateDefaultCtorList<int>>("[1,2]", new JsonSerializerSettings
- {
- ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor
- });
-
- Assert.AreEqual(2, list.Count);
- }
-
- public class PrivateDefaultCtorWithIEnumerableCtorList<T> : List<T>
- {
- private PrivateDefaultCtorWithIEnumerableCtorList()
- {
- }
-
- public PrivateDefaultCtorWithIEnumerableCtorList(IEnumerable<T> values)
- : base(values)
- {
- Add(default(T));
- }
- }
-
- [Test]
- public void DeserializePrivateListConstructor()
- {
- var list = JsonConvert.DeserializeObject<PrivateDefaultCtorWithIEnumerableCtorList<int>>("[1,2]");
-
- Assert.AreEqual(3, list.Count);
- Assert.AreEqual(1, list[0]);
- Assert.AreEqual(2, list[1]);
- Assert.AreEqual(0, list[2]);
- }
-
- [Test]
- public void DeserializeNonIsoDateDictionaryKey()
- {
- Dictionary<DateTime, string> d = JsonConvert.DeserializeObject<Dictionary<DateTime, string>>(@"{""04/28/2013 00:00:00"":""test""}");
-
- Assert.AreEqual(1, d.Count);
-
- DateTime key = DateTime.Parse("04/28/2013 00:00:00", CultureInfo.InvariantCulture);
- Assert.AreEqual("test", d[key]);
- }
-
- [Test]
- public void DeserializeNonGenericList()
- {
- IList l = JsonConvert.DeserializeObject<IList>("['string!']");
-
- Assert.AreEqual(typeof(List<object>), l.GetType());
- Assert.AreEqual(1, l.Count);
- Assert.AreEqual("string!", l[0]);
- }
-
- #if !(NET40 || NET35 || NET20 || PORTABLE40)
- [Test]
- public void DeserializeReadOnlyListInterface()
- {
- IReadOnlyList<int> list = JsonConvert.DeserializeObject<IReadOnlyList<int>>("[1,2,3]");
-
- Assert.AreEqual(3, list.Count);
- Assert.AreEqual(1, list[0]);
- Assert.AreEqual(2, list[1]);
- Assert.AreEqual(3, list[2]);
- }
-
- [Test]
- public void DeserializeReadOnlyCollectionInterface()
- {
- IReadOnlyCollection<int> list = JsonConvert.DeserializeObject<IReadOnlyCollection<int>>("[1,2,3]");
-
- Assert.AreEqual(3, list.Count);
-
- Assert.AreEqual(1, list.ElementAt(0));
- Assert.AreEqual(2, list.ElementAt(1));
- Assert.AreEqual(3, list.ElementAt(2));
- }
-
- [Test]
- public void DeserializeReadOnlyCollection()
- {
- ReadOnlyCollection<int> list = JsonConvert.DeserializeObject<ReadOnlyCollection<int>>("[1,2,3]");
-
- Assert.AreEqual(3, list.Count);
-
- Assert.AreEqual(1, list[0]);
- Assert.AreEqual(2, list[1]);
- Assert.AreEqual(3, list[2]);
- }
-
- [Test]
- public void DeserializeReadOnlyDictionaryInterface()
- {
- IReadOnlyDictionary<string, int> dic = JsonConvert.DeserializeObject<IReadOnlyDictionary<string, int>>("{'one':1,'two':2}");
-
- Assert.AreEqual(2, dic.Count);
-
- Assert.AreEqual(1, dic["one"]);
- Assert.AreEqual(2, dic["two"]);
-
- CustomAssert.IsInstanceOfType(typeof(ReadOnlyDictionary<string, int>), dic);
- }
-
- [Test]
- public void DeserializeReadOnlyDictionary()
- {
- ReadOnlyDictionary<string, int> dic = JsonConvert.DeserializeObject<ReadOnlyDictionary<string, int>>("{'one':1,'two':2}");
-
- Assert.AreEqual(2, dic.Count);
-
- Assert.AreEqual(1, dic["one"]);
- Assert.AreEqual(2, dic["two"]);
- }
-
- public class CustomReadOnlyDictionary<TKey, TValue> : IReadOnlyDictionary<TKey, TValue>
- {
- private readonly IDictionary<TKey, TValue> _dictionary;
-
- public CustomReadOnlyDictionary(IDictionary<TKey, TValue> dictionary)
- {
- _dictionary = dictionary;
- }
-
- public bool ContainsKey(TKey key)
- {
- return _dictionary.ContainsKey(key);
- }
-
- public IEnumerable<TKey> Keys
- {
- get { return _dictionary.Keys; }
- }
-
- public bool TryGetValue(TKey key, out TValue value)
- {
- return _dictionary.TryGetValue(key, out value);
- }
-
- public IEnumerable<TValue> Values
- {
- get { return _dictionary.Values; }
- }
-
- public TValue this[TKey key]
- {
- get { return _dictionary[key]; }
- }
-
- public int Count
- {
- get { return _dictionary.Count; }
- }
-
- public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
- {
- return _dictionary.GetEnumerator();
- }
-
- IEnumerator IEnumerable.GetEnumerator()
- {
- return _dictionary.GetEnumerator();
- }
- }
-
- [Test]
- public void SerializeCustomReadOnlyDictionary()
- {
- IDictionary<string, int> d = new Dictionary<string, int>
- {
- { "one", 1 },
- { "two", 2 }
- };
-
- CustomReadOnlyDictionary<string, int> dic = new CustomReadOnlyDictionary<string, int>(d);
-
- string json = JsonConvert.SerializeObject(dic, Formatting.Indented);
- StringAssert.AreEqual(@"{
- ""one"": 1,
- ""two"": 2
- }", json);
- }
-
- public class CustomReadOnlyCollection<T> : IReadOnlyCollection<T>
- {
- private readonly IList<T> _values;
-
- public CustomReadOnlyCollection(IList<T> values)
- {
- _values = values;
- }
-
- public int Count
- {
- get { return _values.Count; }
- }
-
- public IEnumerator<T> GetEnumerator()
- {
- return _values.GetEnumerator();
- }
-
- IEnumerator IEnumerable.GetEnumerator()
- {
- return _values.GetEnumerator();
- }
- }
-
- [Test]
- public void SerializeCustomReadOnlyCollection()
- {
- IList<int> l = new List<int>
- {
- 1,
- 2,
- 3
- };
-
- CustomReadOnlyCollection<int> list = new CustomReadOnlyCollection<int>(l);
-
- string json = JsonConvert.SerializeObject(list, Formatting.Indented);
- StringAssert.AreEqual(@"[
- 1,
- 2,
- 3
- ]", json);
- }
- #endif
-
- [Test]
- public void TestEscapeDictionaryStrings()
- {
- const string s = @"host\user";
- string serialized = JsonConvert.SerializeObject(s);
- Assert.AreEqual(@"""host\\user""", serialized);
-
- Dictionary<int, object> d1 = new Dictionary<int, object>();
- d1.Add(5, s);
- Assert.AreEqual(@"{""5"":""host\\user""}", JsonConvert.SerializeObject(d1));
-
- Dictionary<string, object> d2 = new Dictionary<string, object>();
- d2.Add(s, 5);
- Assert.AreEqual(@"{""host\\user"":5}", JsonConvert.SerializeObject(d2));
- }
-
- public class GenericListTestClass
- {
- public List<string> GenericList { get; set; }
-
- public GenericListTestClass()
- {
- GenericList = new List<string>();
- }
- }
-
- [Test]
- public void DeserializeExistingGenericList()
- {
- GenericListTestClass c = new GenericListTestClass();
- c.GenericList.Add("1");
- c.GenericList.Add("2");
-
- string json = JsonConvert.SerializeObject(c, Formatting.Indented);
-
- GenericListTestClass newValue = JsonConvert.DeserializeObject<GenericListTestClass>(json);
- Assert.AreEqual(2, newValue.GenericList.Count);
- Assert.AreEqual(typeof(List<string>), newValue.GenericList.GetType());
- }
-
- [Test]
- public void DeserializeSimpleKeyValuePair()
- {
- List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();
- list.Add(new KeyValuePair<string, string>("key1", "value1"));
- list.Add(new KeyValuePair<string, string>("key2", "value2"));
-
- string json = JsonConvert.SerializeObject(list);
-
- Assert.AreEqual(@"[{""Key"":""key1"",""Value"":""value1""},{""Key"":""key2"",""Value"":""value2""}]", json);
-
- List<KeyValuePair<string, string>> result = JsonConvert.DeserializeObject<List<KeyValuePair<string, string>>>(json);
- Assert.AreEqual(2, result.Count);
- Assert.AreEqual("key1", result[0].Key);
- Assert.AreEqual("value1", result[0].Value);
- Assert.AreEqual("key2", result[1].Key);
- Assert.AreEqual("value2", result[1].Value);
- }
-
- [Test]
- public void DeserializeComplexKeyValuePair()
- {
- DateTime dateTime = new DateTime(2000, 12, 1, 23, 1, 1, DateTimeKind.Utc);
-
- List<KeyValuePair<string, WagePerson>> list = new List<KeyValuePair<string, WagePerson>>();
- list.Add(new KeyValuePair<string, WagePerson>("key1", new WagePerson
- {
- BirthDate = dateTime,
- Department = "Department1",
- LastModified = dateTime,
- HourlyWage = 1
- }));
- list.Add(new KeyValuePair<string, WagePerson>("key2", new WagePerson
- {
- BirthDate = dateTime,
- Department = "Department2",
- LastModified = dateTime,
- HourlyWage = 2
- }));
-
- string json = JsonConvert.SerializeObject(list, Formatting.Indented);
-
- StringAssert.AreEqual(@"[
- {
- ""Key"": ""key1"",
- ""Value"": {
- ""HourlyWage"": 1.0,
- ""Name"": null,
- ""BirthDate"": ""2000-12-01T23:01:01Z"",
- ""LastModified"": ""2000-12-01T23:01:01Z""
- }
- },
- {
- ""Key"": ""key2"",
- ""Value"": {
- ""HourlyWage"": 2.0,
- ""Name"": null,
- ""BirthDate"": ""2000-12-01T23:01:01Z"",
- ""LastModified"": ""2000-12-01T23:01:01Z""
- }
- }
- ]", json);
-
- List<KeyValuePair<string, WagePerson>> result = JsonConvert.DeserializeObject<List<KeyValuePair<string, WagePerson>>>(json);
- Assert.AreEqual(2, result.Count);
- Assert.AreEqual("key1", result[0].Key);
- Assert.AreEqual(1, result[0].Value.HourlyWage);
- Assert.AreEqual("key2", result[1].Key);
- Assert.AreEqual(2, result[1].Value.HourlyWage);
- }
-
- [Test]
- public void StringListAppenderConverterTest()
- {
- Movie p = new Movie();
- p.ReleaseCountries = new List<string> { "Existing" };
-
- JsonConvert.PopulateObject("{'ReleaseCountries':['Appended']}", p, new JsonSerializerSettings
- {
- Converters = new List<JsonConverter> { new StringListAppenderConverter() }
- });
-
- Assert.AreEqual(2, p.ReleaseCountries.Count);
- Assert.AreEqual("Existing", p.ReleaseCountries[0]);
- Assert.AreEqual("Appended", p.ReleaseCountries[1]);
- }
-
- [Test]
- public void StringAppenderConverterTest()
- {
- Movie p = new Movie();
- p.Name = "Existing,";
-
- JsonConvert.PopulateObject("{'Name':'Appended'}", p, new JsonSerializerSettings
- {
- Converters = new List<JsonConverter> { new StringAppenderConverter() }
- });
-
- Assert.AreEqual("Existing,Appended", p.Name);
- }
-
- [Test]
- public void DeserializeIDictionary()
- {
- IDictionary dictionary = JsonConvert.DeserializeObject<IDictionary>("{'name':'value!'}");
- Assert.AreEqual(1, dictionary.Count);
- Assert.AreEqual("value!", dictionary["name"]);
- }
-
- [Test]
- public void DeserializeIList()
- {
- IList list = JsonConvert.DeserializeObject<IList>("['1', 'two', 'III']");
- Assert.AreEqual(3, list.Count);
- }
-
- [Test]
- public void NullableValueGenericDictionary()
- {
- IDictionary<string, int?> v1 = new Dictionary<string, int?>
- {
- { "First", 1 },
- { "Second", null },
- { "Third", 3 }
- };
-
- string json = JsonConvert.SerializeObject(v1, Formatting.Indented);
-
- StringAssert.AreEqual(@"{
- ""First"": 1,
- ""Second"": null,
- ""Third"": 3
- }", json);
-
- IDictionary<string, int?> v2 = JsonConvert.DeserializeObject<IDictionary<string, int?>>(json);
- Assert.AreEqual(3, v2.Count);
- Assert.AreEqual(1, v2["First"]);
- Assert.AreEqual(null, v2["Second"]);
- Assert.AreEqual(3, v2["Third"]);
- }
-
- #if !(NET35 || NET20 || PORTABLE || PORTABLE40)
- [Test]
- public void DeserializeConcurrentDictionary()
- {
- IDictionary<string, TestObjects.Component> components = new Dictionary<string, TestObjects.Component>
- {
- { "Key!", new TestObjects.Component() }
- };
- GameObject go = new GameObject
- {
- Components = new ConcurrentDictionary<string, TestObjects.Component>(components),
- Id = "Id!",
- Name = "Name!"
- };
-
- string originalJson = JsonConvert.SerializeObject(go, Formatting.Indented);
-
- StringAssert.AreEqual(@"{
- ""Components"": {
- ""Key!"": {}
- },
- ""Id"": ""Id!"",
- ""Name"": ""Name!""
- }", originalJson);
-
- GameObject newObject = JsonConvert.DeserializeObject<GameObject>(originalJson);
-
- Assert.AreEqual(1, newObject.Components.Count);
- Assert.AreEqual("Id!", newObject.Id);
- Assert.AreEqual("Name!", newObject.Name);
- }
- #endif
-
- [Test]
- public void DeserializeKeyValuePairArray()
- {
- string json = @"[ { ""Value"": [ ""1"", ""2"" ], ""Key"": ""aaa"", ""BadContent"": [ 0 ] }, { ""Value"": [ ""3"", ""4"" ], ""Key"": ""bbb"" } ]";
-
- IList<KeyValuePair<string, IList<string>>> values = JsonConvert.DeserializeObject<IList<KeyValuePair<string, IList<string>>>>(json);
-
- Assert.AreEqual(2, values.Count);
- Assert.AreEqual("aaa", values[0].Key);
- Assert.AreEqual(2, values[0].Value.Count);
- Assert.AreEqual("1", values[0].Value[0]);
- Assert.AreEqual("2", values[0].Value[1]);
- Assert.AreEqual("bbb", values[1].Key);
- Assert.AreEqual(2, values[1].Value.Count);
- Assert.AreEqual("3", values[1].Value[0]);
- Assert.AreEqual("4", values[1].Value[1]);
- }
-
- [Test]
- public void DeserializeNullableKeyValuePairArray()
- {
- string json = @"[ { ""Value"": [ ""1"", ""2"" ], ""Key"": ""aaa"", ""BadContent"": [ 0 ] }, null, { ""Value"": [ ""3"", ""4"" ], ""Key"": ""bbb"" } ]";
-
- IList<KeyValuePair<string, IList<string>>?> values = JsonConvert.DeserializeObject<IList<KeyValuePair<string, IList<string>>?>>(json);
-
- Assert.AreEqual(3, values.Count);
- Assert.AreEqual("aaa", values[0].Value.Key);
- Assert.AreEqual(2, values[0].Value.Value.Count);
- Assert.AreEqual("1", values[0].Value.Value[0]);
- Assert.AreEqual("2", values[0].Value.Value[1]);
- Assert.AreEqual(null, values[1]);
- Assert.AreEqual("bbb", values[2].Value.Key);
- Assert.AreEqual(2, values[2].Value.Value.Count);
- Assert.AreEqual("3", values[2].Value.Value[0]);
- Assert.AreEqual("4", values[2].Value.Value[1]);
- }
-
- [Test]
- public void DeserializeNullToNonNullableKeyValuePairArray()
- {
- string json = @"[ null ]";
-
- ExceptionAssert.Throws<JsonSerializationException>(() => { JsonConvert.DeserializeObject<IList<KeyValuePair<string, IList<string>>>>(json); }, "Cannot convert null value to KeyValuePair. Path '[0]', line 1, position 6.");
- }
-
- #if !(NET40 || NET35 || NET20 || PORTABLE40)
- public class PopulateReadOnlyTestClass
- {
- public IList<int> NonReadOnlyList { get; set; }
- public IDictionary<string, int> NonReadOnlyDictionary { get; set; }
-
- public IList<int> Array { get; set; }
-
- public IList<int> List { get; set; }
- public IDictionary<string, int> Dictionary { get; set; }
-
- public IReadOnlyCollection<int> IReadOnlyCollection { get; set; }
- public ReadOnlyCollection<int> ReadOnlyCollection { get; set; }
- public IReadOnlyList<int> IReadOnlyList { get; set; }
-
- public IReadOnlyDictionary<string, int> IReadOnlyDictionary { get; set; }
- public ReadOnlyDictionary<string, int> ReadOnlyDictionary { get; set; }
-
- public PopulateReadOnlyTestClass()
- {
- NonReadOnlyList = new List<int> { 1 };
- NonReadOnlyDictionary = new Dictionary<string, int> { { "first", 2 } };
-
- Array = new[] { 3 };
-
- List = new ReadOnlyCollection<int>(new[] { 4 });
- Dictionary = new ReadOnlyDictionary<string, int>(new Dictionary<string, int> { { "first", 5 } });
-
- IReadOnlyCollection = new ReadOnlyCollection<int>(new[] { 6 });
- ReadOnlyCollection = new ReadOnlyCollection<int>(new[] { 7 });
- IReadOnlyList = new ReadOnlyCollection<int>(new[] { 8 });
-
- IReadOnlyDictionary = new ReadOnlyDictionary<string, int>(new Dictionary<string, int> { { "first", 9 } });
- ReadOnlyDictionary = new ReadOnlyDictionary<string, int>(new Dictionary<string, int> { { "first", 10 } });
- }
- }
-
- [Test]
- public void SerializeReadOnlyCollections()
- {
- PopulateReadOnlyTestClass c1 = new PopulateReadOnlyTestClass();
-
- string json = JsonConvert.SerializeObject(c1, Formatting.Indented);
-
- StringAssert.AreEqual(@"{
- ""NonReadOnlyList"": [
- 1
- ],
- ""NonReadOnlyDictionary"": {
- ""first"": 2
- },
- ""Array"": [
- 3
- ],
- ""List"": [
- 4
- ],
- ""Dictionary"": {
- ""first"": 5
- },
- ""IReadOnlyCollection"": [
- 6
- ],
- ""ReadOnlyCollection"": [
- 7
- ],
- ""IReadOnlyList"": [
- 8
- ],
- ""IReadOnlyDictionary"": {
- ""first"": 9
- },
- ""ReadOnlyDictionary"": {
- ""first"": 10
- }
- }", json);
- }
-
- [Test]
- public void PopulateReadOnlyCollections()
- {
- string json = @"{
- ""NonReadOnlyList"": [
- 11
- ],
- ""NonReadOnlyDictionary"": {
- ""first"": 12
- },
- ""Array"": [
- 13
- ],
- ""List"": [
- 14
- ],
- ""Dictionary"": {
- ""first"": 15
- },
- ""IReadOnlyCollection"": [
- 16
- ],
- ""ReadOnlyCollection"": [
- 17
- ],
- ""IReadOnlyList"": [
- 18
- ],
- ""IReadOnlyDictionary"": {
- ""first"": 19
- },
- ""ReadOnlyDictionary"": {
- ""first"": 20
- }
- }";
-
- var c2 = JsonConvert.DeserializeObject<PopulateReadOnlyTestClass>(json);
-
- Assert.AreEqual(1, c2.NonReadOnlyDictionary.Count);
- Assert.AreEqual(12, c2.NonReadOnlyDictionary["first"]);
-
- Assert.AreEqual(2, c2.NonReadOnlyList.Count);
- Assert.AreEqual(1, c2.NonReadOnlyList[0]);
- Assert.AreEqual(11, c2.NonReadOnlyList[1]);
-
- Assert.AreEqual(1, c2.Array.Count);
- Assert.AreEqual(13, c2.Array[0]);
- }
- #endif
-
- [Test]
- public void SerializeArray2D()
- {
- Array2D aa = new Array2D();
- aa.Before = "Before!";
- aa.After = "After!";
- aa.Coordinates = new[,] { { 1, 1 }, { 1, 2 }, { 2, 1 }, { 2, 2 } };
-
- string json = JsonConvert.SerializeObject(aa);
-
- Assert.AreEqual(@"{""Before"":""Before!"",""Coordinates"":[[1,1],[1,2],[2,1],[2,2]],""After"":""After!""}", json);
- }
-
- [Test]
- public void SerializeArray3D()
- {
- Array3D aa = new Array3D();
- aa.Before = "Before!";
- aa.After = "After!";
- aa.Coordinates = new[,,] { { { 1, 1, 1 }, { 1, 1, 2 } }, { { 1, 2, 1 }, { 1, 2, 2 } }, { { 2, 1, 1 }, { 2, 1, 2 } }, { { 2, 2, 1 }, { 2, 2, 2 } } };
-
- string json = JsonConvert.SerializeObject(aa);
-
- Assert.AreEqual(@"{""Before"":""Before!"",""Coordinates"":[[[1,1,1],[1,1,2]],[[1,2,1],[1,2,2]],[[2,1,1],[2,1,2]],[[2,2,1],[2,2,2]]],""After"":""After!""}", json);
- }
-
- [Test]
- public void SerializeArray3DWithConverter()
- {
- Array3DWithConverter aa = new Array3DWithConverter();
- aa.Before = "Before!";
- aa.After = "After!";
- aa.Coordinates = new[,,] { { { 1, 1, 1 }, { 1, 1, 2 } }, { { 1, 2, 1 }, { 1, 2, 2 } }, { { 2, 1, 1 }, { 2, 1, 2 } }, { { 2, 2, 1 }, { 2, 2, 2 } } };
-
- string json = JsonConvert.SerializeObject(aa, Formatting.Indented);
-
- StringAssert.AreEqual(@"{
- ""Before"": ""Before!"",
- ""Coordinates"": [
- [
- [
- 1.0,
- 1.0,
- 1.0
- ],
- [
- 1.0,
- 1.0,
- 2.0
- ]
- ],
- [
- [
- 1.0,
- 2.0,
- 1.0
- ],
- [
- 1.0,
- 2.0,
- 2.0
- ]
- ],
- [
- [
- 2.0,
- 1.0,
- 1.0
- ],
- [
- 2.0,
- 1.0,
- 2.0
- ]
- ],
- [
- [
- 2.0,
- 2.0,
- 1.0
- ],
- [
- 2.0,
- 2.0,
- 2.0
- ]
- ]
- ],
- ""After"": ""After!""
- }", json);
- }
-
- [Test]
- public void DeserializeArray3DWithConverter()
- {
- string json = @"{
- ""Before"": ""Before!"",
- ""Coordinates"": [
- [
- [
- 1.0,
- 1.0,
- 1.0
- ],
- [
- 1.0,
- 1.0,
- 2.0
- ]
- ],
- [
- [
- 1.0,
- 2.0,
- 1.0
- ],
- [
- 1.0,
- 2.0,
- 2.0
- ]
- ],
- [
- [
- 2.0,
- 1.0,
- 1.0
- ],
- [
- 2.0,
- 1.0,
- 2.0
- ]
- ],
- [
- [
- 2.0,
- 2.0,
- 1.0
- ],
- [
- 2.0,
- 2.0,
- 2.0
- ]
- ]
- ],
- ""After"": ""After!""
- }";
-
- Array3DWithConverter aa = JsonConvert.DeserializeObject<Array3DWithConverter>(json);
-
- Assert.AreEqual("Before!", aa.Before);
- Assert.AreEqual("After!", aa.After);
- Assert.AreEqual(4, aa.Coordinates.GetLength(0));
- Assert.AreEqual(2, aa.Coordinates.GetLength(1));
- Assert.AreEqual(3, aa.Coordinates.GetLength(2));
- Assert.AreEqual(1, aa.Coordinates[0, 0, 0]);
- Assert.AreEqual(2, aa.Coordinates[1, 1, 1]);
- }
-
- [Test]
- public void DeserializeArray2D()
- {
- string json = @"{""Before"":""Before!"",""Coordinates"":[[1,1],[1,2],[2,1],[2,2]],""After"":""After!""}";
-
- Array2D aa = JsonConvert.DeserializeObject<Array2D>(json);
-
- Assert.AreEqual("Before!", aa.Before);
- Assert.AreEqual("After!", aa.After);
- Assert.AreEqual(4, aa.Coordinates.GetLength(0));
- Assert.AreEqual(2, aa.Coordinates.GetLength(1));
- Assert.AreEqual(1, aa.Coordinates[0, 0]);
- Assert.AreEqual(2, aa.Coordinates[1, 1]);
-
- string after = JsonConvert.SerializeObject(aa);
-
- Assert.AreEqual(json, after);
- }
-
- [Test]
- public void DeserializeArray2D_WithTooManyItems()
- {
- string json = @"{""Before"":""Before!"",""Coordinates"":[[1,1],[1,2,3],[2,1],[2,2]],""After"":""After!""}";
-
- ExceptionAssert.Throws<Exception>(() => JsonConvert.DeserializeObject<Array2D>(json), "Cannot deserialize non-cubical array as multidimensional array.");
- }
-
- [Test]
- public void DeserializeArray2D_WithTooFewItems()
- {
- string json = @"{""Before"":""Before!"",""Coordinates"":[[1,1],[1],[2,1],[2,2]],""After"":""After!""}";
-
- ExceptionAssert.Throws<Exception>(() => JsonConvert.DeserializeObject<Array2D>(json), "Cannot deserialize non-cubical array as multidimensional array.");
- }
-
- [Test]
- public void DeserializeArray3D()
- {
- string json = @"{""Before"":""Before!"",""Coordinates"":[[[1,1,1],[1,1,2]],[[1,2,1],[1,2,2]],[[2,1,1],[2,1,2]],[[2,2,1],[2,2,2]]],""After"":""After!""}";
-
- Array3D aa = JsonConvert.DeserializeObject<Array3D>(json);
-
- Assert.AreEqual("Before!", aa.Before);
- Assert.AreEqual("After!", aa.After);
- Assert.AreEqual(4, aa.Coordinates.GetLength(0));
- Assert.AreEqual(2, aa.Coordinates.GetLength(1));
- Assert.AreEqual(3, aa.Coordinates.GetLength(2));
- Assert.AreEqual(1, aa.Coordinates[0, 0, 0]);
- Assert.AreEqual(2, aa.Coordinates[1, 1, 1]);
-
- string after = JsonConvert.SerializeObject(aa);
-
- Assert.AreEqual(json, after);
- }
-
- [Test]
- public void DeserializeArray3D_WithTooManyItems()
- {
- string json = @"{""Before"":""Before!"",""Coordinates"":[[[1,1,1],[1,1,2,1]],[[1,2,1],[1,2,2]],[[2,1,1],[2,1,2]],[[2,2,1],[2,2,2]]],""After"":""After!""}";
-
- ExceptionAssert.Throws<Exception>(() => JsonConvert.DeserializeObject<Array3D>(json), "Cannot deserialize non-cubical array as multidimensional array.");
- }
-
- [Test]
- public void DeserializeArray3D_WithBadItems()
- {
- string json = @"{""Before"":""Before!"",""Coordinates"":[[[1,1,1],[1,1,2]],[[1,2,1],[1,2,2]],[[2,1,1],[2,1,2]],[[2,2,1],{}]],""After"":""After!""}";
-
- ExceptionAssert.Throws<JsonSerializationException>(() => JsonConvert.DeserializeObject<Array3D>(json), "Unexpected token when deserializing multidimensional array: StartObject. Path 'Coordinates[3][1]', line 1, position 99.");
- }
-
- [Test]
- public void DeserializeArray3D_WithTooFewItems()
- {
- string json = @"{""Before"":""Before!"",""Coordinates"":[[[1,1,1],[1,1]],[[1,2,1],[1,2,2]],[[2,1,1],[2,1,2]],[[2,2,1],[2,2,2]]],""After"":""After!""}";
-
- ExceptionAssert.Throws<Exception>(() => JsonConvert.DeserializeObject<Array3D>(json), "Cannot deserialize non-cubical array as multidimensional array.");
- }
-
- [Test]
- public void SerializeEmpty3DArray()
- {
- Array3D aa = new Array3D();
- aa.Before = "Before!";
- aa.After = "After!";
- aa.Coordinates = new int[0, 0, 0];
-
- string json = JsonConvert.SerializeObject(aa);
-
- Assert.AreEqual(@"{""Before"":""Before!"",""Coordinates"":[],""After"":""After!""}", json);
- }
-
- [Test]
- public void DeserializeEmpty3DArray()
- {
- string json = @"{""Before"":""Before!"",""Coordinates"":[],""After"":""After!""}";
-
- Array3D aa = JsonConvert.DeserializeObject<Array…
Large files files are truncated, but you can click here to view the full file