コード例 #1
0
        internal static RepositoryDescription FromJson(JObject json, TraceContext trace, Uri documentRoot)
        {
            Guard.NotNull(json, "json");
            Guard.NotNull(trace, "trace");

            using (trace.EnterExit())
            {
                // Read the version field
                int majorVersion      = 0;
                var majorVersionToken = json["version"];
                if (majorVersionToken != null && majorVersionToken.Type != JTokenType.Null)
                {
                    if (majorVersionToken.Type != JTokenType.Integer)
                    {
                        trace.JsonParseWarning(
                            majorVersionToken,
                            String.Format(CultureInfo.CurrentCulture, Strings.RepositoryDescription_InvalidVersion, majorVersionToken.ToDisplayString()));
                    }
                    else
                    {
                        majorVersion = majorVersionToken.Value <int>();

                        if (majorVersion < 0)
                        {
                            trace.JsonParseWarning(
                                majorVersionToken,
                                String.Format(CultureInfo.CurrentCulture, Strings.RepositoryDescription_InvalidVersion, majorVersion));
                            majorVersion = 0;
                        }
                    }
                }
                else
                {
                    trace.JsonParseWarning(
                        json,
                        String.Format(CultureInfo.CurrentCulture, Strings.RepositoryDescription_MissingExpectedProperty, "version"));
                }
                var version = new Version(majorVersion, 0);

                // Read mirrors/alternates
                // TODO: Remove old "alternates" name
                var mirrorsToken = json["mirrors"] ?? json["alternates"];
                IEnumerable <Uri> mirrors;
                if (mirrorsToken != null && mirrorsToken.Type != JTokenType.Null)
                {
                    if (mirrorsToken.Type != JTokenType.Array)
                    {
                        trace.JsonParseWarning(
                            mirrorsToken,
                            String.Format(CultureInfo.CurrentCulture, Strings.RepositoryDescription_InvalidMirrors, mirrorsToken.ToDisplayString()));
                        mirrors = Enumerable.Empty <Uri>();
                    }
                    else
                    {
                        mirrors = JsonParsing.ParseUrlArray((JArray)mirrorsToken, trace, documentRoot, Strings.RepositoryDescription_InvalidMirrorUrl);
                    }
                }
                else
                {
                    trace.JsonParseWarning(
                        json,
                        String.Format(CultureInfo.CurrentCulture, Strings.RepositoryDescription_MissingExpectedProperty, "mirrors"));
                    mirrors = Enumerable.Empty <Uri>();
                }

                // Read services
                var servicesToken = json["services"];
                IEnumerable <ServiceDescription> services;
                if (servicesToken != null && servicesToken.Type != JTokenType.Null)
                {
                    if (servicesToken.Type != JTokenType.Object)
                    {
                        trace.JsonParseWarning(
                            mirrorsToken,
                            String.Format(CultureInfo.CurrentCulture, Strings.RepositoryDescription_InvalidServices, servicesToken.ToDisplayString()));
                        services = Enumerable.Empty <ServiceDescription>();
                    }
                    else
                    {
                        services = JsonParsing.ParseUrlDictionary(json.Value <JObject>("services"), trace, documentRoot, Strings.RepositoryDescription_InvalidServiceUrl).Select(pair => new ServiceDescription(pair.Key, pair.Value));
                    }
                }
                else
                {
                    trace.JsonParseWarning(
                        json,
                        String.Format(CultureInfo.CurrentCulture, Strings.RepositoryDescription_MissingExpectedProperty, "services"));
                    services = Enumerable.Empty <ServiceDescription>();
                }

                // Create the object!
                return(new RepositoryDescription(version, mirrors, services));
            }
        }