/// <summary> /// Initializes a new instance of the <see cref="YamlSequenceNode"/> class. /// </summary> /// <param name="events">The events.</param> /// <param name="state">The state.</param> internal YamlSequenceNode(EventReader events, DocumentLoadingState state) { SequenceStart sequence = events.Expect <SequenceStart>(); Load(sequence, state); Style = sequence.Style; bool hasUnresolvedAliases = false; while (!events.Accept <SequenceEnd>()) { YamlNode child = ParseNode(events, state); children.Add(child); hasUnresolvedAliases |= child is YamlAliasNode; } if (hasUnresolvedAliases) { state.AddNodeWithUnresolvedAliases(this); } #if DEBUG else { foreach (var child in children) { if (child is YamlAliasNode) { throw new InvalidOperationException("Error in alias resolution."); } } } #endif events.Expect <SequenceEnd>(); }
/// <summary> /// The default function to write an object to Yaml /// </summary> public void WriteYaml(object value, Type expectedType, YamlStyle style = YamlStyle.Any) { var objectContext = new ObjectContext(this, value, FindTypeDescriptor(expectedType)) { Style = style }; ObjectSerializer.WriteYaml(ref objectContext); }
/// <summary> /// Initializes a new instance of the <see cref="YamlMappingNode"/> class. /// </summary> /// <param name="events">The events.</param> /// <param name="state">The state.</param> internal YamlMappingNode(EventReader events, DocumentLoadingState state) { MappingStart mapping = events.Expect <MappingStart>(); Load(mapping, state); Style = mapping.Style; bool hasUnresolvedAliases = false; while (!events.Accept <MappingEnd>()) { YamlNode key = ParseNode(events, state); YamlNode value = ParseNode(events, state); try { children.Add(key, value); } catch (ArgumentException err) { throw new YamlException(key.Start, key.End, "Duplicate key", err); } hasUnresolvedAliases |= key is YamlAliasNode || value is YamlAliasNode; } if (hasUnresolvedAliases) { state.AddNodeWithUnresolvedAliases(this); } #if DEBUG else { foreach (var child in children) { if (child.Key is YamlAliasNode) { throw new InvalidOperationException("Error in alias resolution."); } if (child.Value is YamlAliasNode) { throw new InvalidOperationException("Error in alias resolution."); } } } #endif events.Expect <MappingEnd>(); }
/// <summary> /// Initializes a new instance of the <see cref="ObjectDescriptor" /> class. /// </summary> /// <param name="attributeRegistry">The attribute registry.</param> /// <param name="type">The type.</param> /// <param name="emitDefaultValues"></param> /// <exception cref="System.ArgumentNullException">type</exception> /// <exception cref="YamlException">Failed to get ObjectDescriptor for type [{0}]. The member [{1}] cannot be registered as a member with the same name is already registered [{2}].DoFormat(type.FullName, member, existingMember)</exception> public ObjectDescriptor(IAttributeRegistry attributeRegistry, Type type, bool emitDefaultValues) { if (attributeRegistry == null) { throw new ArgumentNullException("attributeRegistry"); } if (type == null) { throw new ArgumentNullException("type"); } this.emitDefaultValues = emitDefaultValues; this.AttributeRegistry = attributeRegistry; this.type = type; var styleAttribute = AttributeRegistry.GetAttribute <YamlStyleAttribute>(type); this.style = styleAttribute != null ? styleAttribute.Style : YamlStyle.Any; this.IsCompilerGenerated = AttributeRegistry.GetAttribute <CompilerGeneratedAttribute>(type) != null; }
/// <summary> /// Initializes a new instance of the <see cref="ObjectDescriptor" /> class. /// </summary> /// <param name="attributeRegistry">The attribute registry.</param> /// <param name="type">The type.</param> /// <param name="emitDefaultValues">if set to <c>true</c> [emit default values].</param> /// <param name="ignoreGetters">If set to <c>true</c>, the properties without setters will be ignored.</param> /// <param name="namingConvention">The naming convention.</param> /// <exception cref="System.ArgumentNullException">type</exception> /// <exception cref="YamlException">type</exception> public ObjectDescriptor(IAttributeRegistry attributeRegistry, Type type, bool emitDefaultValues, bool ignoreGetters, IMemberNamingConvention namingConvention) { if (attributeRegistry == null) { throw new ArgumentNullException("attributeRegistry"); } if (type == null) { throw new ArgumentNullException("type"); } if (namingConvention == null) { throw new ArgumentNullException("namingConvention"); } this.memberNamingConvention = namingConvention; this.emitDefaultValues = emitDefaultValues; this.ignoreGetters = ignoreGetters; this.AttributeRegistry = attributeRegistry; this.type = type; attributes = AttributeRegistry.GetAttributes(type.GetTypeInfo()); this.style = YamlStyle.Any; foreach (var attribute in attributes) { var styleAttribute = attribute as YamlStyleAttribute; if (styleAttribute != null) { style = styleAttribute.Style; continue; } if (attribute is CompilerGeneratedAttribute) { this.IsCompilerGenerated = true; } } }
/// <summary> /// Initializes a new instance of the <see cref="ObjectDescriptor" /> class. /// </summary> /// <param name="attributeRegistry">The attribute registry.</param> /// <param name="type">The type.</param> /// <param name="emitDefaultValues"></param> /// <exception cref="System.ArgumentNullException">type</exception> /// <exception cref="YamlException">Failed to get ObjectDescriptor for type [{0}]. The member [{1}] cannot be registered as a member with the same name is already registered [{2}].DoFormat(type.FullName, member, existingMember)</exception> public ObjectDescriptor(IAttributeRegistry attributeRegistry, Type type, bool emitDefaultValues) { if (attributeRegistry == null) throw new ArgumentNullException("attributeRegistry"); if (type == null) throw new ArgumentNullException("type"); this.emitDefaultValues = emitDefaultValues; this.AttributeRegistry = attributeRegistry; this.type = type; var styleAttribute = AttributeRegistry.GetAttribute<YamlStyleAttribute>(type); this.style = styleAttribute != null ? styleAttribute.Style : YamlStyle.Any; this.IsCompilerGenerated = AttributeRegistry.GetAttribute<CompilerGeneratedAttribute>(type) != null; members = PrepareMembers(); // If no members found, we don't need to build a dictionary map if (members.Count <= 0) return; mapMembers = new Dictionary<string, IMemberDescriptor>(members.Count); foreach (var member in members) { IMemberDescriptor existingMember; if (mapMembers.TryGetValue(member.Name, out existingMember)) { throw new YamlException("Failed to get ObjectDescriptor for type [{0}]. The member [{1}] cannot be registered as a member with the same name is already registered [{2}]".DoFormat(type.FullName, member, existingMember)); } mapMembers.Add(member.Name, member); } }
/// <summary> /// Initializes a new instance of the <see cref="MappingStart"/> class. /// </summary> /// <param name="anchor">The anchor.</param> /// <param name="tag">The tag.</param> /// <param name="isImplicit">Indicates whether the event is implicit.</param> /// <param name="style">The style of the mapping.</param> public MappingStart(string anchor, string tag, bool isImplicit, YamlStyle style) : this(anchor, tag, isImplicit, style, Mark.Empty, Mark.Empty) { }
/// <summary> /// Initializes a new instance of the <see cref="MappingStart"/> class. /// </summary> /// <param name="anchor">The anchor.</param> /// <param name="tag">The tag.</param> /// <param name="isImplicit">Indicates whether the event is implicit.</param> /// <param name="style">The style of the mapping.</param> /// <param name="start">The start position of the event.</param> /// <param name="end">The end position of the event.</param> public MappingStart(string anchor, string tag, bool isImplicit, YamlStyle style, Mark start, Mark end) : base(anchor, tag, start, end) { this.isImplicit = isImplicit; this.style = style; }
/// <summary> /// Initializes a new instance of the <see cref="SequenceStart"/> class. /// </summary> public SequenceStart(string anchor, string tag, bool isImplicit, YamlStyle style) : this(anchor, tag, isImplicit, style, Mark.Empty, Mark.Empty) { }
/// <summary> /// Initializes a new instance of the <see cref="SequenceStart"/> class. /// </summary> /// <param name="anchor">The anchor.</param> /// <param name="tag">The tag.</param> /// <param name="isImplicit">if set to <c>true</c> [is implicit].</param> /// <param name="style">The style.</param> /// <param name="start">The start position of the event.</param> /// <param name="end">The end position of the event.</param> public SequenceStart(string anchor, string tag, bool isImplicit, YamlStyle style, Mark start, Mark end) : base(anchor, tag, start, end) { this.isImplicit = isImplicit; this.style = style; }
/// <summary> /// Initializes a new instance of the <see cref="YamlStyleAttribute"/> class. /// </summary> /// <param name="style">The style.</param> public YamlStyleAttribute(YamlStyle style) { this.style = style; }
/// <summary> /// Initializes a new instance of the <see cref="ObjectDescriptor" /> class. /// </summary> /// <param name="attributeRegistry">The attribute registry.</param> /// <param name="type">The type.</param> /// <param name="emitDefaultValues">if set to <c>true</c> [emit default values].</param> /// <param name="namingConvention">The naming convention.</param> /// <exception cref="System.ArgumentNullException">type</exception> /// <exception cref="YamlException">type</exception> public ObjectDescriptor(IAttributeRegistry attributeRegistry, Type type, bool emitDefaultValues, IMemberNamingConvention namingConvention) { if (attributeRegistry == null) throw new ArgumentNullException("attributeRegistry"); if (type == null) throw new ArgumentNullException("type"); if (namingConvention == null) throw new ArgumentNullException("namingConvention"); this.memberNamingConvention = namingConvention; this.emitDefaultValues = emitDefaultValues; this.AttributeRegistry = attributeRegistry; this.type = type; var attributes = AttributeRegistry.GetAttributes(type); this.style = YamlStyle.Any; foreach (var attribute in attributes) { var styleAttribute = attribute as YamlStyleAttribute; if (styleAttribute != null) { style = styleAttribute.Style; continue; } if (attribute is CompilerGeneratedAttribute) { this.IsCompilerGenerated = true; } } }
/// <summary> /// Initializes a new instance of the <see cref="ObjectDescriptor" /> class. /// </summary> /// <param name="attributeRegistry">The attribute registry.</param> /// <param name="type">The type.</param> /// <param name="emitDefaultValues">if set to <c>true</c> [emit default values].</param> /// <param name="namingConvention">The naming convention.</param> /// <exception cref="System.ArgumentNullException">type</exception> /// <exception cref="YamlException">type</exception> public ObjectDescriptor(IAttributeRegistry attributeRegistry, Type type, bool emitDefaultValues, IMemberNamingConvention namingConvention) { if (attributeRegistry == null) throw new ArgumentNullException("attributeRegistry"); if (type == null) throw new ArgumentNullException("type"); if (namingConvention == null) throw new ArgumentNullException("namingConvention"); this.memberNamingConvention = namingConvention; this.emitDefaultValues = emitDefaultValues; this.AttributeRegistry = attributeRegistry; this.type = type; var styleAttribute = AttributeRegistry.GetAttribute<YamlStyleAttribute>(type); this.style = styleAttribute != null ? styleAttribute.Style : YamlStyle.Any; this.IsCompilerGenerated = AttributeRegistry.GetAttribute<CompilerGeneratedAttribute>(type) != null; }
/// <summary> /// Initializes a new instance of the <see cref="ObjectDescriptor" /> class. /// </summary> /// <param name="attributeRegistry">The attribute registry.</param> /// <param name="type">The type.</param> /// <param name="emitDefaultValues"></param> /// <exception cref="System.ArgumentNullException">type</exception> /// <exception cref="YamlException">Failed to get ObjectDescriptor for type [{0}]. The member [{1}] cannot be registered as a member with the same name is already registered [{2}].DoFormat(type.FullName, member, existingMember)</exception> public ObjectDescriptor(IAttributeRegistry attributeRegistry, Type type, bool emitDefaultValues) { if (attributeRegistry == null) throw new ArgumentNullException("attributeRegistry"); if (type == null) throw new ArgumentNullException("type"); this.emitDefaultValues = emitDefaultValues; this.AttributeRegistry = attributeRegistry; this.type = type; var styleAttribute = AttributeRegistry.GetAttribute<YamlStyleAttribute>(type); this.style = styleAttribute != null ? styleAttribute.Style : YamlStyle.Any; this.IsCompilerGenerated = AttributeRegistry.GetAttribute<CompilerGeneratedAttribute>(type) != null; var memberList = PrepareMembers(); // Sort members by name // This is to make sure that properties/fields for an object // are always displayed in the same order //memberList.Sort(SortMembers); // Free the member list this.members = memberList.ToArray(); // If no members found, we don't need to build a dictionary map if (members.Length <= 0) return; mapMembers = new Dictionary<string, IMemberDescriptor>(members.Length); foreach (var member in members) { IMemberDescriptor existingMember; if (mapMembers.TryGetValue(member.Name, out existingMember)) { throw new YamlException("Failed to get ObjectDescriptor for type [{0}]. The member [{1}] cannot be registered as a member with the same name is already registered [{2}]".DoFormat(type.FullName, member, existingMember)); } mapMembers.Add(member.Name, member); } }