Exemplo n.º 1
0
        public static IEnumerable <TargetObject> ConvertFromYaml(TargetObject targetObject, VisitTargetObject next)
        {
            // Only attempt to deserialize if the input is a string, file or URI
            if (!IsAcceptedType(targetObject))
            {
                return new TargetObject[] { targetObject }
            }
            ;

            var d = new DeserializerBuilder()
                    .IgnoreUnmatchedProperties()
                    .WithTypeConverter(new PSObjectYamlTypeConverter())
                    .WithNodeTypeResolver(new PSObjectYamlTypeResolver())
                    .Build();

            var reader = ReadAsReader(targetObject.Value, out InputFileInfo sourceInfo);
            var parser = new YamlDotNet.Core.Parser(reader);
            var result = new List <TargetObject>();

            parser.TryConsume <StreamStart>(out _);
            while (parser.Current is DocumentStart)
            {
                var item = d.Deserialize <PSObject>(parser: parser);

                if (item == null)
                {
                    continue;
                }

                result.AddRange(VisitItem(item, sourceInfo, next));
            }
            return(result.Count == 0 ? EmptyArray : result.ToArray());
        }
Exemplo n.º 2
0
        private T ParseStreamReader(StreamReader streamReader)
        {
            var deserializer = new DeserializerBuilder().Build();
            var parser       = new YamlDotNet.Core.Parser(streamReader);

            // Consume the stream start event "manually"
            parser.TryConsume <StreamStart>(out _);

            while (parser.TryConsume <DocumentStart>(out _))
            // Deserialize the document
            {
                return(deserializer.Deserialize <T>(parser));
            }

            return(null);
        }