Ejemplo n.º 1
0
        public void Can_Deserialize_Empty_Elements_With_Attributes_to_Nullable_Values()
        {
            var doc    = CreateXmlWithAttributesAndNullValues();
            var xml    = new XmlAttributeDeserializer();
            var output = xml.Deserialize <NullableValues>(new RestResponse {
                Content = doc
            });

            Assert.Null(output.Id);
            Assert.Null(output.StartDate);
            Assert.Null(output.UniqueId);
        }
Ejemplo n.º 2
0
        public void Can_Deserialize_Parentless_aka_Inline_List_Items_With_Matching_Class_Name()
        {
            var xmlpath = PathFor("InlineListSample.xml");
            var doc     = XDocument.Load(xmlpath);
            var xml     = new XmlAttributeDeserializer();
            var output  = xml.Deserialize <InlineListSample>(new RestResponse {
                Content = doc.ToString()
            });

            Assert.IsNotEmpty(output.images);
            Assert.AreEqual(4, output.images.Count);
        }
Ejemplo n.º 3
0
        public void Can_Deserialize_Mixture_Of_Empty_Elements_With_Attributes_And_Populated_Elements()
        {
            var doc    = CreateXmlWithAttributesAndNullValuesAndPopulatedValues();
            var xml    = new XmlAttributeDeserializer();
            var output = xml.Deserialize <NullableValues>(new RestResponse {
                Content = doc
            });

            Assert.Null(output.Id);
            Assert.Null(output.StartDate);
            Assert.AreEqual(new Guid(GUID_STRING), output.UniqueId);
        }
Ejemplo n.º 4
0
        public void Can_Deserialize_Nested_Class()
        {
            var doc      = CreateElementsXml();
            var response = new RestResponse {
                Content = doc
            };
            var d = new XmlAttributeDeserializer();
            var p = d.Deserialize <PersonForXml>(response);

            Assert.NotNull(p.FavoriteBand);
            Assert.AreEqual("Goldfinger", p.FavoriteBand.Name);
        }
Ejemplo n.º 5
0
        public void Can_Deserialize_Boolean_From_String()
        {
            var xmlpath  = PathFor("boolean_from_string.xml");
            var doc      = XDocument.Load(xmlpath);
            var response = new RestResponse {
                Content = doc.ToString()
            };
            var d      = new XmlAttributeDeserializer();
            var output = d.Deserialize <BooleanTest>(response);

            Assert.True(output.Value);
        }
Ejemplo n.º 6
0
        public void Can_Deserialize_Directly_To_Lists_Off_Root_Element()
        {
            var xmlpath = PathFor("directlists.xml");
            var doc     = XDocument.Load(xmlpath);
            var xml     = new XmlAttributeDeserializer();
            var output  = xml.Deserialize <List <Database> >(new RestResponse {
                Content = doc.ToString()
            });

            Assert.IsNotEmpty(output);
            Assert.AreEqual(2, output.Count);
        }
Ejemplo n.º 7
0
        public void Can_Deserialize_To_Standalone_List_With_Matching_Class_Case()
        {
            var xmlpath = PathFor("InlineListSample.xml");
            var doc     = XDocument.Load(xmlpath);
            var xml     = new XmlAttributeDeserializer();
            var output  = xml.Deserialize <List <image> >(new RestResponse {
                Content = doc.ToString()
            });

            Assert.IsNotEmpty(output);
            Assert.AreEqual(4, output.Count);
        }
Ejemplo n.º 8
0
        public void Can_Deserialize_Nested_List_Without_Elements_To_Empty_List()
        {
            var doc    = CreateXmlWithEmptyNestedList();
            var xml    = new XmlAttributeDeserializer();
            var output = xml.Deserialize <EmptyListSample>(new RestResponse {
                Content = doc
            });

            Assert.NotNull(output.images);
            Assert.NotNull(output.Images);
            Assert.IsEmpty(output.images);
            Assert.IsEmpty(output.Images);
        }
