Example #1
0
File: AVC.cs Project: skaven81/CKAN
        /// <summary>
        /// Inflates metadata with KSP Version information read from AVC.
        /// </summary>
        override public void InflateMetadata(JObject metadata, string unused_filename, object unused_context)
        {
            log.Debug("Inflating from contained AVC data...");

            // The CKAN spec states that only a KSP version can be supplied, *or* a min/max can
            // be provided. Since min/max are more descriptive, we check and use them first.
            if (ksp_version_min != null || ksp_version_max != null)
            {
                log.Debug("Inflating ksp min/max");
                metadata.Remove("ksp_version"); // In case it's there from KS
                if (ksp_version_min != null)
                {
                    Inflate(metadata, "ksp_version_min", ksp_version_min.ToString());
                }
                if (ksp_version_max != null)
                {
                    Inflate(metadata, "ksp_version_max", ksp_version_max.ToString());
                }
            }
            else if (ksp_version != null)
            {
                log.Debug("Inflating ksp_version");
                Inflate(metadata, "ksp_version", ksp_version.ToString());
            }

            // It's cool if we don't have version info at all, it's optional in the AVC spec.
        }
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            string major = "0";
            string minor = "0";
            string patch = "0";

            JToken token = JToken.Load(reader);

            log.DebugFormat("Read Token: {0}, {1}", new Object[] { token.Type, token.ToString() });
            if (token.Type == JTokenType.String)
            {
                string[] tokenArray = token.ToString().Split('.');

                if (tokenArray.Length >= 0)
                {
                    major = tokenArray [0];
                }

                if (tokenArray.Length >= 1)
                {
                    minor = tokenArray [1];
                }

                if (tokenArray.Length >= 2)
                {
                    patch = tokenArray [2];
                }
            }
            else if (token.Type == JTokenType.Object)
            {
                major = (string)token ["MAJOR"];
                minor = (string)token ["MINOR"];
                patch = (string)token ["PATCH"];
            }
            else
            {
                throw new InvalidCastException("Trying to convert non-JSON object to Version object");
            }

            string version = string.Join(".", major, minor, patch);

            log.DebugFormat("  extracted version: {0}", version);
            KSPVersion result = new KSPVersion(version);

            log.DebugFormat("  generated result: {0}", result.ToString());
            return(result);
        }