/// <summary> /// Writes this object's state to a YAML emitter. /// </summary> /// <param name="emitter"></param> void IYamlSerializable.WriteYaml(Emitter emitter) { foreach (var item in events) { emitter.Emit(item); } }
public CustomSerializationObjectGraphVisitor(Emitter emitter, IObjectGraphVisitor nextVisitor, IEnumerable<IYamlTypeConverter> typeConverters) : base(nextVisitor) { this.emitter = emitter; this.typeConverters = typeConverters != null ? typeConverters.ToList() : Enumerable.Empty<IYamlTypeConverter>(); }
public void Serialize(MapNode node, Emitter emitter) { emitter.Emit(new MappingStart()); SerializeScalarProperties(node, emitter); emitter.Emit(new MappingEnd()); }
public void Execute(ITextView textView, ITextUndoHistoryRegistry textUndoHistoryRegistry) { var undoHistory = textUndoHistoryRegistry.RegisterHistory(textView); using (var transaction = undoHistory.CreateTransaction("Format Document")) { var text = textView.TextBuffer.CurrentSnapshot.GetText(); var formatted = new StringWriter(); var parser = new Parser(new Scanner(new StringReader(text), skipComments: false)); var emitter = new Emitter(formatted); while (parser.MoveNext()) { emitter.Emit(parser.Current); } var edit = textView.TextBuffer.CreateEdit(); edit.Replace(0, text.Length, formatted.ToString()); edit.Apply(); transaction.Complete(); } }
public void UnicodeInScalarsCanBeSingleQuotedWhenOutputEncodingSupportsIt(string text, int codePage) { var document = StreamedDocumentWith( SequenceWith( SingleQuotedScalar(text) ) ); var buffer = new MemoryStream(); var encoding = Encoding.GetEncoding(codePage); using (var writer = new StreamWriter(buffer, encoding)) { var emitter = new Emitter(writer, 2, int.MaxValue, false); foreach (var evt in document) { emitter.Emit(evt); } } var yaml = encoding.GetString(buffer.ToArray()); yaml.Should() .Contain("'" + text + "'"); }
public WriterEventEmitter(Emitter emitter) { this.emitter = emitter; }
/// <summary> /// Saves the current node to the specified emitter. /// </summary> /// <param name="emitter">The emitter where the node is to be saved.</param> /// <param name="state">The state.</param> internal override void Emit(Emitter emitter, EmitterState state) { throw new NotSupportedException("A YamlAliasNode is an implementation detail and should never be saved."); }
private void EmitDocument(Emitter emitter, IObjectGraphTraversalStrategy traversalStrategy, IObjectGraphVisitor emittingVisitor, object graph, Type type) { emitter.Emit(new StreamStart()); emitter.Emit(new DocumentStart()); traversalStrategy.Traverse(graph, type, emittingVisitor); emitter.Emit(new DocumentEnd(true)); emitter.Emit(new StreamEnd()); }
private IEventEmitter CreateEventEmitter(Emitter emitter, SerializationOptions options) { var writer = new WriterEventEmitter(emitter); if ((options & SerializationOptions.JsonCompatible) != 0) { return new JsonEventEmitter(writer); } else { return new TypeAssigningEventEmitter(writer); } }
private IObjectGraphVisitor CreateEmittingVisitor(Emitter emitter, SerializationOptions options, IObjectGraphTraversalStrategy traversalStrategy, IEventEmitter eventEmitter, object graph, Type type) { IObjectGraphVisitor emittingVisitor = new EmittingObjectGraphVisitor(eventEmitter); emittingVisitor = new CustomSerializationObjectGraphVisitor(emitter, emittingVisitor, converters); if ((options & SerializationOptions.DisableAliases) == 0) { var anchorAssigner = new AnchorAssigner(); traversalStrategy.Traverse(graph, type, anchorAssigner); emittingVisitor = new AnchorAssigningObjectGraphVisitor(emittingVisitor, eventEmitter, anchorAssigner); } if ((options & SerializationOptions.EmitDefaults) == 0) { emittingVisitor = new DefaultExclusiveObjectGraphVisitor(emittingVisitor); } return emittingVisitor; }
/// <summary> /// Serializes the specified object. /// </summary> /// <param name="emitter">The <see cref="Emitter" /> where to serialize the object.</param> /// <param name="graph">The object to serialize.</param> /// <param name="options">Options that control how the serialization is to be performed.</param> public void Serialize(Emitter emitter, object graph, SerializationOptions options = SerializationOptions.None) { Serialize(emitter, graph, graph != null ? graph.GetType() : typeof(object), options); }
internal void Save(Emitter emitter) { AssignAnchors(); emitter.Emit(new DocumentStart()); RootNode.Save(emitter, new EmitterState()); emitter.Emit(new DocumentEnd(false)); }
private void ParseAndEmit(string name) { string testText = YamlFile(name).ReadToEnd(); Parser parser = new Parser(new StringReader(testText)); using(StringWriter output = new StringWriter()) { Emitter emitter = new Emitter(output, 2, int.MaxValue, false); while(parser.MoveNext()) { //Console.WriteLine(parser.Current.GetType().Name); Console.Error.WriteLine(parser.Current); emitter.Emit(parser.Current); } string result = output.ToString(); Console.WriteLine(); Console.WriteLine("------------------------------"); Console.WriteLine(); Console.WriteLine(testText); Console.WriteLine(); Console.WriteLine("------------------------------"); Console.WriteLine(); Console.WriteLine(result); Console.WriteLine(); Console.WriteLine("------------------------------"); Console.WriteLine(); /* Parser resultParser = new Parser(new StringReader(result)); while(resultParser.MoveNext()) { Console.WriteLine(resultParser.Current.GetType().Name); } */ /* if(testText != result) { Console.WriteLine(); Console.WriteLine("------------------------------"); Console.WriteLine(); Console.WriteLine("Expected:"); Console.WriteLine(); Console.WriteLine(testText); Console.WriteLine(); Console.WriteLine("------------------------------"); Console.WriteLine(); Console.WriteLine("Result:"); Console.WriteLine(); Console.WriteLine(result); Console.WriteLine(); Console.WriteLine("------------------------------"); Console.WriteLine(); } Assert.AreEqual(testText, result, "The emitter did not generate the correct text."); */ } }
/// <summary> /// Saves the current node to the specified emitter. /// </summary> /// <param name="emitter">The emitter where the node is to be saved.</param> /// <param name="state">The state.</param> internal override void Emit(Emitter emitter, EmitterState state) { emitter.Emit(new Scalar(Anchor, Tag, Value, Style, true, false)); }
public void Serialize(MetaModel.MetaModel metaModel, TextWriter writer) { Emitter emitter = new Emitter(writer); emitter.Emit(new StreamStart()); emitter.Emit(new DocumentStart()); emitter.Emit(new MappingStart()); //icons emitter.Emit(new Scalar(Icons)); emitter.Emit(new SequenceStart(null, null, true, SequenceStyle.Block)); foreach (var modelIcon in metaModel.IconsList) { emitter.Emit(new MappingStart()); emitter.Emit(new Scalar(Name)); emitter.Emit(new Scalar(modelIcon.Name)); emitter.Emit(new Scalar(Title)); emitter.Emit(new Scalar(modelIcon.Title)); emitter.Emit(new Scalar(Shortcut)); emitter.Emit(new Scalar(modelIcon.Shortcut)); emitter.Emit(new MappingEnd()); } emitter.Emit(new SequenceEnd()); //recent files if (metaModel.RecentFiles.Count > 0) { emitter.Emit(new Scalar(RecentFiles)); emitter.Emit(new SequenceStart(null, null, true, SequenceStyle.Block)); foreach (var recentFile in metaModel.RecentFiles) { emitter.Emit(new Scalar(recentFile)); } emitter.Emit(new SequenceEnd()); } //last opened file if (metaModel.LastOpenedFile != null) { emitter.Emit(new Scalar(LastOpenedFile)); emitter.Emit(new Scalar(metaModel.LastOpenedFile)); } //map back color emitter.Emit(new Scalar(MapBackColor)); emitter.Emit(new Scalar(Convert.ToColorHexValue(metaModel.MapEditorBackColor))); //note back color emitter.Emit(new Scalar(NoteBackColor)); emitter.Emit(new Scalar(Convert.ToColorHexValue(metaModel.NoteEditorBackColor))); //node styles if (metaModel.NodeStyles.Any()) { emitter.Emit(new Scalar(NodeStyles)); emitter.Emit(new SequenceStart(null, null, true, SequenceStyle.Block)); foreach (var nodeStyle in metaModel.NodeStyles) { emitter.Emit(new MappingStart()); emitter.Emit(new Scalar(Title)); emitter.Emit(new Scalar(nodeStyle.Title)); emitter.Emit(new Scalar(RefNode)); new MapYamlSerializer().Serialize(nodeStyle.RefNode, emitter); emitter.Emit(new MappingEnd()); } emitter.Emit(new SequenceEnd()); } emitter.Emit(new MappingEnd()); emitter.Emit(new DocumentEnd(true)); emitter.Emit(new StreamEnd()); }
/// <summary> /// Saves the current node to the specified emitter. /// </summary> /// <param name="emitter">The emitter where the node is to be saved.</param> /// <param name="state">The state.</param> internal abstract void Emit(Emitter emitter, EmitterState state);
/// <summary> /// Saves the current node to the specified emitter. /// </summary> /// <param name="emitter">The emitter where the node is to be saved.</param> /// <param name="state">The state.</param> internal void Save(Emitter emitter, EmitterState state) { if (!string.IsNullOrEmpty(Anchor) && !state.EmittedAnchors.Add(Anchor)) { emitter.Emit(new AnchorAlias(Anchor)); } else { Emit(emitter, state); } }
/// <summary> /// Serializes the specified object. /// </summary> /// <param name="emitter">The <see cref="Emitter" /> where to serialize the object.</param> /// <param name="graph">The object to serialize.</param> /// <param name="type">The static type of the object to serialize.</param> /// <param name="options">Options that control how the serialization is to be performed.</param> public void Serialize(Emitter emitter, object graph, Type type, SerializationOptions options = SerializationOptions.None) { if (emitter == null) { throw new ArgumentNullException("emitter"); } if (type == null) { throw new ArgumentNullException("type"); } var traversalStrategy = CreateTraversalStrategy(options); var eventEmitter = CreateEventEmitter(emitter, options); var emittingVisitor = CreateEmittingVisitor(emitter, options, traversalStrategy, eventEmitter, graph, type); EmitDocument(emitter, traversalStrategy, emittingVisitor, graph, type); }