Ejemplo n.º 9
0
        public void Can_Deserialize_Parentless_aka_Inline_List_Items_Without_Matching_Class_Name_Using_XmlAttributeDeserializer()
        {
            var xmlpath = Environment.CurrentDirectory + @"\SampleData\InlineListSample.xml";
            var doc     = XDocument.Load(xmlpath);

            var xml    = new XmlAttributeDeserializer();
            var output = xml.Deserialize <InlineListSample>(new RestResponse {
                Content = doc.ToString()
            });

            Assert.NotEmpty(output.Images);
            Assert.Equal(4, output.Images.Count);
        }
        public void Can_Deserialize_Root_Elements_Without_Matching_Case_And_Dashes()
        {
            string       doc      = CreateLowerCasedRootElementWithDashesXml();
            RestResponse response = new RestResponse {
                Content = doc
            };
            XmlAttributeDeserializer d = new XmlAttributeDeserializer();
            List <IncomingInvoice>   p = d.Deserialize <List <IncomingInvoice> >(response);

            Assert.NotNull(p);
            Assert.AreEqual(1, p.Count);
            Assert.AreEqual(45, p[0].ConceptId);
        }
        public void Can_Deserialize_Inline_List_Without_Elements_To_Empty_List()
        {
            string doc = CreateXmlWithEmptyInlineList();
            XmlAttributeDeserializer xml    = new XmlAttributeDeserializer();
            EmptyListSample          output = xml.Deserialize <EmptyListSample>(new RestResponse {
                Content = doc
            });

            Assert.NotNull(output.images);
            Assert.NotNull(output.Images);
            Assert.IsEmpty(output.images);
            Assert.IsEmpty(output.Images);
        }
    public void Can_Deserialize_Google_Weather_Xml_WithDeserializeAs()
    {
        var xmlpath  = PathFor("GoogleWeather.xml");
        var doc      = XDocument.Load(xmlpath);
        var response = new RestResponse {
            Content = doc.ToString()
        };
        var d      = new XmlAttributeDeserializer();
        var output = d.Deserialize <GoogleWeatherApi>(response) !;

        Assert.Equal(4, output.Weather.Count);
        Assert.Equal("Sunny", output.Weather[0].Condition.Data);
    }
Ejemplo n.º 13
0
        public void Can_Deserialize_Place_Listing()
        {
            var xmlpath  = Environment.CurrentDirectory + @"\..\..\..\LonelySharp.Tests\Responses\Places_List.xml";
            var doc      = XDocument.Load(xmlpath);
            var response = new RestResponse {
                Content = doc.ToString()
            };

            var d      = new XmlAttributeDeserializer();
            var output = d.Deserialize <PlaceCollection>(response);

            Assert.AreEqual(output[0].Fullname, "Australia & Pacific -> Australia -> Queensland -> Brisbane");
            Assert.AreEqual(output[0].Northlatitude, -27.434999);
        }
        public void Can_Deserialize_Lastfm_Xml()
        {
            string       xmlpath  = this.PathFor("Lastfm.xml");
            XDocument    doc      = XDocument.Load(xmlpath);
            RestResponse response = new RestResponse {
                Content = doc.ToString()
            };
            XmlAttributeDeserializer d = new XmlAttributeDeserializer();
            Event output = d.Deserialize <Event>(response);

            //Assert.IsNotEmpty(output.artists);
            Assert.AreEqual("http://www.last.fm/event/328799+Philip+Glass+at+Barbican+Centre+on+12+June+2008", output.url);
            Assert.AreEqual("http://www.last.fm/venue/8777860+Barbican+Centre", output.venue.url);
        }
        public void Can_Deserialize_Google_Weather_Xml_WithDeserializeAs()
        {
            string       xmlpath  = this.PathFor("GoogleWeather.xml");
            XDocument    doc      = XDocument.Load(xmlpath);
            RestResponse response = new RestResponse {
                Content = doc.ToString()
            };
            XmlAttributeDeserializer d      = new XmlAttributeDeserializer();
            GoogleWeatherApi         output = d.Deserialize <GoogleWeatherApi>(response);

            Assert.IsNotEmpty(output.Weather);
            Assert.AreEqual(4, output.Weather.Count);
            Assert.AreEqual("Sunny", output.Weather[0].Condition.Data);
        }
Ejemplo n.º 16
0
        public void Can_Deserialize_POI()
        {
            var xmlpath  = Environment.CurrentDirectory + @"\..\..\..\LonelySharp.Tests\Responses\POI_Details.xml";
            var doc      = XDocument.Load(xmlpath);
            var response = new RestResponse {
                Content = doc.ToString()
            };

            var d      = new XmlAttributeDeserializer();
            var output = d.Deserialize <POI>(response);

            Assert.AreEqual(output.Name, "Alhambra Lounge");
            Assert.AreEqual(output.Urls[0].Value, "www.alhambralounge.com");
        }
