Exemple #1
0
        public void SerializeDeserializeTests(bool indented, bool defaults, bool enumAsString)
        {
            {
                var test = (int)1;
                var json = test.ToJson(indented, defaults, enumAsString);
                Assert.Equal(test, json.FromJson <int>());
                Assert.Equal(test, json.FromJson(typeof(int)));
                Assert.Equal(test.GetType().Name, json.FromJson(typeof(int)).GetType().Name);
            }

            {
                var test = (double)1.23;
                var json = test.ToJson(indented, defaults, enumAsString);
                Assert.Equal(test, json.FromJson <double>());
                Assert.Equal(test, json.FromJson(typeof(double)));
                Assert.Equal(test.GetType().Name, json.FromJson(typeof(double)).GetType().Name);
            }

            {
                var test = "hallo";
                var json = test.ToJson(indented, defaults, enumAsString);
                Assert.Equal(test, json.FromJson <string>());
                Assert.Equal(test, json.FromJson(typeof(string)));
                Assert.Equal(test.GetType().Name, json.FromJson(typeof(string)).GetType().Name);
            }

            {
                var test = new PropertyClass
                {
                    Name         = "hello",
                    Age          = 1,
                    Weight       = 47.11,
                    DayOfWeek    = DayOfWeek.Monday,
                    ListOfString = new List <string> {
                        "A", "B", "C"
                    }
                };
                var json = test.ToJson(indented, defaults, enumAsString);
                if (enumAsString)
                {
                    Assert.Contains(test.DayOfWeek.ToString().ToLower(), json);
                }
                else
                {
                    Assert.DoesNotContain(test.DayOfWeek.ToString().ToLower(), json);
                }
                {
                    var result = json.FromJson <PropertyClass>();
                    Assert.Equal(test.Name, result.Name);
                    Assert.Equal(test.Age, result.Age);
                    Assert.Equal(test.Weight, result.Weight);
                    Assert.Equal(test.DayOfWeek, result.DayOfWeek);
                    Assert.Equal(3, result.ListOfString.Count);
                    Assert.Equal("A", result.ListOfString[0]);
                    Assert.Equal("B", result.ListOfString[1]);
                    Assert.Equal("C", result.ListOfString[2]);
                }
                {
                    var result = (PropertyClass)json.FromJson(typeof(PropertyClass));
                    Assert.Equal(test.Name, result.Name);
                    Assert.Equal(test.Age, result.Age);
                    Assert.Equal(test.Weight, result.Weight);
                    Assert.Equal(test.DayOfWeek, result.DayOfWeek);
                    Assert.Equal(3, result.ListOfString.Count);
                    Assert.Equal("A", result.ListOfString[0]);
                    Assert.Equal("B", result.ListOfString[1]);
                    Assert.Equal("C", result.ListOfString[2]);
                }
            }

            {
                var test = new PropertyClass
                {
                    Name         = "hello",
                    Age          = 1,
                    Weight       = 47.11,
                    DayOfWeek    = DayOfWeek.Friday,
                    ListOfString = new List <string> {
                        "A", "B", "C"
                    }
                };
                var json = test.ToJsonEnvelope(indented, defaults, enumAsString);
                if (enumAsString)
                {
                    Assert.Contains(test.DayOfWeek.ToString().ToLower(), json);
                }
                else
                {
                    Assert.DoesNotContain(test.DayOfWeek.ToString().ToLower(), json);
                }
                {
                    var result = json.FormJsonEnvelope <PropertyClass>();
                    Assert.Equal(test.Name, result.Name);
                    Assert.Equal(test.Age, result.Age);
                    Assert.Equal(test.Weight, result.Weight);
                    Assert.Equal(test.DayOfWeek, result.DayOfWeek);
                    Assert.Equal(3, result.ListOfString.Count);
                    Assert.Equal("A", result.ListOfString[0]);
                    Assert.Equal("B", result.ListOfString[1]);
                    Assert.Equal("C", result.ListOfString[2]);
                }

                {
                    var resultRaw = json.FormJsonEnvelope(typeof(PropertyClass));
                    Assert.Equal(typeof(PropertyClass), resultRaw.GetType());
                    var result = (PropertyClass)resultRaw;
                    Assert.Equal(test.Name, result.Name);
                    Assert.Equal(test.Age, result.Age);
                    Assert.Equal(test.Weight, result.Weight);
                    Assert.Equal(test.DayOfWeek, result.DayOfWeek);
                    Assert.Equal(3, result.ListOfString.Count);
                    Assert.Equal("A", result.ListOfString[0]);
                    Assert.Equal("B", result.ListOfString[1]);
                    Assert.Equal("C", result.ListOfString[2]);
                }

                {
                    var(Name, Data) = json.FormJsonEnvelopeAsEnvelop <PropertyClass>();
                    Assert.Equal(Name, typeof(PropertyClass).Name);
                    Assert.Equal(typeof(PropertyClass).Name, Data.GetType().Name);
                    Assert.Equal(test.Name, Data.Name);
                    Assert.Equal(test.Age, Data.Age);
                    Assert.Equal(test.Weight, Data.Weight);
                    Assert.Equal(3, Data.ListOfString.Count);
                    Assert.Equal("A", Data.ListOfString[0]);
                    Assert.Equal("B", Data.ListOfString[1]);
                    Assert.Equal("C", Data.ListOfString[2]);
                }
            }
        }