Beispiel #1
0
        public void LongString()
        {
            var input  = @"
                                                                               1
                                                                               2
                                                                               3
                                                                               4
                                                                               5
                                                                               6
                                                                               7
                                                                               8
                                                                               9
                                                                              10
                                                                              11
                                                                              12
                                                                              13
                                                                              14
                                                                              15
                                                                              16
                                                                              17
                                                                              18
                                                                              19";
            var script = CSScriptSerializer.Serialize(input);
            var output = CSScriptSerializer.Deserialize <string>(script);

            Assert.Equal(input, output);
        }
Beispiel #2
0
        public void NousableProperties()
        {
            var input = new Private();

            Assert.Equal($"The type {typeof(Private)} does not have public writable properties",
                         Assert.Throws <InvalidOperationException>(() => CSScriptSerializer.Serialize(input)).Message);
        }
Beispiel #3
0
        public void SelfReferencingType()
        {
            var input  = new Recursive();
            var script = CSScriptSerializer.Serialize(input);
            var output = CSScriptSerializer.Deserialize <Recursive>(script);

            Assert.Equal(input.Self, output.Self);
        }
Beispiel #4
0
        private void ValidateStringLiteral(string input, bool verbatim)
        {
            var script = CSScriptSerializer.Serialize(input);

            Assert.Equal(verbatim ? '@' : '"', script[0]);

            var output = CSScriptSerializer.Deserialize <string>(script);

            Assert.Equal(input, output);
        }
Beispiel #5
0
        public void CombinedFlagsEnum()
        {
            var input  = FlagsEnum.FirstFlag | FlagsEnum.SecondAndThird;
            var script = CSScriptSerializer.Serialize(input);
            var output = CSScriptSerializer.Deserialize <FlagsEnum>(script);

            Assert.Equal(input, output);
            Assert.Equal(typeof(RoundtrippingTest).Name + "." + typeof(FlagsEnum).Name + "." + FlagsEnum.FirstFlag + " | "
                         + typeof(RoundtrippingTest).Name + "." + typeof(FlagsEnum).Name + "." + FlagsEnum.SecondAndThird, script);
        }
Beispiel #6
0
        public void SimpleStruct()
        {
            var input = new Point {
                X = 1, Y = 1
            };
            var script = CSScriptSerializer.Serialize(input);
            var output = CSScriptSerializer.Deserialize <Point>(script);

            Assert.Equal(input.X, output.X);
            Assert.Equal(input.Y, output.Y);
        }
Beispiel #7
0
        public void OverridenProperties()
        {
            var input = new OverrideDerived {
                Property = "1"
            };

            var script = CSScriptSerializer.Serialize(input);
            var output = CSScriptSerializer.Deserialize <OverrideDerived>(script);

            Assert.Equal(input.Property, output.Property);
            Assert.Equal(input.GetSetCount(), output.GetSetCount());
        }
Beispiel #8
0
        public void NestedRecursiveGenerics()
        {
            var input = new Nested1 <string> .Nested3 {
                Parent = new Nested1 <string> {
                    BaseProp = "base"
                }
            };

            var script = CSScriptSerializer.Serialize(input);
            var output = CSScriptSerializer.Deserialize <Nested1 <string> .Nested3>(script);

            Assert.Equal(input.Parent.BaseProp, output.Parent.BaseProp);
        }
Beispiel #9
0
        public void NestedGenerics()
        {
            var input = new Nested1 <string> .Nested2 <int, bool> {
                Prop = Tuple.Create("1", 1, false)
            };

            var script = CSScriptSerializer.Serialize(input);
            var output = CSScriptSerializer.Deserialize <Nested1 <string> .Nested2 <int, bool> >(script);

            Assert.Equal(input.Prop.Item1, output.Prop.Item1);
            Assert.Equal(input.Prop.Item2, output.Prop.Item2);
            Assert.Equal(input.Prop.Item3, output.Prop.Item3);
        }
Beispiel #10
0
        public void HiddenPropertiesWithCustomValuesAndDefaults()
        {
            ICSScriptSerializer _;

            CSScriptSerializer.Serializers.TryRemove(typeof(HiddenDerived), out _);
            CSScriptSerializer.Serializers[typeof(HiddenDerived)] =
                new PropertyCSScriptSerializer <HiddenDerived>(
                    new Dictionary <string, Func <HiddenDerived, object, bool> >
            {
                { "Property", (o, v) => (int)v != 2 }
            },
                    null,
                    new Dictionary <string, Func <HiddenDerived, object> >
            {
                { "Property", o => o.Property + 1 }
            },
                    new Dictionary <string, Func <HiddenDerived, object, bool> >
            {
                { "HiddenBase.Property", (o, v) => (string)v != "11" }
            },
                    new Dictionary <string, Func <HiddenDerived, object> >
            {
                { "HiddenBase.Property", o => ((HiddenBase)o).Property + "1" }
            });

            var input = new HiddenDerived {
                Property = 1
            };

            ((HiddenBase)input).Property = "1";

            var script = CSScriptSerializer.Serialize(input);
            var output = CSScriptSerializer.Deserialize <HiddenDerived>(script);

            Assert.Equal(0, output.Property);
            Assert.Null(((HiddenBase)output).Property);

            input = new HiddenDerived {
                Property = 2
            };
            ((HiddenBase)input).Property = "2";

            script = CSScriptSerializer.Serialize(input);
            output = CSScriptSerializer.Deserialize <HiddenDerived>(script);

            Assert.Equal(3, output.Property);
            Assert.Equal("21", ((HiddenBase)output).Property);

            CSScriptSerializer.Serializers.TryRemove(typeof(HiddenDerived), out _);
        }