Ejemplo n.º 17
0
        public void Can_Deserialize_Google_Weather_Xml()
        {
            var xmlpath  = PathFor("GoogleWeather.xml");
            var doc      = XDocument.Load(xmlpath);
            var response = new RestResponse {
                Content = doc.ToString()
            };
            var d      = new XmlAttributeDeserializer();
            var output = d.Deserialize <xml_api_reply>(response);

            Assert.IsNotEmpty(output.weather);
            Assert.AreEqual(4, output.weather.Count);
            Assert.AreEqual("Sunny", output.weather[0].condition.data);
        }
    public void Can_Deserialize_To_List_Inheritor_From_Custom_Root_With_Attributes()
    {
        var xmlPath = PathFor("ListWithAttributes.xml");
        var doc     = XDocument.Load(xmlPath);
        var xml     = new XmlAttributeDeserializer {
            RootElement = "Calls"
        };
        var output = xml.Deserialize <TwilioCallList>(new RestResponse {
            Content = doc.ToString()
        }) !;

        Assert.Equal(3, output.NumPages);
        Assert.Equal(2, output.Count);
    }
    public void Can_Deserialize_Eventful_Xml()
    {
        var xmlpath  = PathFor("eventful.xml");
        var doc      = XDocument.Load(xmlpath);
        var response = new RestResponse {
            Content = doc.ToString()
        };
        var d      = new XmlAttributeDeserializer();
        var output = d.Deserialize <VenueSearch>(response) !;

        Assert.Equal(3, output.venues.Count);
        Assert.Equal("Tivoli", output.venues[0].name);
        Assert.Equal("http://eventful.com/brisbane/venues/tivoli-/V0-001-002169294-8", output.venues[1].url);
        Assert.Equal("V0-001-000266914-3", output.venues[2].id);
    }
Ejemplo n.º 20
0
        public void Can_Deserialize_POI_List()
        {
            var xmlpath  = Environment.CurrentDirectory + @"\..\..\..\LonelySharp.Tests\Responses\POI_List.xml";
            var doc      = XDocument.Load(xmlpath);
            var response = new RestResponse {
                Content = doc.ToString()
            };

            var d      = new XmlAttributeDeserializer();
            var output = d.Deserialize <POICollection>(response);

            Assert.AreEqual(output[0].ID, 1077533);
            Assert.AreEqual(output[0].POIType.ToString(), "Eat");
            Assert.AreEqual(output.Count, 222);
        }
        public void Can_Deserialize_Lists_of_Simple_Types()
        {
            string    xmlpath               = this.PathFor("xmllists.xml");
            XDocument doc                   = XDocument.Load(xmlpath);
            XmlAttributeDeserializer xml    = new XmlAttributeDeserializer();
            SimpleTypesListSample    output = xml.Deserialize <SimpleTypesListSample>(
                new RestResponse {
                Content = doc.ToString()
            });

            Assert.IsNotEmpty(output.Names);
            Assert.IsNotEmpty(output.Numbers);
            Assert.False(output.Names[0].Length == 0);
            Assert.False(output.Numbers.Sum() == 0);
        }
        public void Can_Deserialize_To_List_Inheritor_From_Custom_Root_With_Attributes()
        {
            string    xmlpath            = this.PathFor("ListWithAttributes.xml");
            XDocument doc                = XDocument.Load(xmlpath);
            XmlAttributeDeserializer xml = new XmlAttributeDeserializer {
                RootElement = "Calls"
            };
            TwilioCallList output = xml.Deserialize <TwilioCallList>(new RestResponse {
                Content = doc.ToString()
            });

            Assert.AreEqual(3, output.NumPages);
            Assert.IsNotEmpty(output);
            Assert.AreEqual(2, output.Count);
        }
