Exemplo n.º 1
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var obj = JObject.Load(reader);

            // Get package name
            var name = obj["Name"].Value <string>();

            // Try get package version
            var     versionString = obj["Version"].Value <string>();
            Version version;

            if (!Version.TryParse(versionString, out version))
            {
                logger.LogWarning(
                    string.Format("The version of Package Dependency {0} could not be deserialized.", name),
                    Logging.WarningLevel.Moderate);
            }

            // Create new PackageDependencyInfo
            var packageInfo = new PackageDependencyInfo(name, version);

            // Try get dependent node IDs
            var nodes = obj["Nodes"].Values <string>();

            foreach (var nodeID in nodes)
            {
                Guid guid;
                if (!Guid.TryParse(nodeID, out guid))
                {
                    logger.LogWarning(
                        string.Format("The id ({0}) of a node dependent on {1} could not be parsed as a GUID.", nodeID, name),
                        Logging.WarningLevel.Moderate);
                }
                else
                {
                    packageInfo.AddDependent(guid);
                }
            }
            return(packageInfo);
        }
Exemplo n.º 2
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var obj = JObject.Load(reader);

            // Get dependency name
            var name = obj["Name"].Value <string>();

            // Try get dependency version
            var     versionString = obj["Version"].Value <string>();
            Version version;

            if (!Version.TryParse(versionString, out version))
            {
                logger.LogWarning(
                    string.Format("The version of Package Dependency {0} could not be deserialized.", name),
                    Logging.WarningLevel.Moderate);
            }

            //default to package.
            ReferenceType parsedType = ReferenceType.Package;
            JToken        referenceTypeToken;

            if (obj.TryGetValue(ReferenceTypePropString, out referenceTypeToken))
            {
                var referenceTypeString = referenceTypeToken.Value <string>();
                if (!Enum.TryParse <ReferenceType>(referenceTypeString, out parsedType))
                {
                    logger.LogWarning(
                        string.Format("The ReferenceType of Dependency {0} could not be deserialized.", name),
                        Logging.WarningLevel.Moderate);
                }
            }

            INodeLibraryDependencyInfo depInfo;

            //select correct constructor based on referenceType
            switch (parsedType)
            {
            case ReferenceType.Package:
                depInfo = new PackageDependencyInfo(name, version);
                break;

            /*TODO add other cases: for example
             * case ReferenceType.ZeroTouch
             * depInfp = new ZeroTouchDependencyInfo(name,version)
             */
            default:
                depInfo = new PackageDependencyInfo(name, version);
                break;
            }



            // Try get dependent node IDs
            var nodes = obj["Nodes"].Values <string>();

            foreach (var nodeID in nodes)
            {
                Guid guid;
                if (!Guid.TryParse(nodeID, out guid))
                {
                    logger.LogWarning(
                        string.Format("The id ({0}) of a node dependent on {1} could not be parsed as a GUID.", nodeID, name),
                        Logging.WarningLevel.Moderate);
                }
                else
                {
                    depInfo.AddDependent(guid);
                }
            }
            return(depInfo);
        }