Beispiel #11
0
        public void HiddenProperties()
        {
            var input = new HiddenDerived {
                Property = 2
            };

            ((HiddenBase)input).Property = "1";

            var script = CSScriptSerializer.Serialize(input);
            var output = CSScriptSerializer.Deserialize <HiddenDerived>(script);

            Assert.Equal(input.Property, output.Property);
            Assert.Equal(((HiddenBase)input).Property, ((HiddenBase)output).Property);
        }
Beispiel #12
0
        private static void Serialize <T>(CSScriptLoaderBase <T> loader, Func <T, T> transform = null)
            where T : class, ILoadable
        {
            Console.WriteLine("Serializing " + typeof(T).Name + " instances...");

            var directory = Path.Combine(AppContext.BaseDirectory, "New", loader.RelativePath);

            Directory.CreateDirectory(directory);
            foreach (var item in loader.GetAll())
            {
                try
                {
                    var itemToSerialize = transform != null?transform(item) : item;

                    string script = null;
                    if (SerializeToScript)
                    {
                        script = CSScriptSerializer.Serialize(itemToSerialize);
                        File.WriteAllText(
                            Path.Combine(
                                loader.RelativePath,
                                CSScriptLoaderHelpers.GetScriptFilename(itemToSerialize.Name)),
                            script);
                    }
                    else
                    {
                        var code = CSClassSerializer.Serialize(
                            itemToSerialize, itemToSerialize.Name, loader.DataType.Namespace, loader.DataType.Name);
                        File.WriteAllText(
                            Path.Combine(directory, CSScriptLoaderHelpers.GetClassFilename(itemToSerialize.Name)),
                            code);
                    }

                    Verify(script, (dynamic)item);
                }
                catch (Exception e)
                {
                    throw new Exception("Failed to serialize " + item.Name, e);
                }
            }
        }
Beispiel #13
0
        public void KnownTypes()
        {
            var dateTime       = new DateTime(2000, 1, 1, 0, 0, 0);
            var dateTimeOffset = new DateTimeOffset(new DateTime(), TimeSpan.FromHours(-8.0));
            var timeSpan       = new TimeSpan(0, 10, 9, 8, 7);
            var guid           = Guid.NewGuid();
            var input          = new AllSupportedTypes
            {
                Int16                  = -1234,
                Int32                  = -123456789,
                Int64                  = -1234567890123456789L,
                Double                 = -1.23456789,
                Decimal                = -1234567890.01M,
                DateTime               = new DateTime(dateTime.Ticks, dateTime.Kind),
                DateTimeOffset         = new DateTimeOffset(dateTimeOffset.DateTime, dateTimeOffset.Offset),
                TimeSpan               = new TimeSpan(timeSpan.Ticks),
                String                 = "value",
                Single                 = -1.234F,
                Boolean                = true,
                Byte                   = 255,
                UnsignedInt16          = 1234,
                UnsignedInt32          = 1234565789U,
                UnsignedInt64          = 1234567890123456789UL,
                Character              = 'a',
                SignedByte             = -128,
                Guid                   = new Guid(guid.ToString()),
                EnumInt32              = EnumInt32.SomeValue,
                FlagsEnum              = FlagsEnum.FirstFlag | FlagsEnum.SecondFlag,
                NullableInt16          = -1234,
                NullableInt32          = null,
                NullableInt64          = -1234567890123456789L,
                NullableDouble         = -1.23456789,
                NullableDecimal        = -1234567890.01M,
                NullableDateTime       = new DateTime(dateTime.Ticks, dateTime.Kind),
                NullableDateTimeOffset = new DateTimeOffset(dateTimeOffset.DateTime, dateTimeOffset.Offset),
                NullableTimeSpan       = null,
                NullableSingle         = -1.234F,
                NullableBoolean        = null,
                NullableByte           = 255,
                NullableUnsignedInt16  = 1234,
                NullableUnsignedInt32  = 1234565789U,
                NullableUnsignedInt64  = null,
                NullableCharacter      = 'a',
                NullableSignedByte     = -128,
                NullableGuid           = new Guid(guid.ToString()),
                NullableEnumInt32      = null,
                NullableFlagsEnum      = FlagsEnum.Default,
                Object                 = null,
                IntArray               = new[] { new int?[, ] {
                                                     { 1 }, { 2 }
                                                 }, new int?[, ] {
                                                     { null }, { 3 }
                                                 } },
                DecimalArray = new[, ] {
                    { new decimal[] { 1, 2 }, new decimal[] { 3, 4 } }
                },
                StringList = new List <string> {
                    "1", null
                },
                Dictionary = new Dictionary <bool, bool?> {
                    { false, null }, { true, true }
                },
                Tuple      = Tuple.Create(-1, "minus one"),
                ValueTuple = (-1, "minus one"),
                Type       = typeof(AllSupportedTypes)
            };

            var script = CSScriptSerializer.Serialize(input);
            var output = CSScriptSerializer.DeserializeAsync <AllSupportedTypes>(script).GetAwaiter().GetResult();

            foreach (var propertyInfo in typeof(AllSupportedTypes).GetProperties())
            {
                Assert.Equal(propertyInfo.GetValue(input, null), propertyInfo.GetValue(output, null));
            }
        }
 public static T Load <T>(string script)
 => CSScriptSerializer.Deserialize <T>(script, Enumerable.Empty <Assembly>(), Namespaces);