public void Should_serialize_dynamic_object_collection(Type type, IEnumerable value)
        {
            SkipUnsupportedDataType(type, value);

            var dynamicObjects = new DynamicObjectMapper(new DynamicObjectMapperSettings {
                WrapNullAsDynamicObject = true
            }).MapCollection(value);

            var config = CreateModelFor(type);
            var copy   = dynamicObjects.Clone(config);

            var dynamicObjectsCount = dynamicObjects?.Count() ?? 0;
            var copyCount           = copy?.Count() ?? 0;

            copyCount.ShouldBe(dynamicObjectsCount);

            for (int i = 0; i < copyCount; i++)
            {
                var originalValue = dynamicObjects.ElementAt(i)?.Get();
                var copyValue     = copy.ElementAt(i)?.Get();
                copyValue.ShouldBe(originalValue);
            }

            var c = new DynamicObjectMapper().Map(copy);

            if (value is null)
            {
                c.ShouldBeNull();
            }
            else
            {
                var array     = value.Cast <object>().ToArray();
                var copyArray = c.Cast <object>().ToArray();

                for (int i = 0; i < copyArray.Length; i++)
                {
                    copyArray[i].ShouldBe(array[i]);
                }
            }
        }
コード例 #2
0
        public void Should_serialize_dynamic_object_collection(Type type, IEnumerable value, CultureInfo culture)
        {
            SkipUnsupportedDataType(type, value);

            using var cultureContext = culture.CreateContext();

            var dynamicObjects = new DynamicObjectMapper().MapCollection(value);
            var copy           = dynamicObjects.Serialize();

            var dynamicObjectsCount = dynamicObjects?.Count() ?? 0;
            var copyCount           = copy?.Count() ?? 0;

            copyCount.ShouldBe(dynamicObjectsCount);

            for (int i = 0; i < copyCount; i++)
            {
                var originalValue = dynamicObjects.ElementAt(i)?.Get();
                var copyValue     = copy.ElementAt(i)?.Get();
                copyValue.ShouldBe(originalValue);
            }

            var c = new DynamicObjectMapper().Map(copy);

            if (value is null)
            {
                c.ShouldBeNull();
            }
            else
            {
                var array     = value.Cast <object>().ToArray();
                var copyArray = c.Cast <object>().ToArray();

                for (int i = 0; i < copyArray.Length; i++)
                {
                    copyArray[i].ShouldBe(array[i]);
                }
            }
        }