Esempio n. 1
0
        public static Category ParseJson(JToken cat)
        {
            Category pc = new Category();

            pc.CategoryId = Json.TryGetJsonProperty(cat, "id");
            // NEW API will return these OK, old does not...
            // Debug.Assert(pc.CategoryId != null);

            pc.FullPath = Json.TryGetJsonProperty(cat, "fullpathname"); // old v1!
            pc.NodeName = Json.TryGetJsonProperty(cat, "name");

            pc.PluralName = Json.TryGetJsonProperty(cat, "pluralName"); // v2

            if (string.IsNullOrEmpty(pc.PluralName))
            {
                pc.PluralName = pc.NodeName;
            }

            var subcats = cat["categories"]; // v2, recursive

            if (subcats != null)
            {
                List <Category> sc = new List <Category>();
                foreach (var sub in subcats)
                {
                    if (sub != null)
                    {
                        Category c = Category.ParseJson(sub);
                        if (c != null)
                        {
                            sc.Add(c);
                        }
                    }
                }
                if (sc.Count > 0)
                {
                    pc._subs = sc;
                }
            }

            var parents = cat["parents"]; // old v1!

            if (parents != null)
            {
                // I wonder, HOW MULTIPLE PARENTS WOULD WORK.
                JArray prnts = (JArray)parents;
                foreach (var item in prnts)
                {
                    // V2 NOTE: THIS IS INCORRECT since its not really the full
                    // path, but instead the node.
                    pc.FullPath = item.ToString();
                    break;
                }
            }

            string primary = Json.TryGetJsonProperty(cat, "primary");

            if (primary != null && (primary == "true" || primary == "True"))
            {
                pc.IsPrimary = true;
            }

            // Since the isolated storage may still have older versions of the
            // data, this should use a fallback-mechanism.
            try
            {
                var icon = cat["icon"];
                if (icon != null)
                {
                    MultiResolutionImage mri = MultiResolutionImage.ParseJson(icon);
                    if (mri != null)
                    {
                        pc.MultiResolutionIcon = mri;
                    }
                }
            }
            catch (InvalidOperationException)
            {
                try
                {
                    string uri = Json.TryGetJsonProperty(cat, "icon");
                    if (uri != null)
                    {
                        pc._iconUri = new Uri(uri);
                    }
                }
                catch (Exception)
                {
                }
            }

            return(pc);
        }
Esempio n. 2
0
        public static Badge ParseJson(JToken badge)
        {
            Badge b = new Badge();

            b.Id      = Json.TryGetJsonProperty(badge, "id");
            b.BadgeId = Json.TryGetJsonProperty(badge, "badgeId");

            b.Name        = Json.TryGetJsonProperty(badge, "name");
            b.Hint        = Json.TryGetJsonProperty(badge, "hint");
            b.Description = Json.TryGetJsonProperty(badge, "description");

            var image = badge["image"];

            if (image != null)
            {
                try
                {
                    // TODO: Move to using MultiResolutionImage!
                    b.MultiResolutionIcon = MultiResolutionImage.ParseJson(image);

                    string prefix    = Json.TryGetJsonProperty(image, "prefix");
                    string imageName = Json.TryGetJsonProperty(image, "name");
                    var    sizes     = image["sizes"];
                    if (sizes != null)
                    {
                        List <int> szz = sizes.Select(size => int.Parse(size.ToString(), CultureInfo.InvariantCulture)).ToList();
                        b.Sizes = szz;
                        if (szz.Count > 0)
                        {
                            b.IconUri      = new Uri(prefix + szz[0] + imageName, UriKind.Absolute);
                            b.IconLargeUri = new Uri(prefix + szz[szz.Count - 1] + imageName, UriKind.Absolute);

                            if (szz.Count > 2)
                            {
                                b.IconMediumUri = new Uri(prefix + szz[1] + imageName, UriKind.Absolute);
                            }
                            else
                            {
                                // No medium-sized icon available.
                                b.IconMediumUri = b.IconUri;
                            }
                        }
                        b.ImagePrefix  = prefix;
                        b.ImagePostfix = imageName;
                    }
                }
                catch (Exception)
                {
                }
            }

            var unlocks = badge["unlocks"];

            if (unlocks != null)
            {
                List <Checkin> lc = new List <Checkin>();
                foreach (var unlock in unlocks)
                {
                    try
                    {
                        Checkin c = Checkin.ParseJson(unlock);
                        lc.Add(c);
                    }
                    catch (Exception)
                    {
                        // note: silent watchson?
                    }
                }
                b.Unlocks = lc;
            }

            return(b);
        }