public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.Null) { return(null); } var urlString = (string)reader.Value; var url = urlString.AsUri(); return (#if !STANDALONE objectType == typeof(WebAudio) ? WebAudio.FromUrl(url) : objectType == typeof(WebVideo) ? WebVideo.FromUrl(url) : objectType == typeof(WebImage) ? WebImage.FromUrl(url) : #endif WebFile.FromUrl(url)); }
/// <summary> /// Converts a string to an object of the specified type. /// </summary> /// <param name="str">The string to convert.</param> /// <param name="type">The destination type.</param> /// <param name="descriptiveName">A descriptive name for the object (if any).</param> /// <param name="culture">The culture identifier for number and dates.</param> /// <returns>The converted object.</returns> public static object ConvertFromString(string str, TypeBase type, string descriptiveName = null, string culture = null, Currency currency = Currency.None, HtmlDocument ownerDocument = null, string inlineListSeparator = null, string timezone = null, string format = null) { var cult = GetCulture(culture); var underlyingType = TypeBase.GetUnderlyingType(type); if (underlyingType != null) { return(ConvertFromString(str, underlyingType, descriptiveName, culture, currency, ownerDocument, inlineListSeparator, timezone, format)); } var enumType = type as EnumType; if (enumType != null) { return(enumType.Resolve(str, false)); } var t = type.GetNativeType(); if (t == typeof(string)) { return(str); } if (t == typeof(Html)) { return(new Html(str, ownerDocument != null ? ownerDocument.GetLazyBaseUrl() : null)); } if (t == typeof(FileSize)) { return(FileSize.Parse(str)); } if (t == typeof(GeographicLocation)) { return(GeographicLocation.FromString(str)); } if (t == typeof(Uri)) { return(new Uri(str)); } if (t == typeof(UriObject)) { return(UriObject.FromUrl(str.AsUri())); } if (t == typeof(WebImage)) { return(WebImage.FromUrl(str.AsUri())); } if (t == typeof(WebFile)) { return(WebFile.FromUrl(str.AsUri())); } if (t == typeof(WebAudio)) { return(WebAudio.FromUrl(str.AsUri())); } if (t == typeof(WebVideo)) { return(WebVideo.FromUrl(str.AsUri())); } if (t == typeof(Money)) { if (string.IsNullOrEmpty(str)) { return(null); } return(Money.Parse(str, currency, cult)); } // if (t == typeof(Memo)) return new Memo(str); if (t == typeof(SByte)) { return(Convert.ToSByte(str)); } if (t == typeof(Int16)) { return(Convert.ToInt16(RemoveThousandsSeparators(str, cult), cult)); } if (t == typeof(Int32)) { return(Convert.ToInt32(RemoveThousandsSeparators(str, cult), cult)); } if (t == typeof(Int64)) { return(Convert.ToInt64(RemoveThousandsSeparators(str, cult), cult)); } if (t == typeof(Byte)) { return(Convert.ToByte(str)); } if (t == typeof(UInt16)) { return(Convert.ToUInt16(RemoveThousandsSeparators(str, cult), cult)); } if (t == typeof(UInt32)) { return(Convert.ToUInt32(RemoveThousandsSeparators(str, cult), cult)); } if (t == typeof(UInt64)) { return(Convert.ToUInt64(RemoveThousandsSeparators(str, cult), cult)); } if (t == typeof(Single)) { return(Convert.ToSingle(RemoveThousandsSeparators(str, cult), cult)); } if (t == typeof(Double)) { return(Convert.ToDouble(RemoveThousandsSeparators(str, cult), cult)); } if (t == typeof(Decimal)) { return(Convert.ToDecimal(RemoveThousandsSeparators(str, cult), cult)); } if (t == typeof(DateTime)) { return(ParseDateTime(str, cult, ownerDocument != null ? Utils.TryGetPageRetrievalDate(ownerDocument) : null, timezone, format)); } if (t == typeof(Size)) { return(new Size(int.Parse(str.Capture(@"(\d+)\s*(?:x|×|\*)")), int.Parse(str.Capture(@"(?:x|×|\*)\s*(\d+)")))); } if (type == TypeBase.BusinessWebsite) { return(BusinessWebsite.FromString(str)); } if (type is EntityType) { return(ObjectManager.GetEntityAsync((EntityType)type, str.AsUri(), false, descriptiveName).AssumeCompleted()); } if (t == typeof(TimeSpan)) { return(ParseTimeSpan(str, cult)); } if (t == typeof(bool)) { var value = ConvertFromBoolean(str, true, cult); if (value == null) { throw new InvalidDataException(string.Format("String cannot be converted to boolean: \"{0}\"", str)); } return(value.Value); } if (t == typeof(BusinessWebsite)) { return(BusinessWebsite.TryParse(str)); } var simpleType = type as SimpleType; if (simpleType != null && simpleType.InlineListItemType != null) { return(Utils.ParseInlineList(str, simpleType, inlineListSeparator)); } throw new ArgumentException("Type cannot be converted from string: " + t.ToString()); }