summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Pages/JsonSourceConverter.cs
blob: 461c96d9138ce11bb8dc38c2165a9504c0cfcab5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using System;

namespace Xamarin.Forms.Pages
{
	public class JsonSourceConverter : TypeConverter
	{
		public override object ConvertFromInvariantString(string value)
		{
			if (value != null)
			{
				value = value.Trim();
				Uri uri;
				if (Uri.TryCreate(value, UriKind.Absolute, out uri) && uri.Scheme != "file")
					return new UriJsonSource { Uri = uri };
				if (value.StartsWith("[") || value.StartsWith("{"))
					return new StringJsonSource { Json = value };
			}

			throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" into {1}", value, typeof(JsonSource)));
		}
	}
}