Ejemplo n.º 23
0
        /// <summary>
        /// In case ScanTimeout was set too low, some scanners might have timedout. This method tries to recalculate the query in such a way that
        /// only the timedout scanners return their result. This is especially useful in a multi-threaded environment.
        /// </summary>
        /// <param name="result">The new pods that have been calculated.</param>
        /// <param name="includeInOriginal">When true, it will add the new pods to the original QueryResult</param>
        public static List <Pod> RecalculateResults(this QueryResult result, bool includeInOriginal = true)
        {
            if (!string.IsNullOrEmpty(result.Recalculate) && !string.IsNullOrEmpty(result.Timedout))
            {
                WebClient client      = new WebClient();
                string    newPodsData = client.DownloadString(result.Recalculate);

                XmlAttributeDeserializer deserializer = new XmlAttributeDeserializer();
                RestResponse             response     = new RestResponse();
                response.Content = newPodsData;

                QueryResult asyncResult = deserializer.Deserialize <QueryResult>(response);
                result.Recalculate  = asyncResult.Recalculate;
                result.TimedoutPods = asyncResult.TimedoutPods;

                List <Pod> newPods = asyncResult.Pods;

                if (newPods != null)
                {
                    newPods.Sort((p1, p2) => p1.Position.CompareTo(p2.Position));

                    if (includeInOriginal)
                    {
                        if (result.Pods == null)
                        {
                            result.Pods = newPods;
                        }
                        else
                        {
                            foreach (Pod newPod in newPods)
                            {
                                if (!result.Pods.Contains(newPod))
                                {
                                    result.Pods.Add(newPod);
                                }
                            }
                        }

                        //We make sure that the pods are in the right order.
                        result.Pods.Sort((p1, p2) => p1.Position.CompareTo(p2.Position));
                    }

                    return(newPods);
                }
            }

            return(new List <Pod>());
        }
    public void Can_Deserialize_Lastfm_Xml()
    {
        var xmlpath  = PathFor("Lastfm.xml");
        var doc      = XDocument.Load(xmlpath);
        var response = new RestResponse {
            Content = doc.ToString()
        };
        var d      = new XmlAttributeDeserializer();
        var output = d.Deserialize <Event>(response) !;

        Assert.Equal(
            "http://www.last.fm/event/328799+Philip+Glass+at+Barbican+Centre+on+12+June+2008",
            output.url
            );
        Assert.Equal("http://www.last.fm/venue/8777860+Barbican+Centre", output.venue.url);
    }
        public void Can_Deserialize_Elements_to_Nullable_Values()
        {
            CultureInfo culture          = CultureInfo.InvariantCulture;
            string      doc              = CreateXmlWithoutEmptyValues(culture);
            XmlAttributeDeserializer xml = new XmlAttributeDeserializer
            {
                Culture = culture
            };
            NullableValues output = xml.Deserialize <NullableValues>(new RestResponse {
                Content = doc
            });

            Assert.NotNull(output.Id);
            Assert.NotNull(output.StartDate);
            Assert.NotNull(output.UniqueId);
            Assert.AreEqual(123, output.Id);
            Assert.AreEqual(new DateTime(2010, 2, 21, 9, 35, 00), output.StartDate);
            Assert.AreEqual(new Guid(GUID_STRING), output.UniqueId);
        }
Ejemplo n.º 26
0
        public static List <Pod> GetAsyncPods(this QueryResult result, bool includeInOriginal = true)
        {
            if (result.Pods.Any(p => !string.IsNullOrEmpty(p.Async)))
            {
                IEnumerable <string> asyncPods = result.Pods.Where(p => !string.IsNullOrEmpty(p.Async)).Select(p => p.Async);
                WebClient            client    = new WebClient();

                XmlAttributeDeserializer deserializer = new XmlAttributeDeserializer();
                RestResponse             response     = new RestResponse();
                List <Pod> newPods = new List <Pod>();

                foreach (string asyncPodLink in asyncPods)
                {
                    string newPodsData = client.DownloadString(asyncPodLink);
                    response.Content = newPodsData;
                    newPods.Add(deserializer.Deserialize <Pod>(response));
                }

                newPods.Sort((p1, p2) => p1.Position.CompareTo(p2.Position));

                if (includeInOriginal)
                {
                    foreach (Pod newPod in newPods)
                    {
                        //Replace the existing pod with the new pod.
                        //Note: Pods are compared by ID, so this is functional code.
                        if (result.Pods.Contains(newPod))
                        {
                            result.Pods.Remove(newPod);
                            result.Pods.Add(newPod);
                        }
                    }

                    //We make sure that the pods are in the right order.
                    result.Pods.Sort((p1, p2) => p1.Position.CompareTo(p2.Position));
                }

                return(newPods);
            }

            return(new List <Pod>());
        }
Ejemplo n.º 27
0
        public Lang[] GetLangs()
        {
            RestRequest request = new RestRequest("getLangs");

            request.AddParameter("key", _key);

            RestResponse             response     = (RestResponse)_client.Execute(request);
            XmlAttributeDeserializer deserializer = new XmlAttributeDeserializer();

            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                var    strs     = deserializer.Deserialize <List <string> >(response);
                var    allLangs = (Lang[])Enum.GetValues(typeof(Lang));
                Lang[] result   = allLangs.Where(lang => strs.Contains(lang.ToString().ToLowerInvariant())).ToArray();
                return(result);
            }
            else
            {
                var error = deserializer.Deserialize <YandexError>(response);
                throw new YandexLinguisticsException(error);
            }
        }
