From: Layomi Akinrinade Date: Mon, 13 May 2019 21:19:31 +0000 (-0700) Subject: Add support for more collections (dotnet/corefx#37308) X-Git-Tag: submit/tizen/20210909.063632~11031^2~1597 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=00ad94242f3523ec692d83ae0be2c7b00c189978;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Add support for more collections (dotnet/corefx#37308) * Add support for additional collections * Add more tests * Some changes * Address review feedback * Some nits * Remove immutable dependency * Use Invoke, not DyanmicInvoke * Address review comments * Utilize JsonPropertyInfoCommon generic TElement parameter Commit migrated from https://github.com/dotnet/corefx/commit/cc00646d20b70827aa52ecd67ae6748d03eb3233 --- diff --git a/src/libraries/System.Text.Json/src/Resources/Strings.resx b/src/libraries/System.Text.Json/src/Resources/Strings.resx index f2f72cb..dc3eb4a 100644 --- a/src/libraries/System.Text.Json/src/Resources/Strings.resx +++ b/src/libraries/System.Text.Json/src/Resources/Strings.resx @@ -339,4 +339,7 @@ An item with the same property name '{0}' has already been added. + + Deserialization of type {0} is not supported. + \ No newline at end of file diff --git a/src/libraries/System.Text.Json/src/System.Text.Json.csproj b/src/libraries/System.Text.Json/src/System.Text.Json.csproj index 99d08f7..14f1ecb 100644 --- a/src/libraries/System.Text.Json/src/System.Text.Json.csproj +++ b/src/libraries/System.Text.Json/src/System.Text.Json.csproj @@ -49,6 +49,8 @@ + + diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ClassMaterializer.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ClassMaterializer.cs index 75d1f8c..5f78640 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ClassMaterializer.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ClassMaterializer.cs @@ -2,10 +2,37 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System.Collections; +using System.Collections.Generic; +using System.Diagnostics; +using System.Reflection; +using System.Text.Json.Serialization.Converters; + namespace System.Text.Json.Serialization { internal abstract class ClassMaterializer { public abstract JsonClassInfo.ConstructorDelegate CreateConstructor(Type classType); + + public abstract object ImmutableCreateRange(Type constructingType, Type elementType); + + protected MethodInfo ImmutableCreateRangeMethod(Type constructingType, Type elementType) + { + MethodInfo[] constructingTypeMethods = constructingType.GetMethods(); + MethodInfo createRange = null; + + foreach (MethodInfo method in constructingTypeMethods) + { + if (method.Name == "CreateRange" && method.GetParameters().Length == 1) + { + createRange = method; + break; + } + } + + Debug.Assert(createRange != null); + + return createRange.MakeGenericMethod(elementType); + } } } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/DefaultArrayConverter.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/DefaultArrayConverter.cs index f48d347..bc2ee86 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/DefaultArrayConverter.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/DefaultArrayConverter.cs @@ -9,8 +9,10 @@ namespace System.Text.Json.Serialization.Converters { internal sealed class DefaultArrayConverter : JsonEnumerableConverter { - public override IEnumerable CreateFromList(Type elementType, IList sourceList) + public override IEnumerable CreateFromList(ref ReadStack state, IList sourceList, JsonSerializerOptions options) { + Type elementType = state.Current.GetElementType(); + Array array; if (sourceList.Count > 0 && sourceList[0] is Array probe) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/DefaultEnumerableConverter.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/DefaultEnumerableConverter.cs index afddd65..3e8c81f 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/DefaultEnumerableConverter.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/DefaultEnumerableConverter.cs @@ -1,4 +1,8 @@ -using System.Collections; +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections; using System.Collections.Generic; using System.Text.Json.Serialization.Policies; @@ -15,10 +19,7 @@ namespace System.Text.Json.Serialization.Converters foreach (object item in sourceList) { - if (item is T itemT) - { - _list.Add(itemT); - } + _list.Add((T)item); } } @@ -81,8 +82,10 @@ namespace System.Text.Json.Serialization.Converters internal sealed class DefaultEnumerableConverter : JsonEnumerableConverter { - public override IEnumerable CreateFromList(Type elementType, IList sourceList) + public override IEnumerable CreateFromList(ref ReadStack state, IList sourceList, JsonSerializerOptions options) { + Type elementType = state.Current.GetElementType(); + Type t = typeof(JsonEnumerableT<>).MakeGenericType(elementType); return (IEnumerable)Activator.CreateInstance(t, sourceList); } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/DefaultIEnumerableConstructibleConverter.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/DefaultIEnumerableConstructibleConverter.cs new file mode 100644 index 0000000..8aa82ed --- /dev/null +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/DefaultIEnumerableConstructibleConverter.cs @@ -0,0 +1,43 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections; +using System.Collections.Concurrent; +using System.Text.Json.Serialization.Policies; + +namespace System.Text.Json.Serialization.Converters +{ + internal sealed class DefaultIEnumerableConstructibleConverter : JsonEnumerableConverter + { + public static ConcurrentDictionary s_objectJsonProperties = new ConcurrentDictionary(); + + public override IEnumerable CreateFromList(ref ReadStack state, IList sourceList, JsonSerializerOptions options) + { + Type enumerableType = state.Current.JsonPropertyInfo.RuntimePropertyType; + JsonClassInfo elementClassInfo = state.Current.JsonPropertyInfo.ElementClassInfo; + + JsonPropertyInfo propertyInfo; + if (elementClassInfo.ClassType == ClassType.Object) + { + Type objectType = elementClassInfo.Type; + + if (s_objectJsonProperties.ContainsKey(objectType)) + { + propertyInfo = s_objectJsonProperties[objectType]; + } + else + { + propertyInfo = JsonClassInfo.CreateProperty(objectType, objectType, null, typeof(object), options); + s_objectJsonProperties[objectType] = propertyInfo; + } + } + else + { + propertyInfo = elementClassInfo.GetPolicyProperty(); + } + + return propertyInfo.CreateIEnumerableConstructibleType(enumerableType, sourceList); + } + } +} diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/DefaultImmutableConverter.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/DefaultImmutableConverter.cs new file mode 100644 index 0000000..00b77ba7 --- /dev/null +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/DefaultImmutableConverter.cs @@ -0,0 +1,134 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections; +using System.Collections.Generic; +using System.Collections.Concurrent; +using System.Diagnostics; +using System.Text.Json.Serialization.Policies; + +namespace System.Text.Json.Serialization.Converters +{ + // This converter returns enumerables in the System.Collections.Immutable namespace. + internal sealed class DefaultImmutableConverter : JsonEnumerableConverter + { + public const string ImmutableNamespace = "System.Collections.Immutable"; + + private const string ImmutableListTypeName = "System.Collections.Immutable.ImmutableList"; + private const string ImmutableListGenericTypeName = "System.Collections.Immutable.ImmutableList`1"; + private const string ImmutableListGenericInterfaceTypeName = "System.Collections.Immutable.IImmutableList`1"; + + private const string ImmutableStackTypeName = "System.Collections.Immutable.ImmutableStack"; + private const string ImmutableStackGenericTypeName = "System.Collections.Immutable.ImmutableStack`1"; + private const string ImmutableStackGenericInterfaceTypeName = "System.Collections.Immutable.IImmutableStack`1"; + + private const string ImmutableQueueTypeName = "System.Collections.Immutable.ImmutableQueue"; + private const string ImmutableQueueGenericTypeName = "System.Collections.Immutable.ImmutableQueue`1"; + private const string ImmutableQueueGenericInterfaceTypeName = "System.Collections.Immutable.IImmutableQueue`1"; + + private const string ImmutableSortedSetTypeName = "System.Collections.Immutable.ImmutableSortedSet"; + private const string ImmutableSortedSetGenericTypeName = "System.Collections.Immutable.ImmutableSortedSet`1"; + + private const string ImmutableHashSetTypeName = "System.Collections.Immutable.ImmutableHashSet"; + private const string ImmutableHashSetGenericTypeName = "System.Collections.Immutable.ImmutableHashSet`1"; + private const string ImmutableSetGenericInterfaceTypeName = "System.Collections.Immutable.IImmutableSet`1"; + + internal delegate object ImmutableCreateRangeDelegate(IEnumerable items); + + public static ConcurrentDictionary CreateRangeDelegates = new ConcurrentDictionary(); + + private string GetConstructingTypeName(string immutableCollectionTypeName) + { + switch (immutableCollectionTypeName) + { + case ImmutableListGenericTypeName: + case ImmutableListGenericInterfaceTypeName: + return ImmutableListTypeName; + case ImmutableStackGenericTypeName: + case ImmutableStackGenericInterfaceTypeName: + return ImmutableStackTypeName; + case ImmutableQueueGenericTypeName: + case ImmutableQueueGenericInterfaceTypeName: + return ImmutableQueueTypeName; + case ImmutableSortedSetGenericTypeName: + return ImmutableSortedSetTypeName; + case ImmutableHashSetGenericTypeName: + case ImmutableSetGenericInterfaceTypeName: + return ImmutableHashSetTypeName; + default: + // TODO: Refactor exception throw following serialization exception changes. + throw new NotSupportedException(SR.Format(SR.DeserializeTypeNotSupported, immutableCollectionTypeName)); + } + } + + private string GetDelegateKey( + Type immutableCollectionType, + Type elementType, + out Type underlyingType, + out string constructingTypeName) + { + // Use the generic type definition of the immutable collection to determine an appropriate constructing type, + // i.e. a type that we can invoke the `CreateRange` method on, which returns an assignable immutable collection. + underlyingType = immutableCollectionType.GetGenericTypeDefinition(); + constructingTypeName = GetConstructingTypeName(underlyingType.FullName); + + return $"{constructingTypeName}:{elementType.FullName}"; + } + + public void RegisterImmutableCollectionType(Type immutableCollectionType, Type elementType, JsonSerializerOptions options) + { + // Get a unique identifier for a delegate which will point to the appropiate CreateRange method. + string delegateKey = GetDelegateKey(immutableCollectionType, elementType, out Type underlyingType, out string constructingTypeName); + + // Exit if we have registered this immutable collection type. + if (CreateRangeDelegates.ContainsKey(delegateKey)) + { + return; + } + + // Get the constructing type. + Type constructingType = underlyingType.Assembly.GetType(constructingTypeName); + + // Create a delegate which will point to the CreateRange method. + object createRangeDelegate = options.ClassMaterializerStrategy.ImmutableCreateRange(constructingType, elementType); + Debug.Assert(createRangeDelegate != null); + + // Cache the delegate + CreateRangeDelegates.TryAdd(delegateKey, createRangeDelegate); + } + + public override IEnumerable CreateFromList(ref ReadStack state, IList sourceList, JsonSerializerOptions options) + { + Type immutableCollectionType = state.Current.JsonPropertyInfo.RuntimePropertyType; + Type elementType = state.Current.GetElementType(); + + string delegateKey = GetDelegateKey(immutableCollectionType, elementType, out _, out _); + Debug.Assert(CreateRangeDelegates.ContainsKey(delegateKey)); + + JsonClassInfo elementClassInfo = state.Current.JsonPropertyInfo.ElementClassInfo; + + JsonPropertyInfo propertyInfo; + if (elementClassInfo.ClassType == ClassType.Object) + { + Type objectType = elementClassInfo.Type; + + if (DefaultIEnumerableConstructibleConverter.s_objectJsonProperties.ContainsKey(objectType)) + { + propertyInfo = DefaultIEnumerableConstructibleConverter.s_objectJsonProperties[objectType]; + } + else + { + propertyInfo = JsonClassInfo.CreateProperty(objectType, objectType, null, typeof(object), options); + DefaultIEnumerableConstructibleConverter.s_objectJsonProperties[objectType] = propertyInfo; + } + } + else + { + propertyInfo = elementClassInfo.GetPolicyProperty(); + } + + return propertyInfo.CreateImmutableCollectionFromList(delegateKey, sourceList); + } + } +} diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonClassInfo.AddProperty.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonClassInfo.AddProperty.cs index 8eaebc9..05b9ca1 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonClassInfo.AddProperty.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonClassInfo.AddProperty.cs @@ -52,7 +52,7 @@ namespace System.Text.Json.Serialization return jsonInfo; } - internal JsonPropertyInfo CreateProperty(Type declaredPropertyType, Type runtimePropertyType, PropertyInfo propertyInfo, Type parentClassType, JsonSerializerOptions options) + internal static JsonPropertyInfo CreateProperty(Type declaredPropertyType, Type runtimePropertyType, PropertyInfo propertyInfo, Type parentClassType, JsonSerializerOptions options) { Type collectionElementType = null; switch (GetClassType(runtimePropertyType)) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonEnumerableConverter.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonEnumerableConverter.cs index dd92cec..3d2d587 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonEnumerableConverter.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonEnumerableConverter.cs @@ -3,11 +3,12 @@ // See the LICENSE file in the project root for more information. using System.Collections; +using System.Collections.Generic; namespace System.Text.Json.Serialization.Policies { internal abstract class JsonEnumerableConverter { - public abstract IEnumerable CreateFromList(Type elementType, IList sourceList); + public abstract IEnumerable CreateFromList(ref ReadStack state, IList sourceList, JsonSerializerOptions options); } } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonPropertyInfo.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonPropertyInfo.cs index 3702b01..8c429bc 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonPropertyInfo.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonPropertyInfo.cs @@ -2,8 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System.Buffers; using System.Collections; +using System.Collections.Generic; using System.Diagnostics; using System.Reflection; using System.Text.Json.Serialization.Converters; @@ -17,6 +17,8 @@ namespace System.Text.Json.Serialization // Cache the converters so they don't get created for every enumerable property. private static readonly JsonEnumerableConverter s_jsonArrayConverter = new DefaultArrayConverter(); private static readonly JsonEnumerableConverter s_jsonEnumerableConverter = new DefaultEnumerableConverter(); + private static readonly JsonEnumerableConverter s_jsonIEnumerableConstuctibleConverter = new DefaultIEnumerableConstructibleConverter(); + private static readonly JsonEnumerableConverter s_jsonImmutableConverter = new DefaultImmutableConverter(); public ClassType ClassType; @@ -40,7 +42,6 @@ namespace System.Text.Json.Serialization public bool IgnoreNullValues { get; private set; } - // todo: to minimize hashtable lookups, cache JsonClassInfo: //public JsonClassInfo ClassInfo; @@ -201,10 +202,6 @@ namespace System.Text.Json.Serialization { ShouldDeserialize = true; } - //else - //{ - // // todo: future feature that allows non-List types (e.g. from System.Collections.Immutable) to have converters. - //} } //else if (HasSetter) //{ @@ -228,6 +225,22 @@ namespace System.Text.Json.Serialization { EnumerableConverter = s_jsonEnumerableConverter; } + // Else if IList can't be assigned from the property type (we populate and return an IList directly) + // and the type can be constructed with an IEnumerable, then use the + // IEnumerableConstructible converter to create the instance. + else if (!typeof(IList).IsAssignableFrom(RuntimePropertyType) && + RuntimePropertyType.GetConstructor(new Type[] { typeof(List<>).MakeGenericType(elementType) }) != null) + { + EnumerableConverter = s_jsonIEnumerableConstuctibleConverter; + } + // Else if it's a System.Collections.Immutable type with one generic argument. + else if (RuntimePropertyType.IsGenericType && + RuntimePropertyType.FullName.StartsWith(DefaultImmutableConverter.ImmutableNamespace) && + RuntimePropertyType.GetGenericArguments().Length == 1) + { + EnumerableConverter = s_jsonImmutableConverter; + ((DefaultImmutableConverter)EnumerableConverter).RegisterImmutableCollectionType(RuntimePropertyType, elementType, options); + } } } else @@ -259,6 +272,10 @@ namespace System.Text.Json.Serialization return (TAttribute)PropertyInfo?.GetCustomAttribute(typeof(TAttribute), inherit: false); } + public abstract IEnumerable CreateImmutableCollectionFromList(string delegateKey, IList sourceList); + + public abstract IEnumerable CreateIEnumerableConstructibleType(Type enumerableType, IList sourceList); + public abstract IList CreateConverterList(); public abstract Type GetDictionaryConcreteType(); diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonPropertyInfoCommon.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonPropertyInfoCommon.cs index 50f22bd..708f2d9 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonPropertyInfoCommon.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonPropertyInfoCommon.cs @@ -96,5 +96,31 @@ namespace System.Text.Json.Serialization { return typeof(Dictionary); } + + // Creates an IEnumerable and populates it with the items in the, + // sourceList argument then uses the delegateKey argument to identify the appropriate cached + // CreateRange method to create and return the desired immutable collection type. + public override IEnumerable CreateImmutableCollectionFromList(string delegateKey, IList sourceList) + { + Debug.Assert(DefaultImmutableConverter.CreateRangeDelegates.ContainsKey(delegateKey)); + + DefaultImmutableConverter.ImmutableCreateRangeDelegate createRangeDelegate = ( + (DefaultImmutableConverter.ImmutableCreateRangeDelegate)DefaultImmutableConverter.CreateRangeDelegates[delegateKey]); + + return (IEnumerable)createRangeDelegate.Invoke(CreateGenericIEnumerableFromList(sourceList)); + } + + public override IEnumerable CreateIEnumerableConstructibleType(Type enumerableType, IList sourceList) + { + return (IEnumerable)Activator.CreateInstance(enumerableType, CreateGenericIEnumerableFromList(sourceList)); + } + + private IEnumerable CreateGenericIEnumerableFromList(IList sourceList) + { + foreach (object item in sourceList) + { + yield return (TRuntimeProperty)item; + } + } } } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.HandleArray.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.HandleArray.cs index a19e02f..b36dbdf 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.HandleArray.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.HandleArray.cs @@ -106,8 +106,7 @@ namespace System.Text.Json.Serialization JsonEnumerableConverter converter = state.Current.JsonPropertyInfo.EnumerableConverter; Debug.Assert(converter != null); - Type elementType = state.Current.GetElementType(); - value = converter.CreateFromList(elementType, (IList)value); + value = converter.CreateFromList(ref state, (IList)value, options); setPropertyDirectly = true; } else if (!popStackOnEnd) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReflectionEmitMaterializer.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReflectionEmitMaterializer.cs index d6e0efe..be5dec1 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReflectionEmitMaterializer.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReflectionEmitMaterializer.cs @@ -6,6 +6,7 @@ using System.Diagnostics; using System.Reflection; using System.Reflection.Emit; +using System.Text.Json.Serialization.Converters; namespace System.Text.Json.Serialization { @@ -34,6 +35,14 @@ namespace System.Text.Json.Serialization return (JsonClassInfo.ConstructorDelegate)dynamicMethod.CreateDelegate(typeof(JsonClassInfo.ConstructorDelegate)); } + + public override object ImmutableCreateRange(Type constructingType, Type elementType) + { + MethodInfo createRange = ImmutableCreateRangeMethod(constructingType, elementType); + + return createRange.CreateDelegate( + typeof(DefaultImmutableConverter.ImmutableCreateRangeDelegate<>).MakeGenericType(elementType), null); + } } } #endif diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReflectionMaterializer.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReflectionMaterializer.cs index 284affc..5c5c431 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReflectionMaterializer.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReflectionMaterializer.cs @@ -2,6 +2,10 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System.Diagnostics; +using System.Reflection; +using System.Text.Json.Serialization.Converters; + namespace System.Text.Json.Serialization { internal sealed class ReflectionMaterializer : ClassMaterializer @@ -10,5 +14,13 @@ namespace System.Text.Json.Serialization { return () => Activator.CreateInstance(type); } + + public override object ImmutableCreateRange(Type constructingType, Type elementType) + { + MethodInfo createRange = ImmutableCreateRangeMethod(constructingType, elementType); + + return createRange.CreateDelegate( + typeof(DefaultImmutableConverter.ImmutableCreateRangeDelegate<>).MakeGenericType(elementType), null); + } } } diff --git a/src/libraries/System.Text.Json/tests/Serialization/Array.ReadTests.cs b/src/libraries/System.Text.Json/tests/Serialization/Array.ReadTests.cs index 85fb647..29458f2 100644 --- a/src/libraries/System.Text.Json/tests/Serialization/Array.ReadTests.cs +++ b/src/libraries/System.Text.Json/tests/Serialization/Array.ReadTests.cs @@ -3,6 +3,8 @@ // See the LICENSE file in the project root for more information. using System.Collections.Generic; +using System.Collections.Immutable; +using System.Reflection; using Xunit; namespace System.Text.Json.Serialization.Tests @@ -161,5 +163,19 @@ namespace System.Text.Json.Serialization.Tests TestClassWithGenericIReadOnlyListT obj = JsonSerializer.Parse(TestClassWithGenericIReadOnlyListT.s_data); obj.Verify(); } + + [Fact] + public static void ReadClassWithObjectIEnumerableConstructibleTypes() + { + TestClassWithObjectIEnumerableConstructibleTypes obj = JsonSerializer.Parse(TestClassWithObjectIEnumerableConstructibleTypes.s_data); + obj.Verify(); + } + + [Fact] + public static void ReadClassWithObjectImmutableTypes() + { + TestClassWithObjectImmutableTypes obj = JsonSerializer.Parse(TestClassWithObjectImmutableTypes.s_data); + obj.Verify(); + } } } diff --git a/src/libraries/System.Text.Json/tests/Serialization/Array.WriteTests.cs b/src/libraries/System.Text.Json/tests/Serialization/Array.WriteTests.cs index d6e0715..2926d4b 100644 --- a/src/libraries/System.Text.Json/tests/Serialization/Array.WriteTests.cs +++ b/src/libraries/System.Text.Json/tests/Serialization/Array.WriteTests.cs @@ -318,5 +318,51 @@ namespace System.Text.Json.Serialization.Tests obj.Verify(); } } + + [Fact] + public static void WriteClassWithObjectIEnumerableConstructibleTypes() + { + string json; + + { + TestClassWithObjectIEnumerableConstructibleTypes obj = new TestClassWithObjectIEnumerableConstructibleTypes(); + obj.Initialize(); + obj.Verify(); + json = JsonSerializer.ToString(obj); + } + + { + TestClassWithObjectIEnumerableConstructibleTypes obj = JsonSerializer.Parse(json); + obj.Verify(); + } + + { + TestClassWithObjectIEnumerableConstructibleTypes obj = JsonSerializer.Parse(TestClassWithObjectIEnumerableConstructibleTypes.s_data); + obj.Verify(); + } + } + + [Fact] + public static void WriteClassWithObjectImmutableTypes() + { + string json; + + { + TestClassWithObjectImmutableTypes obj = new TestClassWithObjectImmutableTypes(); + obj.Initialize(); + obj.Verify(); + json = JsonSerializer.ToString(obj); + } + + { + TestClassWithObjectImmutableTypes obj = JsonSerializer.Parse(json); + obj.Verify(); + } + + { + TestClassWithObjectImmutableTypes obj = JsonSerializer.Parse(TestClassWithObjectImmutableTypes.s_data); + obj.Verify(); + } + } } } diff --git a/src/libraries/System.Text.Json/tests/Serialization/Null.WriteTests.cs b/src/libraries/System.Text.Json/tests/Serialization/Null.WriteTests.cs index 20b1925..1759d25 100644 --- a/src/libraries/System.Text.Json/tests/Serialization/Null.WriteTests.cs +++ b/src/libraries/System.Text.Json/tests/Serialization/Null.WriteTests.cs @@ -52,6 +52,11 @@ namespace System.Text.Json.Serialization.Tests obj.ICollectionT = null; obj.IReadOnlyCollectionT = null; obj.IReadOnlyListT = null; + obj.StackT = null; + obj.QueueT = null; + obj.HashSetT = null; + obj.LinkedListT = null; + obj.SortedSetT = null; obj.NullableInt = null; obj.NullableIntArray = null; obj.Object = null; @@ -65,6 +70,11 @@ namespace System.Text.Json.Serialization.Tests Assert.Contains(@"""ICollectionT"":null", json); Assert.Contains(@"""IReadOnlyCollectionT"":null", json); Assert.Contains(@"""IReadOnlyListT"":null", json); + Assert.Contains(@"""StackT"":null", json); + Assert.Contains(@"""QueueT"":null", json); + Assert.Contains(@"""HashSetT"":null", json); + Assert.Contains(@"""LinkedListT"":null", json); + Assert.Contains(@"""SortedSetT"":null", json); Assert.Contains(@"""NullableInt"":null", json); Assert.Contains(@"""Object"":null", json); Assert.Contains(@"""NullableIntArray"":null", json); diff --git a/src/libraries/System.Text.Json/tests/Serialization/PolymorphicTests.cs b/src/libraries/System.Text.Json/tests/Serialization/PolymorphicTests.cs index eaa0cd5..238d405 100644 --- a/src/libraries/System.Text.Json/tests/Serialization/PolymorphicTests.cs +++ b/src/libraries/System.Text.Json/tests/Serialization/PolymorphicTests.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Collections.Generic; +using System.Collections.Immutable; using Xunit; namespace System.Text.Json.Serialization.Tests @@ -68,6 +69,9 @@ namespace System.Text.Json.Serialization.Tests public static void ArrayAsRootObject() { const string ExpectedJson = @"[1,true,{""City"":""MyCity""},null,""foo""]"; + const string ReversedExpectedJson = @"[""foo"",null,{""City"":""MyCity""},true,1]"; + + string[] expectedObjects = { @"""foo""", @"null", @"{""City"":""MyCity""}", @"true", @"1" }; var address = new Address(); address.Initialize(); @@ -125,6 +129,102 @@ namespace System.Text.Json.Serialization.Tests json = JsonSerializer.ToString(ireadonlylist); Assert.Equal(ExpectedJson, json); + + Stack stack = new Stack(new List { 1, true, address, null, "foo" }); + json = JsonSerializer.ToString(stack); + Assert.Equal(ReversedExpectedJson, json); + + json = JsonSerializer.ToString(stack); + Assert.Equal(ReversedExpectedJson, json); + + Queue queue = new Queue(new List { 1, true, address, null, "foo" }); + json = JsonSerializer.ToString(queue); + Assert.Equal(ExpectedJson, json); + + json = JsonSerializer.ToString(queue); + Assert.Equal(ExpectedJson, json); + + HashSet hashset = new HashSet(new List { 1, true, address, null, "foo" }); + json = JsonSerializer.ToString(hashset); + Assert.Equal(ExpectedJson, json); + + json = JsonSerializer.ToString(hashset); + Assert.Equal(ExpectedJson, json); + + LinkedList linkedlist = new LinkedList(new List { 1, true, address, null, "foo" }); + json = JsonSerializer.ToString(linkedlist); + Assert.Equal(ExpectedJson, json); + + json = JsonSerializer.ToString(linkedlist); + Assert.Equal(ExpectedJson, json); + + IImmutableList iimmutablelist = ImmutableList.CreateRange(new List { 1, true, address, null, "foo" }); + json = JsonSerializer.ToString(iimmutablelist); + Assert.Equal(ExpectedJson, json); + + json = JsonSerializer.ToString(iimmutablelist); + Assert.Equal(ExpectedJson, json); + + IImmutableStack iimmutablestack = ImmutableStack.CreateRange(new List { 1, true, address, null, "foo" }); + json = JsonSerializer.ToString(iimmutablestack); + Assert.Equal(ReversedExpectedJson, json); + + json = JsonSerializer.ToString(iimmutablestack); + Assert.Equal(ReversedExpectedJson, json); + + IImmutableQueue iimmutablequeue = ImmutableQueue.CreateRange(new List { 1, true, address, null, "foo" }); + json = JsonSerializer.ToString(iimmutablequeue); + Assert.Equal(ExpectedJson, json); + + json = JsonSerializer.ToString(iimmutablequeue); + Assert.Equal(ExpectedJson, json); + + IImmutableSet iimmutableset = ImmutableHashSet.CreateRange(new List { 1, true, address, null, "foo" }); + json = JsonSerializer.ToString(iimmutableset); + foreach (string obj in expectedObjects) + { + Assert.Contains(obj, json); + } + + json = JsonSerializer.ToString(iimmutableset); + foreach (string obj in expectedObjects) + { + Assert.Contains(obj, json); + } + + ImmutableHashSet immutablehashset = ImmutableHashSet.CreateRange(new List { 1, true, address, null, "foo" }); + json = JsonSerializer.ToString(immutablehashset); + foreach (string obj in expectedObjects) + { + Assert.Contains(obj, json); + } + + json = JsonSerializer.ToString(immutablehashset); + foreach (string obj in expectedObjects) + { + Assert.Contains(obj, json); + } + + ImmutableList immutablelist = ImmutableList.CreateRange(new List { 1, true, address, null, "foo" }); + json = JsonSerializer.ToString(immutablelist); + Assert.Equal(ExpectedJson, json); + + json = JsonSerializer.ToString(immutablelist); + Assert.Equal(ExpectedJson, json); + + ImmutableStack immutablestack = ImmutableStack.CreateRange(new List { 1, true, address, null, "foo" }); + json = JsonSerializer.ToString(immutablestack); + Assert.Equal(ReversedExpectedJson, json); + + json = JsonSerializer.ToString(immutablestack); + Assert.Equal(ReversedExpectedJson, json); + + ImmutableQueue immutablequeue = ImmutableQueue.CreateRange(new List { 1, true, address, null, "foo" }); + json = JsonSerializer.ToString(immutablequeue); + Assert.Equal(ExpectedJson, json); + + json = JsonSerializer.ToString(immutablequeue); + Assert.Equal(ExpectedJson, json); } [Fact] @@ -164,6 +264,20 @@ namespace System.Text.Json.Serialization.Tests Assert.Contains(@"""ICollectionT"":[""Hello"",""World""]", json); Assert.Contains(@"""IReadOnlyCollectionT"":[""Hello"",""World""]", json); Assert.Contains(@"""IReadOnlyListT"":[""Hello"",""World""]", json); + Assert.Contains(@"""StackT"":[""World"",""Hello""]", json); + Assert.Contains(@"""QueueT"":[""Hello"",""World""]", json); + Assert.Contains(@"""HashSetT"":[""Hello"",""World""]", json); + Assert.Contains(@"""LinkedListT"":[""Hello"",""World""]", json); + Assert.Contains(@"""SortedSetT"":[""Hello"",""World""]", json); + Assert.Contains(@"""IImmutableListT"":[""Hello"",""World""]", json); + Assert.Contains(@"""IImmutableStackT"":[""World"",""Hello""]", json); + Assert.Contains(@"""IImmutableQueueT"":[""Hello"",""World""]", json); + Assert.True(json.Contains(@"""IImmutableSetT"":[""Hello"",""World""]") || json.Contains(@"""IImmutableSetT"":[""World"",""Hello""]")); + Assert.True(json.Contains(@"""ImmutableHashSetT"":[""Hello"",""World""]") || json.Contains(@"""ImmutableHashSetT"":[""World"",""Hello""]")); + Assert.Contains(@"""ImmutableListT"":[""Hello"",""World""]", json); + Assert.Contains(@"""ImmutableStackT"":[""World"",""Hello""]", json); + Assert.Contains(@"""ImmutableQueueT"":[""Hello"",""World""]", json); + Assert.Contains(@"""ImmutableSortedSetT"":[""Hello"",""World""]", json); Assert.Contains(@"""NullableInt"":42", json); Assert.Contains(@"""Object"":{}", json); Assert.Contains(@"""NullableIntArray"":[null,42,null]", json); diff --git a/src/libraries/System.Text.Json/tests/Serialization/TestClasses.Polymorphic.cs b/src/libraries/System.Text.Json/tests/Serialization/TestClasses.Polymorphic.cs index f97e14c..23708f6 100644 --- a/src/libraries/System.Text.Json/tests/Serialization/TestClasses.Polymorphic.cs +++ b/src/libraries/System.Text.Json/tests/Serialization/TestClasses.Polymorphic.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Collections.Generic; +using System.Collections.Immutable; using Xunit; namespace System.Text.Json.Serialization.Tests @@ -115,6 +116,20 @@ namespace System.Text.Json.Serialization.Tests public object /*ICollection*/ ICollectionT { get; set; } public object /*IReadOnlyCollection*/ IReadOnlyCollectionT { get; set; } public object /*IReadOnlyList*/ IReadOnlyListT { get; set; } + public object /*Stack*/ StackT { get; set; } + public object /*Queue*/ QueueT { get; set; } + public object /*HashSet*/ HashSetT { get; set; } + public object /*LinkedList*/ LinkedListT { get; set; } + public object /*SortedSet*/ SortedSetT { get; set; } + public object /*IImmutableList*/ IImmutableListT { get; set; } + public object /*IImmutableStack*/ IImmutableStackT { get; set; } + public object /*IImmutableQueue*/ IImmutableQueueT { get; set; } + public object /*IImmutableSet*/ IImmutableSetT { get; set; } + public object /*ImmutableHashSet*/ ImmutableHashSetT { get; set; } + public object /*ImmutableList*/ ImmutableListT { get; set; } + public object /*ImmutableStack*/ ImmutableStackT { get; set; } + public object /*ImmutableQueue*/ ImmutableQueueT { get; set; } + public object /*ImmutableSortedSet*/ ImmutableSortedSetT { get; set; } public object /*int?*/ NullableInt { get; set; } public object /*object*/ Object { get; set; } public object /*int?[]*/ NullableIntArray { get; set; } @@ -124,40 +139,27 @@ namespace System.Text.Json.Serialization.Tests Address = new Address(); ((Address)Address).Initialize(); - List = new List - { - "Hello", "World" - }; - - Array = new string[] - { - "Hello", "Again" - }; - - IEnumerableT = new List - { - "Hello", "World" - }; - - IListT = new List - { - "Hello", "World" - }; - - ICollectionT = new List - { - "Hello", "World" - }; - - IReadOnlyCollectionT = new List - { - "Hello", "World" - }; - - IReadOnlyListT = new List - { - "Hello", "World" - }; + List = new List { "Hello", "World" }; + Array = new string[] { "Hello", "Again" }; + IEnumerableT = new List { "Hello", "World" }; + IListT = new List { "Hello", "World" }; + ICollectionT = new List { "Hello", "World" }; + IReadOnlyCollectionT = new List { "Hello", "World" }; + IReadOnlyListT = new List { "Hello", "World" }; + StackT = new Stack(new List { "Hello", "World" }); + QueueT = new Queue(new List { "Hello", "World" }); + HashSetT = new HashSet(new List { "Hello", "World" }); + LinkedListT = new LinkedList(new List { "Hello", "World" }); + SortedSetT = new SortedSet(new List { "Hello", "World" }); + IImmutableListT = ImmutableList.CreateRange(new List { "Hello", "World" }); + IImmutableStackT = ImmutableStack.CreateRange(new List { "Hello", "World" }); + IImmutableQueueT = ImmutableQueue.CreateRange(new List { "Hello", "World" }); + IImmutableSetT = ImmutableHashSet.CreateRange(new List { "Hello", "World" }); + ImmutableHashSetT = ImmutableHashSet.CreateRange(new List { "Hello", "World" }); + ImmutableListT = ImmutableList.CreateRange(new List { "Hello", "World" }); + ImmutableStackT = ImmutableStack.CreateRange(new List { "Hello", "World" }); + ImmutableQueueT = ImmutableQueue.CreateRange(new List { "Hello", "World" }); + ImmutableSortedSetT = ImmutableSortedSet.CreateRange(new List { "Hello", "World" }); NullableInt = new int?(42); Object = new object(); diff --git a/src/libraries/System.Text.Json/tests/Serialization/TestClasses.SimpleTestClass.cs b/src/libraries/System.Text.Json/tests/Serialization/TestClasses.SimpleTestClass.cs index 8edbc63..f5b090f 100644 --- a/src/libraries/System.Text.Json/tests/Serialization/TestClasses.SimpleTestClass.cs +++ b/src/libraries/System.Text.Json/tests/Serialization/TestClasses.SimpleTestClass.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Collections.Generic; +using System.Collections.Immutable; using System.Linq; using Xunit; @@ -57,6 +58,20 @@ namespace System.Text.Json.Serialization.Tests public Dictionary MyStringToStringDict { get; set; } public IDictionary MyStringToStringIDict { get; set; } public IReadOnlyDictionary MyStringToStringIReadOnlyDict { get; set; } + public Stack MyStringStackT { get; set; } + public Queue MyStringQueueT { get; set; } + public HashSet MyStringHashSetT { get; set; } + public LinkedList MyStringLinkedListT { get; set; } + public SortedSet MyStringSortedSetT { get; set; } + public IImmutableList MyStringIImmutableListT { get; set; } + public IImmutableStack MyStringIImmutableStackT { get; set; } + public IImmutableQueue MyStringIImmutableQueueT { get; set; } + public IImmutableSet MyStringIImmutableSetT { get; set; } + public ImmutableHashSet MyStringImmutableHashSetT { get; set; } + public ImmutableList MyStringImmutableListT { get; set; } + public ImmutableStack MyStringImmutableStackT { get; set; } + public ImmutableQueue MyStringImmutablQueueT { get; set; } + public ImmutableSortedSet MyStringImmutableSortedSetT { get; set; } public List MyListOfNullString { get; set; } public static readonly string s_json = $"{{{s_partialJsonProperties},{s_partialJsonArrays}}}"; @@ -110,6 +125,20 @@ namespace System.Text.Json.Serialization.Tests @"""MyStringICollectionT"" : [""Hello""]," + @"""MyStringIReadOnlyCollectionT"" : [""Hello""]," + @"""MyStringIReadOnlyListT"" : [""Hello""]," + + @"""MyStringStackT"" : [""Hello"", ""World""]," + + @"""MyStringQueueT"" : [""Hello"", ""World""]," + + @"""MyStringHashSetT"" : [""Hello""]," + + @"""MyStringLinkedListT"" : [""Hello""]," + + @"""MyStringSortedSetT"" : [""Hello""]," + + @"""MyStringIImmutableListT"" : [""Hello""]," + + @"""MyStringIImmutableStackT"" : [""Hello""]," + + @"""MyStringIImmutableQueueT"" : [""Hello""]," + + @"""MyStringIImmutableSetT"" : [""Hello""]," + + @"""MyStringImmutableHashSetT"" : [""Hello""]," + + @"""MyStringImmutableListT"" : [""Hello""]," + + @"""MyStringImmutableStackT"" : [""Hello""]," + + @"""MyStringImmutablQueueT"" : [""Hello""]," + + @"""MyStringImmutableSortedSetT"" : [""Hello""]," + @"""MyListOfNullString"" : [null]"; public static readonly byte[] s_data = Encoding.UTF8.GetBytes(s_json); @@ -164,6 +193,23 @@ namespace System.Text.Json.Serialization.Tests MyStringToStringDict = new Dictionary { { "key", "value" } }; MyStringToStringIDict = new Dictionary { { "key", "value" } }; MyStringToStringIReadOnlyDict = new Dictionary { { "key", "value" } }; + + MyStringStackT = new Stack(new List() { "Hello", "World" } ); + MyStringQueueT = new Queue(new List() { "Hello", "World" }); + MyStringHashSetT = new HashSet(new List() { "Hello" }); + MyStringLinkedListT = new LinkedList(new List() { "Hello" }); + MyStringSortedSetT = new SortedSet(new List() { "Hello" }); + + MyStringIImmutableListT = ImmutableList.CreateRange(new List { "Hello" }); + MyStringIImmutableStackT = ImmutableStack.CreateRange(new List { "Hello" }); + MyStringIImmutableQueueT = ImmutableQueue.CreateRange(new List { "Hello" }); + MyStringIImmutableSetT = ImmutableHashSet.CreateRange(new List { "Hello" }); + MyStringImmutableHashSetT = ImmutableHashSet.CreateRange(new List { "Hello" }); + MyStringImmutableListT = ImmutableList.CreateRange(new List { "Hello" }); + MyStringImmutableStackT = ImmutableStack.CreateRange(new List { "Hello" }); + MyStringImmutablQueueT = ImmutableQueue.CreateRange(new List { "Hello" }); + MyStringImmutableSortedSetT = ImmutableSortedSet.CreateRange(new List { "Hello" }); + MyListOfNullString = new List { null }; } @@ -217,6 +263,33 @@ namespace System.Text.Json.Serialization.Tests Assert.Equal("value", MyStringToStringDict["key"]); Assert.Equal("value", MyStringToStringIDict["key"]); Assert.Equal("value", MyStringToStringIReadOnlyDict["key"]); + + Assert.Equal(2, MyStringStackT.Count); + Assert.True(MyStringStackT.Contains("Hello")); + Assert.True(MyStringStackT.Contains("World")); + + string[] expectedQueue = { "Hello", "World" }; + int i = 0; + foreach (string item in MyStringQueueT) + { + Assert.Equal(expectedQueue[i], item); + i++; + } + + Assert.Equal("Hello", MyStringHashSetT.First()); + Assert.Equal("Hello", MyStringLinkedListT.First()); + Assert.Equal("Hello", MyStringSortedSetT.First()); + + Assert.Equal("Hello", MyStringIImmutableListT[0]); + Assert.Equal("Hello", MyStringIImmutableStackT.First()); + Assert.Equal("Hello", MyStringIImmutableQueueT.First()); + Assert.Equal("Hello", MyStringIImmutableSetT.First()); + Assert.Equal("Hello", MyStringImmutableHashSetT.First()); + Assert.Equal("Hello", MyStringImmutableListT[0]); + Assert.Equal("Hello", MyStringImmutableStackT.First()); + Assert.Equal("Hello", MyStringImmutablQueueT.First()); + Assert.Equal("Hello", MyStringImmutableSortedSetT.First()); + Assert.Null(MyListOfNullString[0]); } } diff --git a/src/libraries/System.Text.Json/tests/Serialization/TestClasses.SimpleTestClassWithObject.cs b/src/libraries/System.Text.Json/tests/Serialization/TestClasses.SimpleTestClassWithObject.cs index 042b395..6ae0511 100644 --- a/src/libraries/System.Text.Json/tests/Serialization/TestClasses.SimpleTestClassWithObject.cs +++ b/src/libraries/System.Text.Json/tests/Serialization/TestClasses.SimpleTestClassWithObject.cs @@ -3,6 +3,8 @@ // See the LICENSE file in the project root for more information. using System.Collections.Generic; +using System.Collections.Immutable; +using System.Linq; using Xunit; namespace System.Text.Json.Serialization.Tests @@ -35,6 +37,20 @@ namespace System.Text.Json.Serialization.Tests public object MyStringToStringDict { get; set; } public object MyStringToStringIDict { get; set; } public object MyStringToStringIReadOnlyDict { get; set; } + public object MyStringStackT { get; set; } + public object MyStringQueueT { get; set; } + public object MyStringHashSetT { get; set; } + public object MyStringLinkedListT { get; set; } + public object MyStringSortedSetT { get; set; } + public object MyStringIImmutableListT { get; set; } + public object MyStringIImmutableStackT { get; set; } + public object MyStringIImmutableQueueT { get; set; } + public object MyStringIImmutableSetT { get; set; } + public object MyStringImmutableHashSetT { get; set; } + public object MyStringImmutableListT { get; set; } + public object MyStringImmutableStackT { get; set; } + public object MyStringImmutablQueueT { get; set; } + public object MyStringImmutableSortedSetT { get; set; } public new static readonly string s_json = @"{" + @@ -81,6 +97,20 @@ namespace System.Text.Json.Serialization.Tests @"""MyStringToStringDict"" : {""key"" : ""value""}," + @"""MyStringToStringIDict"" : {""key"" : ""value""}," + @"""MyStringToStringIReadOnlyDict"" : {""key"" : ""value""}" + + @"""MyStringStackT"" : [""Hello"", ""World""]," + + @"""MyStringQueueT"" : [""Hello"", ""World""]," + + @"""MyStringHashSetT"" : [""Hello""]," + + @"""MyStringLinkedListT"" : [""Hello""]," + + @"""MyStringSortedSetT"" : [""Hello""]," + + @"""MyStringIImmutableListT"" : [""Hello""]," + + @"""MyStringIImmutableStackT"" : [""Hello""]," + + @"""MyStringIImmutableQueueT"" : [""Hello""]," + + @"""MyStringIImmutableSetT"" : [""Hello""]," + + @"""MyStringImmutableHashSetT"" : [""Hello""]," + + @"""MyStringImmutableListT"" : [""Hello""]," + + @"""MyStringImmutableStackT"" : [""Hello""]," + + @"""MyStringImmutablQueueT"" : [""Hello""]," + + @"""MyStringImmutableSortedSetT"" : [""Hello""]" + @"}"; public new static readonly byte[] s_data = Encoding.UTF8.GetBytes(s_json); @@ -117,6 +147,22 @@ namespace System.Text.Json.Serialization.Tests MyStringToStringDict = new Dictionary { { "key", "value" } }; MyStringToStringIDict = new Dictionary { { "key", "value" } }; MyStringToStringIReadOnlyDict = new Dictionary { { "key", "value" } }; + + MyStringStackT = new Stack(new List() { "Hello", "World" }); + MyStringQueueT = new Queue(new List() { "Hello", "World" }); + MyStringHashSetT = new HashSet(new List() { "Hello" }); + MyStringLinkedListT = new LinkedList(new List() { "Hello" }); + MyStringSortedSetT = new SortedSet(new List() { "Hello" }); + + MyStringIImmutableListT = ImmutableList.CreateRange(new List { "Hello" }); + MyStringIImmutableStackT = ImmutableStack.CreateRange(new List { "Hello" }); + MyStringIImmutableQueueT = ImmutableQueue.CreateRange(new List { "Hello" }); + MyStringIImmutableSetT = ImmutableHashSet.CreateRange(new List { "Hello" }); + MyStringImmutableHashSetT = ImmutableHashSet.CreateRange(new List { "Hello" }); + MyStringImmutableListT = ImmutableList.CreateRange(new List { "Hello" }); + MyStringImmutableStackT = ImmutableStack.CreateRange(new List { "Hello" }); + MyStringImmutablQueueT = ImmutableQueue.CreateRange(new List { "Hello" }); + MyStringImmutableSortedSetT = ImmutableSortedSet.CreateRange(new List { "Hello" }); } public override void Verify() @@ -140,6 +186,43 @@ namespace System.Text.Json.Serialization.Tests Assert.Equal(2.2d, ((double[])MyDoubleArray)[0]); Assert.Equal(new DateTime(2019, 1, 30, 12, 1, 2, DateTimeKind.Utc), ((DateTime[])MyDateTimeArray)[0]); Assert.Equal(SampleEnum.Two, ((SampleEnum[])MyEnumArray)[0]); + + Assert.Equal("Hello", ((List)MyStringList)[0]); + Assert.Equal("Hello", ((IEnumerable)MyStringIEnumerableT).First()); + Assert.Equal("Hello", ((IList)MyStringIListT)[0]); + Assert.Equal("Hello", ((ICollection)MyStringICollectionT).First()); + Assert.Equal("Hello", ((IReadOnlyCollection)MyStringIReadOnlyCollectionT).First()); + Assert.Equal("Hello", ((IReadOnlyList)MyStringIReadOnlyListT)[0]); + + Assert.Equal("value", ((Dictionary)MyStringToStringDict)["key"]); + Assert.Equal("value", ((IDictionary)MyStringToStringIDict)["key"]); + Assert.Equal("value", ((IReadOnlyDictionary)MyStringToStringIReadOnlyDict)["key"]); + + Assert.Equal(2, ((Stack)MyStringStackT).Count); + Assert.True(((Stack)MyStringStackT).Contains("Hello")); + Assert.True(((Stack)MyStringStackT).Contains("World")); + + string[] expectedQueue = { "Hello", "World" }; + int i = 0; + foreach (string item in ((Queue)MyStringQueueT)) + { + Assert.Equal(expectedQueue[i], item); + i++; + } + + Assert.Equal("Hello", ((HashSet)MyStringHashSetT).First()); + Assert.Equal("Hello", ((LinkedList)MyStringLinkedListT).First()); + Assert.Equal("Hello", ((SortedSet)MyStringSortedSetT).First()); + + Assert.Equal("Hello", ((IImmutableList)MyStringIImmutableListT)[0]); + Assert.Equal("Hello", ((IImmutableStack)MyStringIImmutableStackT).First()); + Assert.Equal("Hello", ((IImmutableQueue)MyStringIImmutableQueueT).First()); + Assert.Equal("Hello", ((IImmutableSet)MyStringIImmutableSetT).First()); + Assert.Equal("Hello", ((ImmutableHashSet)MyStringImmutableHashSetT).First()); + Assert.Equal("Hello", ((ImmutableList)MyStringImmutableListT)[0]); + Assert.Equal("Hello", ((ImmutableStack)MyStringImmutableStackT).First()); + Assert.Equal("Hello", ((ImmutableQueue)MyStringImmutablQueueT).First()); + Assert.Equal("Hello", ((ImmutableSortedSet)MyStringImmutableSortedSetT).First()); } } } diff --git a/src/libraries/System.Text.Json/tests/Serialization/TestClasses.cs b/src/libraries/System.Text.Json/tests/Serialization/TestClasses.cs index 380fbab..22e7eac 100644 --- a/src/libraries/System.Text.Json/tests/Serialization/TestClasses.cs +++ b/src/libraries/System.Text.Json/tests/Serialization/TestClasses.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Collections.Generic; +using System.Collections.Immutable; using System.Linq; using Xunit; @@ -768,6 +769,295 @@ namespace System.Text.Json.Serialization.Tests } } + public class TestClassWithObjectIEnumerableConstructibleTypes : ITestClass + { + public Stack MyStack { get; set; } + public Queue MyQueue { get; set; } + public HashSet MyHashSet { get; set; } + public LinkedList MyLinkedList { get; set; } + + public static readonly byte[] s_data = Encoding.UTF8.GetBytes( + @"{" + + @"""MyStack"":[" + + SimpleTestClass.s_json + "," + + SimpleTestClass.s_json + + @"]," + + @"""MyQueue"":[" + + SimpleTestClass.s_json + "," + + SimpleTestClass.s_json + + @"]," + + @"""MyHashSet"":[" + + SimpleTestClass.s_json + "," + + SimpleTestClass.s_json + + @"]," + + @"""MyLinkedList"":[" + + SimpleTestClass.s_json + "," + + SimpleTestClass.s_json + + @"]" + + @"}"); + + public void Initialize() + { + MyStack = new Stack(); + { + SimpleTestClass obj = new SimpleTestClass(); + obj.Initialize(); + MyStack.Push(obj); + } + { + SimpleTestClass obj = new SimpleTestClass(); + obj.Initialize(); + MyStack.Push(obj); + } + + MyQueue = new Queue(); + { + SimpleTestClass obj = new SimpleTestClass(); + obj.Initialize(); + MyQueue.Enqueue(obj); + } + { + SimpleTestClass obj = new SimpleTestClass(); + obj.Initialize(); + MyQueue.Enqueue(obj); + } + + MyHashSet = new HashSet(); + { + SimpleTestClass obj = new SimpleTestClass(); + obj.Initialize(); + MyHashSet.Add(obj); + } + { + SimpleTestClass obj = new SimpleTestClass(); + obj.Initialize(); + MyHashSet.Add(obj); + } + + MyLinkedList = new LinkedList(); + { + SimpleTestClass obj = new SimpleTestClass(); + obj.Initialize(); + MyLinkedList.AddLast(obj); + } + { + SimpleTestClass obj = new SimpleTestClass(); + obj.Initialize(); + MyLinkedList.AddLast(obj); + } + } + + public void Verify() + { + Assert.Equal(2, MyStack.Count); + foreach (SimpleTestClass data in MyStack) + { + data.Verify(); + } + + Assert.Equal(2, MyQueue.Count); + foreach (SimpleTestClass data in MyQueue) + { + data.Verify(); + } + + Assert.Equal(2, MyHashSet.Count); + foreach (SimpleTestClass data in MyHashSet) + { + data.Verify(); + } + + Assert.Equal(2, MyLinkedList.Count); + foreach (SimpleTestClass data in MyLinkedList) + { + data.Verify(); + } + } + } + + public class TestClassWithObjectImmutableTypes : ITestClass + { + public IImmutableList MyIImmutableList { get; set; } + public IImmutableStack MyIImmutableStack { get; set; } + public IImmutableQueue MyIImmutableQueue { get; set; } + public IImmutableSet MyIImmutableSet { get; set; } + public ImmutableHashSet MyImmutableHashSet { get; set; } + public ImmutableList MyImmutableList { get; set; } + public ImmutableStack MyImmutableStack { get; set; } + public ImmutableQueue MyImmutableQueue { get; set; } + + public static readonly byte[] s_data = Encoding.UTF8.GetBytes( + @"{" + + @"""MyIImmutableList"":[" + + SimpleTestClass.s_json + "," + + SimpleTestClass.s_json + + @"]," + + @"""MyIImmutableStack"":[" + + SimpleTestClass.s_json + "," + + SimpleTestClass.s_json + + @"]," + + @"""MyIImmutableQueue"":[" + + SimpleTestClass.s_json + "," + + SimpleTestClass.s_json + + @"]," + + @"""MyIImmutableSet"":[" + + SimpleTestClass.s_json + "," + + SimpleTestClass.s_json + + @"]," + + @"""MyImmutableHashSet"":[" + + SimpleTestClass.s_json + "," + + SimpleTestClass.s_json + + @"]," + + @"""MyImmutableList"":[" + + SimpleTestClass.s_json + "," + + SimpleTestClass.s_json + + @"]," + + @"""MyImmutableStack"":[" + + SimpleTestClass.s_json + "," + + SimpleTestClass.s_json + + @"]," + + @"""MyImmutableQueue"":[" + + SimpleTestClass.s_json + "," + + SimpleTestClass.s_json + + @"]" + + @"}"); + + public void Initialize() + { + { + SimpleTestClass obj1 = new SimpleTestClass(); + obj1.Initialize(); + + SimpleTestClass obj2 = new SimpleTestClass(); + obj2.Initialize(); + + MyIImmutableList = ImmutableList.CreateRange(new List { obj1, obj2 }); + } + { + SimpleTestClass obj1 = new SimpleTestClass(); + obj1.Initialize(); + + SimpleTestClass obj2 = new SimpleTestClass(); + obj2.Initialize(); + + MyIImmutableStack = ImmutableStack.CreateRange(new List { obj1, obj2 }); + } + { + SimpleTestClass obj1 = new SimpleTestClass(); + obj1.Initialize(); + + SimpleTestClass obj2 = new SimpleTestClass(); + obj2.Initialize(); + + MyIImmutableQueue = ImmutableQueue.CreateRange(new List { obj1, obj2 }); + } + { + SimpleTestClass obj1 = new SimpleTestClass(); + obj1.Initialize(); + + SimpleTestClass obj2 = new SimpleTestClass(); + obj2.Initialize(); + + MyIImmutableSet = ImmutableHashSet.CreateRange(new List { obj1, obj2 }); + } + { + SimpleTestClass obj1 = new SimpleTestClass(); + obj1.Initialize(); + + SimpleTestClass obj2 = new SimpleTestClass(); + obj2.Initialize(); + + MyImmutableHashSet = ImmutableHashSet.CreateRange(new List { obj1, obj2 }); + } + { + SimpleTestClass obj1 = new SimpleTestClass(); + obj1.Initialize(); + + SimpleTestClass obj2 = new SimpleTestClass(); + obj2.Initialize(); + + MyImmutableList = ImmutableList.CreateRange(new List { obj1, obj2 }); + } + { + SimpleTestClass obj1 = new SimpleTestClass(); + obj1.Initialize(); + + SimpleTestClass obj2 = new SimpleTestClass(); + obj2.Initialize(); + + MyImmutableStack = ImmutableStack.CreateRange(new List { obj1, obj2 }); + } + { + SimpleTestClass obj1 = new SimpleTestClass(); + obj1.Initialize(); + + SimpleTestClass obj2 = new SimpleTestClass(); + obj2.Initialize(); + + MyImmutableQueue = ImmutableQueue.CreateRange(new List { obj1, obj2 }); + } + } + + public void Verify() + { + Assert.Equal(2, MyIImmutableList.Count); + foreach (SimpleTestClass data in MyIImmutableList) + { + data.Verify(); + } + + int i = 0; + foreach (SimpleTestClass data in MyIImmutableStack) + { + data.Verify(); + i++; + } + Assert.Equal(2, i); + + i = 0; + foreach (SimpleTestClass data in MyIImmutableQueue) + { + data.Verify(); + i++; + } + Assert.Equal(2, i); + + Assert.Equal(2, MyIImmutableSet.Count); + foreach (SimpleTestClass data in MyIImmutableSet) + { + data.Verify(); + } + + Assert.Equal(2, MyImmutableHashSet.Count); + foreach (SimpleTestClass data in MyImmutableHashSet) + { + data.Verify(); + } + + Assert.Equal(2, MyImmutableList.Count); + foreach (SimpleTestClass data in MyImmutableList) + { + data.Verify(); + } + + i = 0; + foreach (SimpleTestClass data in MyImmutableStack) + { + data.Verify(); + i++; + } + Assert.Equal(2, i); + + i = 0; + foreach (SimpleTestClass data in MyImmutableQueue) + { + data.Verify(); + i++; + } + Assert.Equal(2, i); + } + } + public class SimpleDerivedTestClass : SimpleTestClass { } diff --git a/src/libraries/System.Text.Json/tests/Serialization/TestData.cs b/src/libraries/System.Text.Json/tests/Serialization/TestData.cs index 8e585d7..ebf962f 100644 --- a/src/libraries/System.Text.Json/tests/Serialization/TestData.cs +++ b/src/libraries/System.Text.Json/tests/Serialization/TestData.cs @@ -34,6 +34,9 @@ namespace System.Text.Json.Serialization.Tests yield return new object[] { typeof(TestClassWithGenericICollectionT), TestClassWithGenericICollectionT.s_data }; yield return new object[] { typeof(TestClassWithGenericIReadOnlyCollectionT), TestClassWithGenericIReadOnlyCollectionT.s_data }; yield return new object[] { typeof(TestClassWithGenericIReadOnlyListT), TestClassWithGenericIReadOnlyListT.s_data }; + yield return new object[] { typeof(TestClassWithStringToPrimitiveDictionary), TestClassWithStringToPrimitiveDictionary.s_data }; + yield return new object[] { typeof(TestClassWithObjectIEnumerableConstructibleTypes), TestClassWithObjectIEnumerableConstructibleTypes.s_data }; + yield return new object[] { typeof(TestClassWithObjectImmutableTypes), TestClassWithObjectImmutableTypes.s_data }; yield return new object[] { typeof(JsonElementTests.JsonElementClass), JsonElementTests.JsonElementClass.s_data }; yield return new object[] { typeof(JsonElementTests.JsonElementArrayClass), JsonElementTests.JsonElementArrayClass.s_data }; yield return new object[] { typeof(ClassWithComplexObjects), ClassWithComplexObjects.s_data }; @@ -65,6 +68,9 @@ namespace System.Text.Json.Serialization.Tests yield return new object[] { new TestClassWithGenericICollectionT() }; yield return new object[] { new TestClassWithGenericIReadOnlyCollectionT() }; yield return new object[] { new TestClassWithGenericIReadOnlyListT() }; + yield return new object[] { new TestClassWithStringToPrimitiveDictionary() }; + yield return new object[] { new TestClassWithObjectIEnumerableConstructibleTypes() }; + yield return new object[] { new TestClassWithObjectImmutableTypes() }; yield return new object[] { new JsonElementTests.JsonElementClass() }; yield return new object[] { new JsonElementTests.JsonElementArrayClass() }; yield return new object[] { new ClassWithComplexObjects() }; diff --git a/src/libraries/System.Text.Json/tests/Serialization/Value.ReadTests.GenericCollections.cs b/src/libraries/System.Text.Json/tests/Serialization/Value.ReadTests.GenericCollections.cs new file mode 100644 index 0000000..ca26eeb --- /dev/null +++ b/src/libraries/System.Text.Json/tests/Serialization/Value.ReadTests.GenericCollections.cs @@ -0,0 +1,631 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using System.Linq; +using Xunit; + +namespace System.Text.Json.Serialization.Tests +{ + public static partial class ValueTests + { + [Fact] + public static void ReadListOfList() + { + List> result = JsonSerializer.Parse>>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + + Assert.Equal(1, result[0][0]); + Assert.Equal(2, result[0][1]); + Assert.Equal(3, result[1][0]); + Assert.Equal(4, result[1][1]); + } + + [Fact] + public static void ReadListOfArray() + { + List result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + + Assert.Equal(1, result[0][0]); + Assert.Equal(2, result[0][1]); + Assert.Equal(3, result[1][0]); + Assert.Equal(4, result[1][1]); + } + + [Fact] + public static void ReadArrayOfList() + { + List[] result = JsonSerializer.Parse[]>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + + Assert.Equal(1, result[0][0]); + Assert.Equal(2, result[0][1]); + Assert.Equal(3, result[1][0]); + Assert.Equal(4, result[1][1]); + } + + [Fact] + public static void ReadPrimitiveList() + { + List i = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[1,2]")); + Assert.Equal(1, i[0]); + Assert.Equal(2, i[1]); + + i = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[]")); + Assert.Equal(0, i.Count); + } + + [Fact] + public static void ReadIEnumerableTOfIEnumerableT() + { + IEnumerable> result = JsonSerializer.Parse>>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 1; + + foreach (IEnumerable ie in result) + { + foreach (int i in ie) + { + Assert.Equal(expected++, i); + } + } + } + + [Fact] + public static void ReadIEnumerableTOfArray() + { + IEnumerable result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 1; + + foreach (int[] arr in result) + { + foreach (int i in arr) + { + Assert.Equal(expected++, i); + } + } + } + + [Fact] + public static void ReadArrayOfIEnumerableT() + { + IEnumerable[] result = JsonSerializer.Parse[]>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 1; + + foreach (IEnumerable arr in result) + { + foreach (int i in arr) + { + Assert.Equal(expected++, i); + } + } + } + + [Fact] + public static void ReadPrimitiveIEnumerableT() + { + IEnumerable result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[1,2]")); + int expected = 1; + + foreach (int i in result) + { + Assert.Equal(expected++, i); + } + + result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[]")); + Assert.Equal(0, result.Count()); + } + + [Fact] + public static void ReadIListTOfIListT() + { + IList> result = JsonSerializer.Parse>>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 1; + + foreach (IList ie in result) + { + foreach (int i in ie) + { + Assert.Equal(expected++, i); + } + } + } + + [Fact] + public static void ReadIListTOfArray() + { + IList result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 1; + + foreach (int[] arr in result) + { + foreach (int i in arr) + { + Assert.Equal(expected++, i); + } + } + } + + [Fact] + public static void ReadArrayOfIListT() + { + IList[] result = JsonSerializer.Parse[]>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 1; + + foreach (IList arr in result) + { + foreach (int i in arr) + { + Assert.Equal(expected++, i); + } + } + } + + [Fact] + public static void ReadPrimitiveIListT() + { + IList result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[1,2]")); + int expected = 1; + + foreach (int i in result) + { + Assert.Equal(expected++, i); + } + + result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[]")); + Assert.Equal(0, result.Count()); + } + + [Fact] + public static void ReadICollectionTOfICollectionT() + { + ICollection> result = JsonSerializer.Parse>>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 1; + + foreach (ICollection ie in result) + { + foreach (int i in ie) + { + Assert.Equal(expected++, i); + } + } + } + + [Fact] + public static void ReadICollectionTOfArray() + { + ICollection result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 1; + + foreach (int[] arr in result) + { + foreach (int i in arr) + { + Assert.Equal(expected++, i); + } + } + } + + [Fact] + public static void ReadArrayOfICollectionT() + { + ICollection[] result = JsonSerializer.Parse[]>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 1; + + foreach (ICollection arr in result) + { + foreach (int i in arr) + { + Assert.Equal(expected++, i); + } + } + } + + [Fact] + public static void ReadPrimitiveICollectionT() + { + ICollection result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[1,2]")); + int expected = 1; + + foreach (int i in result) + { + Assert.Equal(expected++, i); + } + + result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[]")); + Assert.Equal(0, result.Count()); + } + + [Fact] + public static void ReadIReadOnlyCollectionTOfIReadOnlyCollectionT() + { + IReadOnlyCollection> result = JsonSerializer.Parse>>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 1; + + foreach (IReadOnlyCollection ie in result) + { + foreach (int i in ie) + { + Assert.Equal(expected++, i); + } + } + } + + [Fact] + public static void ReadIReadOnlyCollectionTOfArray() + { + IReadOnlyCollection result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 1; + + foreach (int[] arr in result) + { + foreach (int i in arr) + { + Assert.Equal(expected++, i); + } + } + } + + [Fact] + public static void ReadArrayOfIReadOnlyCollectionT() + { + IReadOnlyCollection[] result = JsonSerializer.Parse[]>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 1; + + foreach (IReadOnlyCollection arr in result) + { + foreach (int i in arr) + { + Assert.Equal(expected++, i); + } + } + } + + [Fact] + public static void ReadPrimitiveIReadOnlyCollectionT() + { + IReadOnlyCollection result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[1,2]")); + int expected = 1; + + foreach (int i in result) + { + Assert.Equal(expected++, i); + } + + result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[]")); + Assert.Equal(0, result.Count()); + } + + [Fact] + public static void ReadIReadOnlyListTOfIReadOnlyListT() + { + IReadOnlyList> result = JsonSerializer.Parse>>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 1; + + foreach (IReadOnlyList ie in result) + { + foreach (int i in ie) + { + Assert.Equal(expected++, i); + } + } + } + + [Fact] + public static void ReadIReadOnlyListTOfArray() + { + IReadOnlyList result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 1; + + foreach (int[] arr in result) + { + foreach (int i in arr) + { + Assert.Equal(expected++, i); + } + } + } + + [Fact] + public static void ReadArrayOfIReadOnlyListT() + { + IReadOnlyList[] result = JsonSerializer.Parse[]>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 1; + + foreach (IReadOnlyList arr in result) + { + foreach (int i in arr) + { + Assert.Equal(expected++, i); + } + } + } + + [Fact] + public static void ReadPrimitiveIReadOnlyListT() + { + IReadOnlyList result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[1,2]")); + int expected = 1; + + foreach (int i in result) + { + Assert.Equal(expected++, i); + } + + result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[]")); + Assert.Equal(0, result.Count()); + } + + [Fact] + public static void StackTOfStackT() + { + Stack> result = JsonSerializer.Parse>>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 4; + + foreach (Stack st in result) + { + foreach (int i in st) + { + Assert.Equal(expected--, i); + } + } + } + + [Fact] + public static void ReadStackTOfArray() + { + Stack result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 3; + + foreach (int[] arr in result) + { + foreach (int i in arr) + { + Assert.Equal(expected++, i); + } + + expected = 1; + } + } + + [Fact] + public static void ReadArrayOfStackT() + { + Stack[] result = JsonSerializer.Parse[]>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 2; + + foreach (Stack st in result) + { + foreach (int i in st) + { + Assert.Equal(expected--, i); + } + + expected = 4; + } + } + + [Fact] + public static void ReadPrimitiveStackT() + { + Stack result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[1,2]")); + int expected = 2; + + foreach (int i in result) + { + Assert.Equal(expected--, i); + } + + result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[]")); + Assert.Equal(0, result.Count()); + } + + [Fact] + public static void ReadQueueTOfQueueT() + { + Queue> result = JsonSerializer.Parse>>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 1; + + foreach (Queue q in result) + { + foreach (int i in q) + { + Assert.Equal(expected++, i); + } + } + } + + [Fact] + public static void ReadQueueTOfArray() + { + Queue result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 1; + + foreach (int[] arr in result) + { + foreach (int i in arr) + { + Assert.Equal(expected++, i); + } + } + } + + [Fact] + public static void ReadArrayOfIQueueT() + { + Queue[] result = JsonSerializer.Parse[]>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 1; + + foreach (Queue q in result) + { + foreach (int i in q) + { + Assert.Equal(expected++, i); + } + } + } + + [Fact] + public static void ReadPrimitiveQueueT() + { + Queue result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[1,2]")); + int expected = 1; + + foreach (int i in result) + { + Assert.Equal(expected++, i); + } + result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[]")); + Assert.Equal(0, result.Count()); + + } + + [Fact] + public static void ReadHashSetTOfHashSetT() + { + HashSet> result = JsonSerializer.Parse>>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 1; + + foreach (HashSet hs in result) + { + foreach (int i in hs) + { + Assert.Equal(expected++, i); + } + } + } + + [Fact] + public static void ReadHashSetTOfArray() + { + HashSet result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 1; + + foreach (int[] arr in result) + { + foreach (int i in arr) + { + Assert.Equal(expected++, i); + } + } + } + + [Fact] + public static void ReadArrayOfIHashSetT() + { + HashSet[] result = JsonSerializer.Parse[]>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 1; + + foreach (HashSet hs in result) + { + foreach (int i in hs) + { + Assert.Equal(expected++, i); + } + } + } + + [Fact] + public static void ReadPrimitiveHashSetT() + { + HashSet result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[1,2]")); + int expected = 1; + + foreach (int i in result) + { + Assert.Equal(expected++, i); + } + + result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[]")); + Assert.Equal(0, result.Count()); + } + + [Fact] + public static void ReadLinkedListTOfLinkedListT() + { + LinkedList> result = JsonSerializer.Parse>>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 1; + + foreach (LinkedList l in result) + { + foreach (int i in l) + { + Assert.Equal(expected++, i); + } + } + } + + [Fact] + public static void ReadLinkedListTOfArray() + { + LinkedList result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 1; + + foreach (int[] arr in result) + { + foreach (int i in arr) + { + Assert.Equal(expected++, i); + } + } + } + + [Fact] + public static void ReadArrayOfILinkedListT() + { + LinkedList[] result = JsonSerializer.Parse[]>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 1; + + foreach (LinkedList l in result) + { + foreach (int i in l) + { + Assert.Equal(expected++, i); + } + } + } + + [Fact] + public static void ReadPrimitiveLinkedListT() + { + LinkedList result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[1,2]")); + int expected = 1; + + foreach (int i in result) + { + Assert.Equal(expected++, i); + } + + result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[]")); + Assert.Equal(0, result.Count()); + } + + [Fact] + public static void ReadArrayOfISortedSetT() + { + SortedSet[] result = JsonSerializer.Parse[]>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 1; + + foreach (SortedSet s in result) + { + foreach (int i in s) + { + Assert.Equal(expected++, i); + } + } + } + + [Fact] + public static void ReadPrimitiveSortedSetT() + { + SortedSet result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[1,2]")); + int expected = 1; + + foreach (int i in result) + { + Assert.Equal(expected++, i); + } + + result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[]")); + Assert.Equal(0, result.Count()); + } + } +} diff --git a/src/libraries/System.Text.Json/tests/Serialization/Value.ReadTests.ImmutableCollections.cs b/src/libraries/System.Text.Json/tests/Serialization/Value.ReadTests.ImmutableCollections.cs new file mode 100644 index 0000000..c1b923b --- /dev/null +++ b/src/libraries/System.Text.Json/tests/Serialization/Value.ReadTests.ImmutableCollections.cs @@ -0,0 +1,548 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using System.Collections.Immutable; +using System.Linq; +using Xunit; + +namespace System.Text.Json.Serialization.Tests +{ + public static partial class ValueTests + { + [Fact] + public static void ReadIImmutableListTOfIImmutableListT() + { + IImmutableList> result = JsonSerializer.Parse>>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 1; + + foreach (IImmutableList l in result) + { + foreach (int i in l) + { + Assert.Equal(expected++, i); + } + } + } + + [Fact] + public static void ReadIImmutableListTOfArray() + { + IImmutableList result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 1; + + foreach (int[] arr in result) + { + foreach (int i in arr) + { + Assert.Equal(expected++, i); + } + } + } + + [Fact] + public static void ReadArrayOfIIImmutableListT() + { + IImmutableList[] result = JsonSerializer.Parse[]>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 1; + + foreach (IImmutableList l in result) + { + foreach (int i in l) + { + Assert.Equal(expected++, i); + } + } + } + + [Fact] + public static void ReadPrimitiveIImmutableListT() + { + IImmutableList result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[1,2]")); + int expected = 1; + + foreach (int i in result) + { + Assert.Equal(expected++, i); + } + + result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[]")); + Assert.Equal(0, result.Count()); + } + + [Fact] + public static void ReadIImmutableStackTOfIImmutableStackT() + { + IImmutableStack> result = JsonSerializer.Parse>>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 4; + + foreach (IImmutableStack l in result) + { + foreach (int i in l) + { + Assert.Equal(expected--, i); + } + } + } + + [Fact] + public static void ReadIImmutableStackTOfArray() + { + IImmutableStack result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 3; + + foreach (int[] arr in result) + { + foreach (int i in arr) + { + Assert.Equal(expected++, i); + } + + expected = 1; + } + } + + [Fact] + public static void ReadArrayOfIIImmutableStackT() + { + IImmutableStack[] result = JsonSerializer.Parse[]>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 2; + + foreach (IImmutableStack l in result) + { + foreach (int i in l) + { + Assert.Equal(expected--, i); + } + + expected = 4; + } + } + + [Fact] + public static void ReadPrimitiveIImmutableStackT() + { + IImmutableStack result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[1,2]")); + int expected = 2; + + foreach (int i in result) + { + Assert.Equal(expected--, i); + } + + result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[]")); + Assert.Equal(0, result.Count()); + } + + [Fact] + public static void ReadIImmutableQueueTOfIImmutableQueueT() + { + IImmutableQueue> result = JsonSerializer.Parse>>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 1; + + foreach (IImmutableQueue l in result) + { + foreach (int i in l) + { + Assert.Equal(expected++, i); + } + } + } + + [Fact] + public static void ReadIImmutableQueueTOfArray() + { + IImmutableQueue result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 1; + + foreach (int[] arr in result) + { + foreach (int i in arr) + { + Assert.Equal(expected++, i); + } + } + } + + [Fact] + public static void ReadArrayOfIIImmutableQueueT() + { + IImmutableQueue[] result = JsonSerializer.Parse[]>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 1; + + foreach (IImmutableQueue l in result) + { + foreach (int i in l) + { + Assert.Equal(expected++, i); + } + } + } + + [Fact] + public static void ReadPrimitiveIImmutableQueueT() + { + IImmutableQueue result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[1,2]")); + int expected = 1; + + foreach (int i in result) + { + Assert.Equal(expected++, i); + } + + result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[]")); + Assert.Equal(0, result.Count()); + } + + [Fact] + public static void ReadIImmutableSetTOfIImmutableSetT() + { + IImmutableSet> result = JsonSerializer.Parse>>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + List expected = new List { 1, 2, 3, 4 }; + + foreach (IImmutableSet l in result) + { + foreach (int i in l) + { + expected.Remove(i); + } + } + + Assert.Equal(0, expected.Count); + } + + [Fact] + public static void ReadIImmutableSetTOfArray() + { + IImmutableSet result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + List expected = new List { 1, 2, 3, 4 }; + + foreach (int[] arr in result) + { + foreach (int i in arr) + { + expected.Remove(i); + } + } + + Assert.Equal(0, expected.Count); + } + + [Fact] + public static void ReadArrayOfIIImmutableSetT() + { + IImmutableSet[] result = JsonSerializer.Parse[]>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + List expected = new List { 1, 2, 3, 4 }; + + foreach (IImmutableSet l in result) + { + foreach (int i in l) + { + expected.Remove(i); + } + } + + Assert.Equal(0, expected.Count); + } + + [Fact] + public static void ReadPrimitiveIImmutableSetT() + { + IImmutableSet result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[1,2]")); + List expected = new List { 1, 2 }; + + foreach (int i in result) + { + expected.Remove(i); + } + + Assert.Equal(0, expected.Count); + + result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[]")); + Assert.Equal(0, result.Count()); + } + + [Fact] + public static void ReadImmutableHashSetTOfImmutableHashSetT() + { + ImmutableHashSet> result = JsonSerializer.Parse>>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + List expected = new List { 1, 2, 3, 4 }; + + foreach (ImmutableHashSet l in result) + { + foreach (int i in l) + { + expected.Remove(i); + } + } + + Assert.Equal(0, expected.Count); + } + + [Fact] + public static void ReadImmutableHashSetTOfArray() + { + ImmutableHashSet result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + List expected = new List { 1, 2, 3, 4 }; + + foreach (int[] arr in result) + { + foreach (int i in arr) + { + expected.Remove(i); + } + } + + Assert.Equal(0, expected.Count); + } + + [Fact] + public static void ReadArrayOfIImmutableHashSetT() + { + ImmutableHashSet[] result = JsonSerializer.Parse[]>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + List expected = new List { 1, 2, 3, 4 }; + + foreach (ImmutableHashSet l in result) + { + foreach (int i in l) + { + expected.Remove(i); + } + } + + Assert.Equal(0, expected.Count); + } + + [Fact] + public static void ReadPrimitiveImmutableHashSetT() + { + ImmutableHashSet result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[1,2]")); + List expected = new List { 1, 2 }; + + foreach (int i in result) + { + expected.Remove(i); + } + + Assert.Equal(0, expected.Count); + + result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[]")); + Assert.Equal(0, result.Count()); + } + + [Fact] + public static void ReadImmutableListTOfImmutableListT() + { + ImmutableList> result = JsonSerializer.Parse>>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 1; + + foreach (ImmutableList l in result) + { + foreach (int i in l) + { + Assert.Equal(expected++, i); + } + } + } + + [Fact] + public static void ReadImmutableListTOfArray() + { + ImmutableList result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 1; + + foreach (int[] arr in result) + { + foreach (int i in arr) + { + Assert.Equal(expected++, i); + } + } + } + + [Fact] + public static void ReadArrayOfIImmutableListT() + { + ImmutableList[] result = JsonSerializer.Parse[]>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 1; + + foreach (ImmutableList l in result) + { + foreach (int i in l) + { + Assert.Equal(expected++, i); + } + } + } + + [Fact] + public static void ReadPrimitiveImmutableListT() + { + ImmutableList result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[1,2]")); + int expected = 1; + + foreach (int i in result) + { + Assert.Equal(expected++, i); + } + + result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[]")); + Assert.Equal(0, result.Count()); + } + + [Fact] + public static void ReadImmutableStackTOfImmutableStackT() + { + ImmutableStack> result = JsonSerializer.Parse>>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 4; + + foreach (ImmutableStack l in result) + { + foreach (int i in l) + { + Assert.Equal(expected--, i); + } + } + } + + [Fact] + public static void ReadImmutableStackTOfArray() + { + ImmutableStack result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 3; + + foreach (int[] arr in result) + { + foreach (int i in arr) + { + Assert.Equal(expected++, i); + } + + expected = 1; + } + } + + [Fact] + public static void ReadArrayOfIImmutableStackT() + { + ImmutableStack[] result = JsonSerializer.Parse[]>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 2; + + foreach (ImmutableStack l in result) + { + foreach (int i in l) + { + Assert.Equal(expected--, i); + } + + expected = 4; + } + } + + [Fact] + public static void ReadPrimitiveImmutableStackT() + { + ImmutableStack result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[1,2]")); + int expected = 2; + + foreach (int i in result) + { + Assert.Equal(expected--, i); + } + + result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[]")); + Assert.Equal(0, result.Count()); + } + + [Fact] + public static void ReadImmutableQueueTOfImmutableQueueT() + { + ImmutableQueue> result = JsonSerializer.Parse>>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 1; + + foreach (ImmutableQueue l in result) + { + foreach (int i in l) + { + Assert.Equal(expected++, i); + } + } + } + + [Fact] + public static void ReadImmutableQueueTOfArray() + { + ImmutableQueue result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 1; + + foreach (int[] arr in result) + { + foreach (int i in arr) + { + Assert.Equal(expected++, i); + } + } + } + + [Fact] + public static void ReadArrayOfIImmutableQueueT() + { + ImmutableQueue[] result = JsonSerializer.Parse[]>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 1; + + foreach (ImmutableQueue l in result) + { + foreach (int i in l) + { + Assert.Equal(expected++, i); + } + } + } + + [Fact] + public static void ReadPrimitiveImmutableQueueT() + { + ImmutableQueue result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[1,2]")); + int expected = 1; + + foreach (int i in result) + { + Assert.Equal(expected++, i); + } + + result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[]")); + Assert.Equal(0, result.Count()); + } + + [Fact] + public static void ReadArrayOfIImmutableSortedSetT() + { + ImmutableSortedSet[] result = JsonSerializer.Parse[]>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); + int expected = 1; + + foreach (ImmutableSortedSet l in result) + { + foreach (int i in l) + { + Assert.Equal(expected++, i); + } + } + } + + [Fact] + public static void ReadPrimitiveImmutableSortedSetT() + { + ImmutableSortedSet result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[1,2]")); + int expected = 1; + + foreach (int i in result) + { + Assert.Equal(expected++, i); + } + + result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[]")); + Assert.Equal(0, result.Count()); + } + } +} diff --git a/src/libraries/System.Text.Json/tests/Serialization/Value.ReadTests.cs b/src/libraries/System.Text.Json/tests/Serialization/Value.ReadTests.cs index 1fdd0b0..45c9a33 100644 --- a/src/libraries/System.Text.Json/tests/Serialization/Value.ReadTests.cs +++ b/src/libraries/System.Text.Json/tests/Serialization/Value.ReadTests.cs @@ -49,6 +49,9 @@ namespace System.Text.Json.Serialization.Tests int[] i = JsonSerializer.Parse(Encoding.UTF8.GetBytes(@"[1,2]")); Assert.Equal(1, i[0]); Assert.Equal(2, i[1]); + + i = JsonSerializer.Parse(Encoding.UTF8.GetBytes(@"[]")); + Assert.Equal(0, i.Length); } [Fact] @@ -274,332 +277,6 @@ namespace System.Text.Json.Serialization.Tests Assert.Equal(4, i[1][1]); } - [Fact] - public static void ReadListOfList() - { - List> result = JsonSerializer.Parse>>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); - - Assert.Equal(1, result[0][0]); - Assert.Equal(2, result[0][1]); - Assert.Equal(3, result[1][0]); - Assert.Equal(4, result[1][1]); - } - - [Fact] - public static void ReadListOfArray() - { - List result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); - - Assert.Equal(1, result[0][0]); - Assert.Equal(2, result[0][1]); - Assert.Equal(3, result[1][0]); - Assert.Equal(4, result[1][1]); - } - - [Fact] - public static void ReadArrayOfList() - { - List[] result = JsonSerializer.Parse[]> (Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); - - Assert.Equal(1, result[0][0]); - Assert.Equal(2, result[0][1]); - Assert.Equal(3, result[1][0]); - Assert.Equal(4, result[1][1]); - } - - [Fact] - public static void ReadPrimitiveList() - { - List i = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[1,2]")); - Assert.Equal(1, i[0]); - Assert.Equal(2, i[1]); - } - - [Fact] - public static void ReadIEnumerableTOfIEnumerableT() - { - IEnumerable> result = JsonSerializer.Parse>>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); - int expected = 1; - - foreach (IEnumerable ie in result) - { - foreach (int i in ie) - { - Assert.Equal(expected++, i); - } - } - } - - [Fact] - public static void ReadIEnumerableTOfArray() - { - IEnumerable result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); - int expected = 1; - - foreach (int[] arr in result) - { - foreach (int i in arr) - { - Assert.Equal(expected++, i); - } - } - } - - [Fact] - public static void ReadArrayOfIEnumerableT() - { - IEnumerable[] result = JsonSerializer.Parse[]> (Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); - int expected = 1; - - foreach (IEnumerable arr in result) - { - foreach (int i in arr) - { - Assert.Equal(expected++, i); - } - } - } - - [Fact] - public static void ReadPrimitiveIEnumerableT() - { - IEnumerable result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[1,2]")); - int expected = 1; - - foreach (int i in result) - { - Assert.Equal(expected++, i); - } - } - - [Fact] - public static void ReadIListTOfIListT() - { - IList> result = JsonSerializer.Parse>>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); - int expected = 1; - - foreach (IList ie in result) - { - foreach (int i in ie) - { - Assert.Equal(expected++, i); - } - } - } - - [Fact] - public static void ReadIListTOfArray() - { - IList result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); - int expected = 1; - - foreach (int[] arr in result) - { - foreach (int i in arr) - { - Assert.Equal(expected++, i); - } - } - } - - [Fact] - public static void ReadArrayOfIListT() - { - IList[] result = JsonSerializer.Parse[]>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); - int expected = 1; - - foreach (IList arr in result) - { - foreach (int i in arr) - { - Assert.Equal(expected++, i); - } - } - } - - [Fact] - public static void ReadPrimitiveIListT() - { - IList result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[1,2]")); - int expected = 1; - - foreach (int i in result) - { - Assert.Equal(expected++, i); - } - } - - [Fact] - public static void ReadICollectionTOfICollectionT() - { - ICollection> result = JsonSerializer.Parse>>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); - int expected = 1; - - foreach (ICollection ie in result) - { - foreach (int i in ie) - { - Assert.Equal(expected++, i); - } - } - } - - [Fact] - public static void ReadICollectionTOfArray() - { - ICollection result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); - int expected = 1; - - foreach (int[] arr in result) - { - foreach (int i in arr) - { - Assert.Equal(expected++, i); - } - } - } - - [Fact] - public static void ReadArrayOfICollectionT() - { - ICollection[] result = JsonSerializer.Parse[]>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); - int expected = 1; - - foreach (ICollection arr in result) - { - foreach (int i in arr) - { - Assert.Equal(expected++, i); - } - } - } - - [Fact] - public static void ReadPrimitiveICollectionT() - { - ICollection result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[1,2]")); - int expected = 1; - - foreach (int i in result) - { - Assert.Equal(expected++, i); - } - } - - [Fact] - public static void ReadIReadOnlyCollectionTOfIReadOnlyCollectionT() - { - IReadOnlyCollection> result = JsonSerializer.Parse>>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); - int expected = 1; - - foreach (IReadOnlyCollection ie in result) - { - foreach (int i in ie) - { - Assert.Equal(expected++, i); - } - } - } - - [Fact] - public static void ReadIReadOnlyCollectionTOfArray() - { - IReadOnlyCollection result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); - int expected = 1; - - foreach (int[] arr in result) - { - foreach (int i in arr) - { - Assert.Equal(expected++, i); - } - } - } - - [Fact] - public static void ReadArrayOfIReadOnlyCollectionT() - { - IReadOnlyCollection[] result = JsonSerializer.Parse[]>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); - int expected = 1; - - foreach (IReadOnlyCollection arr in result) - { - foreach (int i in arr) - { - Assert.Equal(expected++, i); - } - } - } - - [Fact] - public static void ReadPrimitiveIReadOnlyCollectionT() - { - IReadOnlyCollection result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[1,2]")); - int expected = 1; - - foreach (int i in result) - { - Assert.Equal(expected++, i); - } - } - - [Fact] - public static void ReadIReadOnlyListTOfIReadOnlyListT() - { - IReadOnlyList> result = JsonSerializer.Parse>>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); - int expected = 1; - - foreach (IReadOnlyList ie in result) - { - foreach (int i in ie) - { - Assert.Equal(expected++, i); - } - } - } - - [Fact] - public static void ReadIReadOnlyListTOfArray() - { - IReadOnlyList result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); - int expected = 1; - - foreach (int[] arr in result) - { - foreach (int i in arr) - { - Assert.Equal(expected++, i); - } - } - } - - [Fact] - public static void ReadArrayOfIReadOnlyListT() - { - IReadOnlyList[] result = JsonSerializer.Parse[]>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")); - int expected = 1; - - foreach (IReadOnlyList arr in result) - { - foreach (int i in arr) - { - Assert.Equal(expected++, i); - } - } - } - - [Fact] - public static void ReadPrimitiveIReadOnlyListT() - { - IReadOnlyList result = JsonSerializer.Parse>(Encoding.UTF8.GetBytes(@"[1,2]")); - int expected = 1; - - foreach (int i in result) - { - Assert.Equal(expected++, i); - } - } - public class TestClassWithBadData { public TestChildClassWithBadData[] Children { get; set; } diff --git a/src/libraries/System.Text.Json/tests/Serialization/Value.WriteTests.GenericCollections.cs b/src/libraries/System.Text.Json/tests/Serialization/Value.WriteTests.GenericCollections.cs new file mode 100644 index 0000000..8128e44 --- /dev/null +++ b/src/libraries/System.Text.Json/tests/Serialization/Value.WriteTests.GenericCollections.cs @@ -0,0 +1,492 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using Xunit; + +namespace System.Text.Json.Serialization.Tests +{ + public static partial class ValueTests + { + [Fact] + public static void WriteListOfList() + { + var input = new List> + { + new List() { 1, 2 }, + new List() { 3, 4 } + }; + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[1,2],[3,4]]", json); + } + + [Fact] + public static void WriteListOfArray() + { + var input = new List + { + new int[] { 1, 2 }, + new int[] { 3, 4 } + }; + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[1,2],[3,4]]", json); + } + + [Fact] + public static void WriteArrayOfList() + { + var input = new List[2]; + input[0] = new List() { 1, 2 }; + input[1] = new List() { 3, 4 }; + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[1,2],[3,4]]", json); + } + + [Fact] + public static void WritePrimitiveList() + { + var input = new List { 1, 2 }; + + string json = JsonSerializer.ToString(input); + Assert.Equal("[1,2]", json); + } + + [Fact] + public static void WriteIEnumerableTOfIEnumerableT() + { + IEnumerable> input = new List> + { + new List() { 1, 2 }, + new List() { 3, 4 } + }; + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[1,2],[3,4]]", json); + } + + [Fact] + public static void WriteIEnumerableTOfArray() + { + IEnumerable input = new List + { + new int[] { 1, 2 }, + new int[] { 3, 4 } + }; + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[1,2],[3,4]]", json); + } + + [Fact] + public static void WriteArrayOfIEnumerableT() + { + IEnumerable[] input = new List[2]; + input[0] = new List() { 1, 2 }; + input[1] = new List() { 3, 4 }; + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[1,2],[3,4]]", json); + } + + [Fact] + public static void WritePrimitiveIEnumerableT() + { + IEnumerable input = new List { 1, 2 }; + + string json = JsonSerializer.ToString(input); + Assert.Equal("[1,2]", json); + } + + [Fact] + public static void WriteIListTOfIListT() + { + IList> input = new List> + { + new List() { 1, 2 }, + new List() { 3, 4 } + }; + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[1,2],[3,4]]", json); + } + + [Fact] + public static void WriteIListTOfArray() + { + IList input = new List + { + new int[] { 1, 2 }, + new int[] { 3, 4 } + }; + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[1,2],[3,4]]", json); + } + + [Fact] + public static void WriteArrayOfIListT() + { + IList[] input = new List[2]; + input[0] = new List() { 1, 2 }; + input[1] = new List() { 3, 4 }; + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[1,2],[3,4]]", json); + } + + [Fact] + public static void WritePrimitiveIListT() + { + IList input = new List { 1, 2 }; + + string json = JsonSerializer.ToString(input); + Assert.Equal("[1,2]", json); + } + + [Fact] + public static void WriteICollectionTOfICollectionT() + { + ICollection> input = new List> + { + new List() { 1, 2 }, + new List() { 3, 4 } + }; + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[1,2],[3,4]]", json); + } + + [Fact] + public static void WriteICollectionTOfArray() + { + ICollection input = new List + { + new int[] { 1, 2 }, + new int[] { 3, 4 } + }; + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[1,2],[3,4]]", json); + } + + [Fact] + public static void WriteArrayOfICollectionT() + { + ICollection[] input = new List[2]; + input[0] = new List() { 1, 2 }; + input[1] = new List() { 3, 4 }; + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[1,2],[3,4]]", json); + } + + [Fact] + public static void WritePrimitiveICollectionT() + { + ICollection input = new List { 1, 2 }; + + string json = JsonSerializer.ToString(input); + Assert.Equal("[1,2]", json); + } + + [Fact] + public static void WriteIReadOnlyCollectionTOfIReadOnlyCollectionT() + { + IReadOnlyCollection> input = new List> + { + new List() { 1, 2 }, + new List() { 3, 4 } + }; + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[1,2],[3,4]]", json); + } + + [Fact] + public static void WriteIReadOnlyCollectionTOfArray() + { + IReadOnlyCollection input = new List + { + new int[] { 1, 2 }, + new int[] { 3, 4 } + }; + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[1,2],[3,4]]", json); + } + + [Fact] + public static void WriteArrayOfIReadOnlyCollectionT() + { + IReadOnlyCollection[] input = new List[2]; + input[0] = new List() { 1, 2 }; + input[1] = new List() { 3, 4 }; + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[1,2],[3,4]]", json); + } + + [Fact] + public static void WritePrimitiveIReadOnlyCollectionT() + { + IReadOnlyCollection input = new List { 1, 2 }; + + string json = JsonSerializer.ToString(input); + Assert.Equal("[1,2]", json); + } + + [Fact] + public static void WriteIReadOnlyListTOfIReadOnlyListT() + { + IReadOnlyList> input = new List> + { + new List() { 1, 2 }, + new List() { 3, 4 } + }; + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[1,2],[3,4]]", json); + } + + [Fact] + public static void WriteIReadOnlyListTOfArray() + { + IReadOnlyList input = new List + { + new int[] { 1, 2 }, + new int[] { 3, 4 } + }; + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[1,2],[3,4]]", json); + } + + [Fact] + public static void WriteArrayOfIReadOnlyListT() + { + IReadOnlyList[] input = new List[2]; + input[0] = new List() { 1, 2 }; + input[1] = new List() { 3, 4 }; + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[1,2],[3,4]]", json); + } + + [Fact] + public static void WritePrimitiveIReadOnlyListT() + { + IReadOnlyList input = new List { 1, 2 }; + + string json = JsonSerializer.ToString(input); + Assert.Equal("[1,2]", json); + } + + [Fact] + public static void WriteStackTOfStackT() + { + Stack> input = new Stack>(new List> + { + new Stack(new List() { 1, 2 }), + new Stack(new List() { 3, 4 }) + }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[4,3],[2,1]]", json); + } + + [Fact] + public static void WriteStackTOfArray() + { + Stack input = new Stack(new List + { + new int[] { 1, 2 }, + new int[] { 3, 4 } + }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[3,4],[1,2]]", json); + } + + [Fact] + public static void WriteArrayOfStackT() + { + Stack[] input = new Stack[2]; + input[0] = new Stack(new List { 1, 2 }); + input[1] = new Stack(new List { 3, 4 }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[2,1],[4,3]]", json); + } + + [Fact] + public static void WritePrimitiveStackT() + { + Stack input = new Stack(new List { 1, 2 }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[2,1]", json); + } + + [Fact] + public static void WriteQueueTOfQueueT() + { + Queue> input = new Queue>(new List> + { + new Queue(new List() { 1, 2 }), + new Queue(new List() { 3, 4 }) + }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[1,2],[3,4]]", json); + } + + [Fact] + public static void WriteQueueTOfArray() + { + Queue input = new Queue(new List + { + new int[] { 1, 2 }, + new int[] { 3, 4 } + }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[1,2],[3,4]]", json); + } + + [Fact] + public static void WriteArrayOfQueueT() + { + Queue[] input = new Queue[2]; + input[0] = new Queue(new List { 1, 2 }); + input[1] = new Queue(new List { 3, 4 }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[1,2],[3,4]]", json); + } + + [Fact] + public static void WritePrimitiveQueueT() + { + Queue input = new Queue(new List { 1, 2 }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[1,2]", json); + } + + [Fact] + public static void WriteHashSetTOfHashSetT() + { + HashSet> input = new HashSet>(new List> + { + new HashSet(new List() { 1, 2 }), + new HashSet(new List() { 3, 4 }) + }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[1,2],[3,4]]", json); + } + + [Fact] + public static void WriteHashSetTOfArray() + { + HashSet input = new HashSet(new List + { + new int[] { 1, 2 }, + new int[] { 3, 4 } + }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[1,2],[3,4]]", json); + } + + [Fact] + public static void WriteArrayOfHashSetT() + { + HashSet[] input = new HashSet[2]; + input[0] = new HashSet(new List { 1, 2 }); + input[1] = new HashSet(new List { 3, 4 }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[1,2],[3,4]]", json); + } + + [Fact] + public static void WritePrimitiveHashSetT() + { + HashSet input = new HashSet(new List { 1, 2 }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[1,2]", json); + } + + [Fact] + public static void WriteLinkedListTOfLinkedListT() + { + LinkedList> input = new LinkedList>(new List> + { + new LinkedList(new List() { 1, 2 }), + new LinkedList(new List() { 3, 4 }) + }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[1,2],[3,4]]", json); + } + + [Fact] + public static void WriteLinkedListTOfArray() + { + LinkedList input = new LinkedList(new List + { + new int[] { 1, 2 }, + new int[] { 3, 4 } + }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[1,2],[3,4]]", json); + } + + [Fact] + public static void WriteArrayOfLinkedListT() + { + LinkedList[] input = new LinkedList[2]; + input[0] = new LinkedList(new List { 1, 2 }); + input[1] = new LinkedList(new List { 3, 4 }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[1,2],[3,4]]", json); + } + + [Fact] + public static void WritePrimitiveLinkedListT() + { + LinkedList input = new LinkedList(new List { 1, 2 }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[1,2]", json); + } + + [Fact] + public static void WriteArrayOfSortedSetT() + { + SortedSet[] input = new SortedSet[2]; + input[0] = new SortedSet(new List { 1, 2 }); + input[1] = new SortedSet(new List { 3, 4 }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[1,2],[3,4]]", json); + } + + [Fact] + public static void WritePrimitiveSortedSetT() + { + SortedSet input = new SortedSet(new List { 1, 2 }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[1,2]", json); + } + } +} diff --git a/src/libraries/System.Text.Json/tests/Serialization/Value.WriteTests.ImmutableCollections.cs b/src/libraries/System.Text.Json/tests/Serialization/Value.WriteTests.ImmutableCollections.cs new file mode 100644 index 0000000..9ecbd84 --- /dev/null +++ b/src/libraries/System.Text.Json/tests/Serialization/Value.WriteTests.ImmutableCollections.cs @@ -0,0 +1,397 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using System.Collections.Immutable; +using Xunit; + +namespace System.Text.Json.Serialization.Tests +{ + public static partial class ValueTests + { + [Fact] + public static void WriteIImmutableListTOfIImmutableListT() + { + IImmutableList> input = ImmutableList.CreateRange(new List>{ + ImmutableList.CreateRange(new List() { 1, 2 }), + ImmutableList.CreateRange(new List() { 3, 4 }) + }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[1,2],[3,4]]", json); + } + + [Fact] + public static void WriteIImmutableListTOfArray() + { + IImmutableList input = ImmutableList.CreateRange(new List + { + new int[] { 1, 2 }, + new int[] { 3, 4 } + }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[1,2],[3,4]]", json); + } + + [Fact] + public static void WriteArrayOfIImmutableListT() + { + IImmutableList[] input = new IImmutableList[2]; + input[0] = ImmutableList.CreateRange(new List() { 1, 2 }); + input[1] = ImmutableList.CreateRange(new List() { 3, 4 }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[1,2],[3,4]]", json); + } + + [Fact] + public static void WritePrimitiveIImmutableListT() + { + IImmutableList input = ImmutableList.CreateRange(new List { 1, 2 }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[1,2]", json); + } + + [Fact] + public static void WriteIImmutableStackTOfIImmutableStackT() + { + IImmutableStack> input = ImmutableStack.CreateRange(new List>{ + ImmutableStack.CreateRange(new List() { 1, 2 }), + ImmutableStack.CreateRange(new List() { 3, 4 }) + }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[4,3],[2,1]]", json); + } + + [Fact] + public static void WriteIImmutableStackTOfArray() + { + IImmutableStack input = ImmutableStack.CreateRange(new List + { + new int[] { 1, 2 }, + new int[] { 3, 4 } + }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[3,4],[1,2]]", json); + } + + [Fact] + public static void WriteArrayOfIImmutableStackT() + { + IImmutableStack[] input = new IImmutableStack[2]; + input[0] = ImmutableStack.CreateRange(new List() { 1, 2 }); + input[1] = ImmutableStack.CreateRange(new List() { 3, 4 }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[2,1],[4,3]]", json); + } + + [Fact] + public static void WritePrimitiveIImmutableStackT() + { + IImmutableStack input = ImmutableStack.CreateRange(new List { 1, 2 }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[2,1]", json); + } + + [Fact] + public static void WriteIImmutableQueueTOfIImmutableQueueT() + { + IImmutableQueue> input = ImmutableQueue.CreateRange(new List>{ + ImmutableQueue.CreateRange(new List() { 1, 2 }), + ImmutableQueue.CreateRange(new List() { 3, 4 }) + }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[1,2],[3,4]]", json); + } + + [Fact] + public static void WriteIImmutableQueueTOfArray() + { + IImmutableQueue input = ImmutableQueue.CreateRange(new List + { + new int[] { 1, 2 }, + new int[] { 3, 4 } + }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[1,2],[3,4]]", json); + } + + [Fact] + public static void WriteArrayOfIImmutableQueueT() + { + IImmutableQueue[] input = new IImmutableQueue[2]; + input[0] = ImmutableQueue.CreateRange(new List() { 1, 2 }); + input[1] = ImmutableQueue.CreateRange(new List() { 3, 4 }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[1,2],[3,4]]", json); + } + + [Fact] + public static void WritePrimitiveIImmutableQueueT() + { + IImmutableQueue input = ImmutableQueue.CreateRange(new List { 1, 2 }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[1,2]", json); + } + + [Fact] + public static void WriteIImmutableSetTOfIImmutableSetT() + { + IImmutableSet> input = ImmutableHashSet.CreateRange(new List>{ + ImmutableHashSet.CreateRange(new List() { 1, 2 }), + ImmutableHashSet.CreateRange(new List() { 3, 4 }) + }); + + string json = JsonSerializer.ToString(input); + Assert.True(json.Contains("[1,2]")); + Assert.True(json.Contains("[3,4]")); + } + + [Fact] + public static void WriteIImmutableSetTOfArray() + { + IImmutableSet input = ImmutableHashSet.CreateRange(new List + { + new int[] { 1, 2 }, + new int[] { 3, 4 } + }); + + string json = JsonSerializer.ToString(input); + Assert.True(json.Contains("[1,2]")); + Assert.True(json.Contains("[3,4]")); + } + + [Fact] + public static void WriteArrayOfIImmutableSetT() + { + IImmutableSet[] input = new IImmutableSet[2]; + input[0] = ImmutableHashSet.CreateRange(new List() { 1, 2 }); + input[1] = ImmutableHashSet.CreateRange(new List() { 3, 4 }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[1,2],[3,4]]", json); + } + + [Fact] + public static void WritePrimitiveIImmutableSetT() + { + IImmutableSet input = ImmutableHashSet.CreateRange(new List { 1, 2 }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[1,2]", json); + } + + [Fact] + public static void WriteImmutableHashSetTOfImmutableHashSetT() + { + ImmutableHashSet> input = ImmutableHashSet.CreateRange(new List>{ + ImmutableHashSet.CreateRange(new List() { 1, 2 }), + ImmutableHashSet.CreateRange(new List() { 3, 4 }) + }); + + string json = JsonSerializer.ToString(input); + Assert.True(json.Contains("[1,2]")); + Assert.True(json.Contains("[3,4]")); + } + + [Fact] + public static void WriteImmutableHashSetTOfArray() + { + ImmutableHashSet input = ImmutableHashSet.CreateRange(new List + { + new int[] { 1, 2 }, + new int[] { 3, 4 } + }); + + string json = JsonSerializer.ToString(input); + Assert.True(json.Contains("[1,2]")); + Assert.True(json.Contains("[3,4]")); + } + + [Fact] + public static void WriteArrayOfImmutableHashSetT() + { + ImmutableHashSet[] input = new ImmutableHashSet[2]; + input[0] = ImmutableHashSet.CreateRange(new List() { 1, 2 }); + input[1] = ImmutableHashSet.CreateRange(new List() { 3, 4 }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[1,2],[3,4]]", json); + } + + [Fact] + public static void WritePrimitiveImmutableHashSetT() + { + ImmutableHashSet input = ImmutableHashSet.CreateRange(new List { 1, 2 }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[1,2]", json); + } + + [Fact] + public static void WriteImmutableListTOfImmutableListT() + { + ImmutableList> input = ImmutableList.CreateRange(new List>{ + ImmutableList.CreateRange(new List() { 1, 2 }), + ImmutableList.CreateRange(new List() { 3, 4 }) + }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[1,2],[3,4]]", json); + } + + [Fact] + public static void WriteImmutableListTOfArray() + { + ImmutableList input = ImmutableList.CreateRange(new List + { + new int[] { 1, 2 }, + new int[] { 3, 4 } + }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[1,2],[3,4]]", json); + } + + [Fact] + public static void WriteArrayOfImmutableListT() + { + ImmutableList[] input = new ImmutableList[2]; + input[0] = ImmutableList.CreateRange(new List() { 1, 2 }); + input[1] = ImmutableList.CreateRange(new List() { 3, 4 }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[1,2],[3,4]]", json); + } + + [Fact] + public static void WritePrimitiveImmutableListT() + { + ImmutableList input = ImmutableList.CreateRange(new List { 1, 2 }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[1,2]", json); + } + + [Fact] + public static void WriteImmutableStackTOfImmutableStackT() + { + ImmutableStack> input = ImmutableStack.CreateRange(new List>{ + ImmutableStack.CreateRange(new List() { 1, 2 }), + ImmutableStack.CreateRange(new List() { 3, 4 }) + }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[4,3],[2,1]]", json); + } + + [Fact] + public static void WriteImmutableStackTOfArray() + { + ImmutableStack input = ImmutableStack.CreateRange(new List + { + new int[] { 1, 2 }, + new int[] { 3, 4 } + }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[3,4],[1,2]]", json); + } + + [Fact] + public static void WriteArrayOfImmutableStackT() + { + ImmutableStack[] input = new ImmutableStack[2]; + input[0] = ImmutableStack.CreateRange(new List() { 1, 2 }); + input[1] = ImmutableStack.CreateRange(new List() { 3, 4 }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[2,1],[4,3]]", json); + } + + [Fact] + public static void WritePrimitiveImmutableStackT() + { + ImmutableStack input = ImmutableStack.CreateRange(new List { 1, 2 }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[2,1]", json); + } + + [Fact] + public static void WriteImmutableQueueTOfImmutableQueueT() + { + ImmutableQueue> input = ImmutableQueue.CreateRange(new List>{ + ImmutableQueue.CreateRange(new List() { 1, 2 }), + ImmutableQueue.CreateRange(new List() { 3, 4 }) + }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[1,2],[3,4]]", json); + } + + [Fact] + public static void WriteImmutableQueueTOfArray() + { + ImmutableQueue input = ImmutableQueue.CreateRange(new List + { + new int[] { 1, 2 }, + new int[] { 3, 4 } + }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[1,2],[3,4]]", json); + } + + [Fact] + public static void WriteArrayOfImmutableQueueT() + { + ImmutableQueue[] input = new ImmutableQueue[2]; + input[0] = ImmutableQueue.CreateRange(new List() { 1, 2 }); + input[1] = ImmutableQueue.CreateRange(new List() { 3, 4 }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[1,2],[3,4]]", json); + } + + [Fact] + public static void WritePrimitiveImmutableQueueT() + { + ImmutableQueue input = ImmutableQueue.CreateRange(new List { 1, 2 }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[1,2]", json); + } + + [Fact] + public static void WriteArrayOfImmutableSortedSetT() + { + ImmutableSortedSet[] input = new ImmutableSortedSet[2]; + input[0] = ImmutableSortedSet.CreateRange(new List() { 1, 2 }); + input[1] = ImmutableSortedSet.CreateRange(new List() { 3, 4 }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[[1,2],[3,4]]", json); + } + + [Fact] + public static void WritePrimitiveImmutableSortedSetT() + { + ImmutableSortedSet input = ImmutableSortedSet.CreateRange(new List { 1, 2 }); + + string json = JsonSerializer.ToString(input); + Assert.Equal("[1,2]", json); + } + } +} diff --git a/src/libraries/System.Text.Json/tests/Serialization/Value.WriteTests.cs b/src/libraries/System.Text.Json/tests/Serialization/Value.WriteTests.cs index 8a6c960..17b845b 100644 --- a/src/libraries/System.Text.Json/tests/Serialization/Value.WriteTests.cs +++ b/src/libraries/System.Text.Json/tests/Serialization/Value.WriteTests.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Collections.Generic; +using System.Collections.Immutable; using Xunit; namespace System.Text.Json.Serialization.Tests @@ -114,281 +115,5 @@ namespace System.Text.Json.Serialization.Tests string json = JsonSerializer.ToString(input); Assert.Equal("[[1,2],[3,4]]", json); } - - [Fact] - public static void WriteListOfList() - { - var input = new List> - { - new List() { 1, 2 }, - new List() { 3, 4 } - }; - - string json = JsonSerializer.ToString(input); - Assert.Equal("[[1,2],[3,4]]", json); - } - - [Fact] - public static void WriteListOfArray() - { - var input = new List - { - new int[] { 1, 2 }, - new int[] { 3, 4 } - }; - - string json = JsonSerializer.ToString(input); - Assert.Equal("[[1,2],[3,4]]", json); - } - - [Fact] - public static void WriteArrayOfList() - { - var input = new List[2]; - input[0] = new List() { 1, 2 }; - input[1] = new List() { 3, 4 }; - - string json = JsonSerializer.ToString(input); - Assert.Equal("[[1,2],[3,4]]", json); - } - - [Fact] - public static void WritePrimitiveList() - { - var input = new List { 1, 2 }; - - string json = JsonSerializer.ToString(input); - Assert.Equal("[1,2]", json); - } - - [Fact] - public static void WriteIEnumerableTOfIEnumerableT() - { - IEnumerable> input = new List> - { - new List() { 1, 2 }, - new List() { 3, 4 } - }; - - string json = JsonSerializer.ToString(input); - Assert.Equal("[[1,2],[3,4]]", json); - } - - [Fact] - public static void WriteIEnumerableTOfArray() - { - IEnumerable input = new List - { - new int[] { 1, 2 }, - new int[] { 3, 4 } - }; - - string json = JsonSerializer.ToString(input); - Assert.Equal("[[1,2],[3,4]]", json); - } - - [Fact] - public static void WriteArrayOfIEnumerableT() - { - IEnumerable[] input = new List[2]; - input[0] = new List() { 1, 2 }; - input[1] = new List() { 3, 4 }; - - string json = JsonSerializer.ToString(input); - Assert.Equal("[[1,2],[3,4]]", json); - } - - [Fact] - public static void WritePrimitiveIEnumerableT() - { - IEnumerable input = new List { 1, 2 }; - - string json = JsonSerializer.ToString(input); - Assert.Equal("[1,2]", json); - } - - [Fact] - public static void WriteIListTOfIListT() - { - IList> input = new List> - { - new List() { 1, 2 }, - new List() { 3, 4 } - }; - - string json = JsonSerializer.ToString(input); - Assert.Equal("[[1,2],[3,4]]", json); - } - - [Fact] - public static void WriteIListTOfArray() - { - IList input = new List - { - new int[] { 1, 2 }, - new int[] { 3, 4 } - }; - - string json = JsonSerializer.ToString(input); - Assert.Equal("[[1,2],[3,4]]", json); - } - - [Fact] - public static void WriteArrayOfIListT() - { - IList[] input = new List[2]; - input[0] = new List() { 1, 2 }; - input[1] = new List() { 3, 4 }; - - string json = JsonSerializer.ToString(input); - Assert.Equal("[[1,2],[3,4]]", json); - } - - [Fact] - public static void WritePrimitiveIListT() - { - IList input = new List { 1, 2 }; - - string json = JsonSerializer.ToString(input); - Assert.Equal("[1,2]", json); - } - - [Fact] - public static void WriteICollectionTOfICollectionT() - { - ICollection> input = new List> - { - new List() { 1, 2 }, - new List() { 3, 4 } - }; - - string json = JsonSerializer.ToString(input); - Assert.Equal("[[1,2],[3,4]]", json); - } - - [Fact] - public static void WriteICollectionTOfArray() - { - ICollection input = new List - { - new int[] { 1, 2 }, - new int[] { 3, 4 } - }; - - string json = JsonSerializer.ToString(input); - Assert.Equal("[[1,2],[3,4]]", json); - } - - [Fact] - public static void WriteArrayOfICollectionT() - { - ICollection[] input = new List[2]; - input[0] = new List() { 1, 2 }; - input[1] = new List() { 3, 4 }; - - string json = JsonSerializer.ToString(input); - Assert.Equal("[[1,2],[3,4]]", json); - } - - [Fact] - public static void WritePrimitiveICollectionT() - { - ICollection input = new List { 1, 2 }; - - string json = JsonSerializer.ToString(input); - Assert.Equal("[1,2]", json); - } - - [Fact] - public static void WriteIReadOnlyCollectionTOfIReadOnlyCollectionT() - { - IReadOnlyCollection> input = new List> - { - new List() { 1, 2 }, - new List() { 3, 4 } - }; - - string json = JsonSerializer.ToString(input); - Assert.Equal("[[1,2],[3,4]]", json); - } - - [Fact] - public static void WriteIReadOnlyCollectionTOfArray() - { - IReadOnlyCollection input = new List - { - new int[] { 1, 2 }, - new int[] { 3, 4 } - }; - - string json = JsonSerializer.ToString(input); - Assert.Equal("[[1,2],[3,4]]", json); - } - - [Fact] - public static void WriteArrayOfIReadOnlyCollectionT() - { - IReadOnlyCollection[] input = new List[2]; - input[0] = new List() { 1, 2 }; - input[1] = new List() { 3, 4 }; - - string json = JsonSerializer.ToString(input); - Assert.Equal("[[1,2],[3,4]]", json); - } - - [Fact] - public static void WritePrimitiveIReadOnlyCollectionT() - { - IReadOnlyCollection input = new List { 1, 2 }; - - string json = JsonSerializer.ToString(input); - Assert.Equal("[1,2]", json); - } - - [Fact] - public static void WriteIReadOnlyListTOfIReadOnlyListT() - { - IReadOnlyList> input = new List> - { - new List() { 1, 2 }, - new List() { 3, 4 } - }; - - string json = JsonSerializer.ToString(input); - Assert.Equal("[[1,2],[3,4]]", json); - } - - [Fact] - public static void WriteIReadOnlyListTOfArray() - { - IReadOnlyList input = new List - { - new int[] { 1, 2 }, - new int[] { 3, 4 } - }; - - string json = JsonSerializer.ToString(input); - Assert.Equal("[[1,2],[3,4]]", json); - } - - [Fact] - public static void WriteArrayOfIReadOnlyListT() - { - IReadOnlyList[] input = new List[2]; - input[0] = new List() { 1, 2 }; - input[1] = new List() { 3, 4 }; - - string json = JsonSerializer.ToString(input); - Assert.Equal("[[1,2],[3,4]]", json); - } - - [Fact] - public static void WritePrimitiveIReadOnlyListT() - { - IReadOnlyList input = new List { 1, 2 }; - - string json = JsonSerializer.ToString(input); - Assert.Equal("[1,2]", json); - } } } diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests.csproj b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests.csproj index 1183e02..fb067fa 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests.csproj +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests.csproj @@ -51,7 +51,11 @@ + + + +