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}"); }
/// <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); }
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()); }
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); }
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); } } }