Ejemplo n.º 28
0
        public void Can_Deserialize_TimeSpan()
        {
            var culture = CultureInfo.InvariantCulture;
            var doc     = new XDocument(culture);

            TimeSpan?nullTimespan      = null;
            TimeSpan?nullValueTimeSpan = new TimeSpan(21, 30, 7);

            var root = new XElement("Person");

            root.Add(new XElement("Tick", new TimeSpan(468006)));
            root.Add(new XElement("Millisecond", new TimeSpan(0, 0, 0, 0, 125)));
            root.Add(new XElement("Second", new TimeSpan(0, 0, 8)));
            root.Add(new XElement("Minute", new TimeSpan(0, 55, 2)));
            root.Add(new XElement("Hour", new TimeSpan(21, 30, 7)));
            root.Add(new XElement("NullableWithoutValue", nullTimespan));
            root.Add(new XElement("NullableWithValue", nullValueTimeSpan));

            doc.Add(root);

            var response = new RestResponse {
                Content = doc.ToString()
            };

            var d = new XmlAttributeDeserializer()
            {
                Culture = culture,
            };
            var payload = d.Deserialize <TimeSpanTestStructure>(response);

            Assert.Equal(new TimeSpan(468006), payload.Tick);
            Assert.Equal(new TimeSpan(0, 0, 0, 0, 125), payload.Millisecond);
            Assert.Equal(new TimeSpan(0, 0, 8), payload.Second);
            Assert.Equal(new TimeSpan(0, 55, 2), payload.Minute);
            Assert.Equal(new TimeSpan(21, 30, 7), payload.Hour);
            Assert.Null(payload.NullableWithoutValue);
            Assert.NotNull(payload.NullableWithValue);
            Assert.Equal(new TimeSpan(21, 30, 7), payload.NullableWithValue.Value);
        }
        public void Can_Deserialize_Attributes_On_Default_Root()
        {
            string       doc      = CreateAttributesXml();
            RestResponse response = new RestResponse {
                Content = doc
            };
            XmlAttributeDeserializer d = new XmlAttributeDeserializer();
            PersonForXml             p = d.Deserialize <PersonForXml>(response);

            Assert.AreEqual("John Sheehan", p.Name);
            Assert.AreEqual(new DateTime(2009, 9, 25, 0, 6, 1), p.StartDate);
            Assert.AreEqual(28, p.Age);
            Assert.AreEqual(long.MaxValue, p.BigNumber);
            Assert.AreEqual(99.9999m, p.Percent);
            Assert.AreEqual(false, p.IsCool);
            Assert.AreEqual(new Guid(GUID_STRING), p.UniqueId);
            Assert.AreEqual(new Uri("http://example.com", UriKind.RelativeOrAbsolute), p.Url);
            Assert.AreEqual(new Uri("/foo/bar", UriKind.RelativeOrAbsolute), p.UrlPath);
            Assert.NotNull(p.BestFriend);
            Assert.AreEqual("The Fonz", p.BestFriend.Name);
            Assert.AreEqual(1952, p.BestFriend.Since);
        }
Ejemplo n.º 30
0
        public void Can_Deserialize_DateTimeOffset()
        {
            var culture = CultureInfo.InvariantCulture;
            var doc     = new XDocument(culture);

            DateTimeOffset DateTimeOffset = new DateTimeOffset(2013, 02, 08, 9, 18, 22, TimeSpan.FromHours(10));
            DateTimeOffset?NullableDateTimeOffsetWithValue = new DateTimeOffset(2013, 02, 08, 9, 18, 23, TimeSpan.FromHours(10));

            var root = new XElement("Dates");

            root.Add(new XElement("DateTimeOffset", DateTimeOffset));
            root.Add(new XElement("NullableDateTimeOffsetWithNull", string.Empty));
            root.Add(new XElement("NullableDateTimeOffsetWithValue", NullableDateTimeOffsetWithValue));

            doc.Add(root);

            var xml = new XmlAttributeDeserializer
            {
                Culture = culture,
            };

            var response = new RestResponse {
                Content = doc.ToString()
            };

            var d = new XmlAttributeDeserializer()
            {
                Culture = culture,
            };
            var payload = d.Deserialize <DateTimeTestStructure>(response);

            Assert.Equal(DateTimeOffset, payload.DateTimeOffset);
            Assert.Null(payload.NullableDateTimeOffsetWithNull);

            Assert.True(payload.NullableDateTimeOffsetWithValue.HasValue);
            Assert.Equal(NullableDateTimeOffsetWithValue, payload.NullableDateTimeOffsetWithValue);
        }