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.AreEqual(3, output.NumPages); Assert.IsNotEmpty(output); Assert.AreEqual(2, output.Count); }
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_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.NotEmpty(output.Weather); Assert.Equal(4, output.Weather.Count); Assert.Equal("Sunny", output.Weather[0].Condition.Data); }
public void Can_Deserialize_Lists_of_Simple_Types() { var xmlpath = PathFor("xmllists.xml"); var doc = XDocument.Load(xmlpath); var xml = new XmlAttributeDeserializer(); var output = xml.Deserialize <SimpleTypesListSample>(new RestResponse() { Content = doc.ToString() }); Assert.NotEmpty(output.Names); Assert.NotEmpty(output.Numbers); Assert.False(output.Names[0].Length == 0); Assert.False(output.Numbers.Sum() == 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 <SampleClasses.xml_api_reply>(response); Assert.NotEmpty(output.weather); Assert.Equal(4, output.weather.Count); Assert.Equal("Sunny", output.weather[0].condition.data); }
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 <SampleClasses.Lastfm.Event>(response); //Assert.NotEmpty(output.artists); 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_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); }
/// <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_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); }
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>()); }
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); }
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_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); }
public void Can_Deserialize_Custom_Formatted_Date() { CultureInfo culture = CultureInfo.InvariantCulture; const string format = "dd yyyy MMM, hh:mm ss tt zzz"; DateTime date = new DateTime(2010, 2, 8, 11, 11, 11); XDocument doc = new XDocument(); XElement root = new XElement("Person"); root.Add(new XElement("StartDate", date.ToString(format, culture))); doc.Add(root); XmlAttributeDeserializer xml = new XmlAttributeDeserializer { DateFormat = format, Culture = culture }; RestResponse response = new RestResponse { Content = doc.ToString() }; PersonForXml output = xml.Deserialize <PersonForXml>(response); Assert.AreEqual(date, output.StartDate); }
public QueryResult Search(string query) { //API example: http://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=wikipedia&srprop=timestamp _client.BaseUrl = string.Format(UseTLS ? "https://{0}.wikipedia.org/w/" : "http://{0}.wikipedia.org/w/", Language.GetStringValue()); RestRequest request = new RestRequest("api.php", Method.GET); //Required request.AddParameter("action", "query"); request.AddParameter("list", "search"); request.AddParameter("srsearch", query); request.AddParameter("format", Format.ToString().ToLower()); //Optional if (Infos.HasElements()) { request.AddParameter("srinfo", string.Join("|", Infos).ToLower()); } if (Limit != 0) { request.AddParameter("srlimit", Limit); } if (Offset != 0) { request.AddParameter("sroffset", Offset); } if (Namespaces.HasElements()) { request.AddParameter("srnamespace", string.Join("|", Namespaces).ToLower()); } if (Properties.HasElements()) { request.AddParameter("srprop", string.Join("|", Properties).ToLower()); } if (Redirects) { request.AddParameter("srredirects", Redirects.ToString().ToLower()); } if (What != What.Title) { request.AddParameter("srwhat", What.ToString().ToLower()); } if (ServedBy) { request.AddParameter("servedby", ServedBy.ToString().ToLower()); } if (!string.IsNullOrEmpty(RequestID)) { request.AddParameter("requestid", RequestID); } //Output RestResponse response = (RestResponse)_client.Execute(request); IDeserializer deserializer; switch (Format) { case Format.XML: deserializer = new XmlAttributeDeserializer(); break; case Format.JSON: deserializer = new JsonDeserializer(); break; default: deserializer = new XmlAttributeDeserializer(); break; } //The format that Wikipedia uses deserializer.DateFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"; deserializer.RootElement = "query"; QueryResult results = deserializer.Deserialize <QueryResult>(response); //For convinience, we autocreate Uris that point directly to the wiki page. if (results.Search != null) { foreach (Search search in results.Search) { search.Url = UseTLS ? new Uri("https://" + Language.GetStringValue() + ".wikipedia.org/wiki/" + search.Title) : new Uri("http://" + Language.GetStringValue() + ".wikipedia.org/wiki/" + search.Title); } } return(results); }
public T Search <T>(string query) { //API example: http://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=wikipedia&srprop=timestamp Client.BaseUrl = new Uri(string.Format((UseTls ? "https://{0}.wikipedia.org/w/" : "http://{0}.wikipedia.org/w/"), "en")); RestRequest request = new RestRequest("api.php", Method.GET); //Required request.AddParameter("action", "query"); request.AddParameter("format", Format.ToString().ToLower()); if (ExternParameters != null) { foreach (var parameter in ExternParameters) { if (!string.IsNullOrWhiteSpace(parameter.Item1) && parameter.Item2 != null) { request.AddParameter(parameter.Item1, parameter.Item2); } } } if (!string.IsNullOrWhiteSpace(query)) { request.AddParameter("srsearch", query); } //Optional if (Infos.HasElements()) { request.AddParameter("srinfo", string.Join("|", Infos).ToLower()); } if (Limit != 0) { switch (List) { case ListType.Geosearch: request.AddParameter("list", List.ToString().ToLower()); request.AddParameter("gslimit", Limit); break; case ListType.Search: request.AddParameter("list", List.ToString().ToLower()); request.AddParameter("srlimit", Limit); break; } } if (Offset != 0) { request.AddParameter("sroffset", Offset); } if (Namespaces.HasElements()) { request.AddParameter("srnamespace", string.Join("|", Namespaces).ToLower()); } if (Properties.HasElements()) { request.AddParameter("srprop", string.Join("|", Properties).ToLower()); } if (Redirects) { request.AddParameter("srredirects", Redirects.ToString().ToLower()); } if (What != What.Title) { request.AddParameter("srwhat", What.ToString().ToLower()); } if (ServedBy) { request.AddParameter("servedby", ServedBy.ToString().ToLower()); } if (!string.IsNullOrEmpty(RequestId)) { request.AddParameter("requestid", RequestId); } //Output RestResponse response = (RestResponse)Client.Execute(request); IDeserializer deserializer; switch (Format) { case Format.Xml: deserializer = new XmlAttributeDeserializer(); break; case Format.Json: deserializer = new JsonDeserializer(); break; default: deserializer = new XmlAttributeDeserializer(); break; } //The format that Wikipedia uses deserializer.DateFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"; return(deserializer.Deserialize <T>(response)); }