private YamlSerializer(YamlSerializationOptions options = null) { _globals = options ?? new YamlSerializationOptions(); _writer = new YamlWriter(); _depth = 0; _allowInline = AllowInline.Nothing; Start(); }
private void SerializeItem(object item, bool list = false, string key = null, IEnumerable <Attribute> extraAttrs = null, object commentSource = null) { _options = new CombinedOptions(_globals) .OverrideWith(CustomAttributeExtensions.GetCustomAttributes(item.GetType(), true)) .OverrideWith(extraAttrs); if (_options.Ignores.Count > 0 && IsIgnored(item, _options.Ignores)) { return; } if ((_allowInline == AllowInline.Anything || (_allowInline == AllowInline.PlainValues && !list && key == null)) && _options.BlankLinesBefore == 0 && !_options.Comments.Any(attr => IsCommentBefore(attr))) { // Do not start a new line } else { _writer.AppendBlankLinesBefore(_options.BlankLinesBefore); AppendComments(commentSource ?? item, before: true); _writer.StartNewLine(); } if (list) { _writer.AppendSequenceItemPrefix(); _allowInline = _options.AlwaysNested ? AllowInline.Nothing : AllowInline.Anything; } else if (key != null) { SerializeString(_options.Name ?? key, true); _writer.AppendKeyValueSeparator(); _allowInline = AllowInline.PlainValues; } if (item is string) { SerializeString((string)item, false); } else if (item is bool) { SerializeBool((bool)item); } else if (item.GetType().IsPrimitive) { SerializeString(item.ToString(), false); } else { if (++_depth > _globals.MaxDepth) { throw new YamlSerializationTooDeep(); } bool nextIsList = (item is IEnumerable) && !(item is IDictionary); bool needsIndent = (list || ((key != null) && !nextIsList)); if (needsIndent) { _writer.Indent(_options.IndentStep); } var prevOptions = _options; if (item is IDictionary) { SerializeDictionary((IDictionary)item); } else if (item is IEnumerable) { SerializeList((IEnumerable)item); } else { SerializeObject(item); } _options = prevOptions; if (needsIndent) { _writer.Undent(); } --_depth; } AppendComments(commentSource ?? item, after: true); _writer.AppendBlankLinesAfter(_options.BlankLinesAfter); _allowInline = AllowInline.Nothing; }