コード例 #1
0
ファイル: Program.cs プロジェクト: aaron-meyers/yaml2json
        private static async Task Run(CliArgs args)
        {
            if (string.IsNullOrWhiteSpace(args.Target))
            {
                args.Target = Path.ChangeExtension(args.Source, ".jsonl");
            }

            long count = 0;

            using (var reader = new StreamReader(args.Source))
                using (var writer = new StreamWriter(args.Target))
                {
                    var deserializer = new DeserializerBuilder().Build();
                    var parser       = new YamlParser(reader);
                    parser.Expect <StreamStart>();
                    while (parser.Current is DocumentStart)
                    {
                        object source = deserializer.Deserialize(parser);

                        if (source != null)
                        {
                            await writer.WriteLineAsync(JsonConvert.SerializeObject(source));

                            count++;
                        }
                    }
                    parser.Expect <StreamEnd>();
                }

            Console.WriteLine($"Wrote {count} line(s) to {args.Target}");
        }
コード例 #2
0
        /// <summary>
        /// Parse Stream Reader.
        /// </summary>
        /// <param name="streamReader">Stream reader.</param>
        /// <returns>Deserialized.</returns>
        private T ParseStreamReader(StreamReader streamReader)
        {
            var deserializer = new DeserializerBuilder().Build();
            var parser       = new YamlDotNet.Core.Parser(streamReader);

            parser.Expect <StreamStart>();

            while (parser.Accept <DocumentStart>())
            {
                return(deserializer.Deserialize <T>(parser));
            }

            return(null);
        }
コード例 #3
0
ファイル: PipelineReciever.cs プロジェクト: gengle/PSRule
        public static IEnumerable <PSObject> ConvertFromYaml(PSObject sourceObject, VisitTargetObject next)
        {
            // Only attempt to deserialize if the input is a string, file or URI
            if (!IsAcceptedType(sourceObject: sourceObject))
            {
                return new PSObject[] { sourceObject }
            }
            ;

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

            var reader = ReadAsReader(sourceObject, out PSSourceInfo source);
            var parser = new YamlDotNet.Core.Parser(reader);

            parser.Expect <StreamStart>();

            var result = new List <PSObject>();

            while (parser.Accept <DocumentStart>())
            {
                var item = d.Deserialize <PSObject>(parser: parser);

                if (item == null)
                {
                    continue;
                }

                NoteSource(item, source);
                var items = next(item);

                if (items == null)
                {
                    continue;
                }

                result.AddRange(items);
            }

            if (result.Count == 0)
            {
                return(null);
            }

            return(result.ToArray());
        }
コード例 #4
0
        private static T Parse(StreamReader r)
        {
            var deserializer = new DeserializerBuilder().Build();
            var parser       = new YamlDotNet.Core.Parser(r);

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

            while (parser.Accept <DocumentStart>())
            // Deserialize the document
            {
                return(deserializer.Deserialize <T>(parser));
            }
            return(null);
        }
コード例 #5
0
        public T ParseFile(string file)
        {
            var    thisAssembly = Assembly.GetExecutingAssembly();
            string filePath     = "DeviceDetectorNET." + string.Join(".", file.Split('/'));

            using (var stream = thisAssembly.GetManifestResourceStream(filePath))
            {
                using (var r = new StreamReader(stream))
                {
                    var deserializer = new DeserializerBuilder().Build();
                    var parser       = new YamlDotNet.Core.Parser(r);

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

                    while (parser.Accept <DocumentStart>())
                    // Deserialize the document
                    {
                        return(deserializer.Deserialize <T>(parser));
                    }
                    return(null);
                }
            }
        }