예제 #1
0
        public static Photo ParseJson(JToken json)
        {
            Photo p = new Photo();

            p.Id = Json.TryGetJsonProperty(json, "id");
            //Debug.Assert(p.Id != null);

            string created = Json.TryGetJsonProperty(json, "createdAt");
            if (created != null)
            {
                DateTime dtc = UnixDate.ToDateTime(created);
                p.CreatedDateTime = dtc;
                p.Created = Checkin.GetDateString(dtc);
            }

            string primaryUri = Json.TryGetJsonProperty(json, "url");
            if (primaryUri != null)
            {
                p.Uri = new Uri(primaryUri, UriKind.Absolute);
            }

            var userJson = json["user"];
            if (userJson != null)
            {
                p.User = CompactUser.ParseJson(userJson);
                if (p.User != null)
                {
                    p.IsSelf = p.User.Relationship == FriendStatus.Self;
                }
            }

            var sizes = json["sizes"];
            if (sizes != null)
            {
                var items = sizes["items"];
                List<UriWidthHeight> sz = new List<UriWidthHeight>();
                foreach (var item in items)
                {
                    sz.Add(new UriWidthHeight
                    {
                        Uri = new Uri(Json.TryGetJsonProperty(item, "url"), UriKind.Absolute),
                        Width = double.Parse(Json.TryGetJsonProperty(item, "width"), CultureInfo.InvariantCulture),
                        Height = double.Parse(Json.TryGetJsonProperty(item, "height"), CultureInfo.InvariantCulture),
                    });
                }
                if (sz.Count > 0)
                {
                    p.SmallestUri = sz[sz.Count - 1].Uri;
                    p.MediumUri = sz[sz.Count > 2 ? sz.Count - 2 : sz.Count - 1].Uri;
                    p.LargerUri = sz[1].Uri;
                }
            }

            Uri pu = new Uri(
                string.Format(
                CultureInfo.InvariantCulture,
                "/Views/PhotoViewer.xaml?photoUri={0}&isSelf={1}&id={2}",

                Uri.EscapeDataString(p.Uri.ToString()),
                p.IsSelf ? "true" : "false",
                p.Id
                )
                , UriKind.Relative);
            p.LocalPhotoViewerUri = pu;

            return p;
        }