/// <summary> /// Initializes a new instance of the <see cref="CollectionDescriptor" /> 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.ArgumentException">Expecting a type inheriting from System.Collections.ICollection;type</exception> public CollectionDescriptor(IAttributeRegistry attributeRegistry, Type type, bool emitDefaultValues, bool respectPrivateSetters, IMemberNamingConvention namingConvention) : base(attributeRegistry, type, emitDefaultValues, respectPrivateSetters, namingConvention) { if (!IsCollection(type)) { throw new ArgumentException("Expecting a type inheriting from System.Collections.ICollection", "type"); } // Gets the element type var collectionType = type.GetInterface(typeof(IEnumerable <>)); ElementType = (collectionType != null) ? collectionType.GetGenericArguments()[0] : typeof(object); // implements ICollection<T> Type itype; if ((itype = type.GetInterface(typeof(ICollection <>))) != null) { var add = itype.GetMethod("Add", new[] { ElementType }); CollectionAddFunction = (obj, value) => add.Invoke(obj, new[] { value }); var countMethod = itype.GetProperty("Count").GetGetMethod(); GetCollectionCountFunction = o => (int)countMethod.Invoke(o, null); var isReadOnly = itype.GetProperty("IsReadOnly").GetGetMethod(); IsReadOnlyFunction = obj => (bool)isReadOnly.Invoke(obj, null); isKeyedCollection = type.ExtendsGeneric(typeof(KeyedCollection <,>)); } // implements IList else if (typeof(IList).IsAssignableFrom(type)) { CollectionAddFunction = (obj, value) => ((IList)obj).Add(value); GetCollectionCountFunction = o => ((IList)o).Count; IsReadOnlyFunction = obj => ((IList)obj).IsReadOnly; } }
/// <summary> /// Initializes a new instance of the <see cref="SerializerSettings" /> class. /// </summary> public SerializerSettings(IYamlSchema schema) { PreferredIndent = 2; IndentLess = false; EmitAlias = true; EmitTags = true; AllowErrors = false; ResetAlias = true; SortKeyForMapping = true; EmitJsonComptible = false; EmitCapacityForList = false; SpecialCollectionMember = "~Items"; LimitPrimitiveFlowSequence = 0; DefaultStyle = YamlStyle.Block; this.schema = schema ?? new CoreSchema(); AssemblyRegistry = new AssemblyRegistry(Schema); attributeRegistry = new AttributeRegistry(); ObjectFactory = new DefaultObjectFactory(); ObjectSerializerBackend = new DefaultObjectSerializerBackend(); ComparerForKeySorting = new DefaultKeyComparer(); NamingConvention = new DefaultNamingConvention(); // Register default mapping for map and seq AssemblyRegistry.RegisterTagMapping("!!map", typeof(IDictionary <object, object>), false); AssemblyRegistry.RegisterTagMapping("!!seq", typeof(IList <object>), false); }
/// <summary> /// Initializes a new instance of the <see cref="GameSystem" /> class. /// </summary> /// <param name="game">The game.</param> public YamlManager(Game game) : base(game) { Services.AddService(this); yamlSettings = new SerializerSettings(); attributeRegistry = yamlSettings.Attributes; serializer = CreateSerializer(yamlSettings); }
/// <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; 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="GameSystem" /> class. /// </summary> /// <param name="game">The game.</param> public YamlManager(Game game) : base(game) { Services.AddService(this); yamlSettings = new SerializerSettings(); attributeRegistry = yamlSettings.Attributes; serializer = new Serializer(yamlSettings); }
/// <summary> /// Initializes a new instance of the <see cref="GameSystem" /> class. /// </summary> /// <param name="registry">The registry.</param> public YamlManager(IServiceRegistry registry) : base(registry) { Services.AddService(this); yamlSettings = new SerializerSettings(); attributeRegistry = yamlSettings.Attributes; serializer = new Serializer(yamlSettings); }
/// <summary> /// Initializes a new instance of the <see cref="CollectionDescriptor" /> 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.ArgumentException">Expecting a type inheriting from System.Collections.ICollection;type</exception> public CollectionDescriptor(IAttributeRegistry attributeRegistry, Type type, bool emitDefaultValues, IMemberNamingConvention namingConvention) : base(attributeRegistry, type, emitDefaultValues, namingConvention) { if (!IsCollection(type)) throw new ArgumentException("Expecting a type inheriting from System.Collections.ICollection", "type"); // Gets the element type var collectionType = type.GetInterface(typeof(IEnumerable<>)); ElementType = (collectionType != null) ? collectionType.GetGenericArguments()[0] : typeof(object); // implements ICollection<T> Type itype; if ((itype = type.GetInterface(typeof(ICollection<>))) != null) { var add = itype.GetMethod("Add", new [] { ElementType }); CollectionAddFunction = (obj, value) => add.Invoke(obj, new [] { value }); var countMethod = itype.GetProperty("Count").GetGetMethod(); GetCollectionCountFunction = o => (int)countMethod.Invoke(o, null); var isReadOnly = itype.GetProperty("IsReadOnly").GetGetMethod(); IsReadOnlyFunction = obj => (bool)isReadOnly.Invoke(obj, null); isKeyedCollection = type.ExtendsGeneric(typeof (KeyedCollection<,>)); } // implements IList else if (typeof (IList).IsAssignableFrom(type)) { CollectionAddFunction = (obj, value) => ((IList) obj).Add(value); GetCollectionCountFunction = o => ((IList) o).Count; IsReadOnlyFunction = obj => ((IList) obj).IsReadOnly; } }
/// <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="namingConvention">The naming convention.</param> /// <exception cref="System.ArgumentException">Type [{0}] is not a primitive</exception> public PrimitiveDescriptor(IAttributeRegistry attributeRegistry, Type type, IMemberNamingConvention namingConvention) : base(attributeRegistry, type, false, namingConvention) { if (!IsPrimitive(type)) throw new ArgumentException("Type [{0}] is not a primitive"); // Handle remap for enum items if (type.IsEnum) { foreach (var member in type.GetFields(BindingFlags.Public|BindingFlags.Static)) { var attributes = attributeRegistry.GetAttributes(member); foreach (var attribute in attributes) { var yamlRemap = attribute as YamlRemapAttribute; if (yamlRemap != null) { if (enumRemap == null) { enumRemap = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase); } enumRemap[yamlRemap.Name] = member.GetValue(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="namingConvention">The naming convention.</param> /// <exception cref="System.ArgumentException">Type [{0}] is not a primitive</exception> public PrimitiveDescriptor(IAttributeRegistry attributeRegistry, Type type, IMemberNamingConvention namingConvention) : base(attributeRegistry, type, false, namingConvention) { if (!IsPrimitive(type)) { throw new ArgumentException("Type [{0}] is not a primitive"); } // Handle remap for enum items if (type.GetTypeInfo().IsEnum) { foreach (var member in type.GetFields(BindingFlags.Public | BindingFlags.Static)) { var attributes = attributeRegistry.GetAttributes(member); foreach (var attribute in attributes) { var yamlRemap = attribute as YamlRemapAttribute; if (yamlRemap != null) { if (enumRemap == null) { enumRemap = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase); } enumRemap[yamlRemap.Name] = member.GetValue(null); } } } } }
/// <summary> /// Initializes a new instance of the <see cref="DictionaryDescriptor" /> 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="respectPrivateSetters">If set to <c>true</c> will de/serialize properties with private setters.</param> /// <param name="namingConvention">The naming convention.</param> /// <exception cref="System.ArgumentException">Expecting a type inheriting from System.Collections.IDictionary;type</exception> public DictionaryDescriptor(IAttributeRegistry attributeRegistry, Type type, bool emitDefaultValues, bool respectPrivateSetters, IMemberNamingConvention namingConvention) : base(attributeRegistry, type, emitDefaultValues, respectPrivateSetters, namingConvention) { if (!IsDictionary(type)) { throw new ArgumentException("Expecting a type inheriting from System.Collections.IDictionary", "type"); } // extract Key, Value types from IDictionary<??, ??> var interfaceType = type.GetInterface(typeof(IDictionary <,>)); if (interfaceType != null) { keyType = interfaceType.GetGenericArguments()[0]; valueType = interfaceType.GetGenericArguments()[1]; IsGenericDictionary = true; getEnumeratorGeneric = typeof(DictionaryDescriptor).GetMethod("GetGenericEnumerable").MakeGenericMethod(keyType, valueType); addMethod = interfaceType.GetMethod("Add", new[] { keyType, valueType }); } else { keyType = typeof(object); valueType = typeof(object); addMethod = type.GetMethod("Add", new[] { keyType, valueType }); } }
/// <summary> /// Initializes a new instance of the <see cref="TypeDescriptorFactory" /> class. /// </summary> /// <param name="attributeRegistry">The attribute registry.</param> /// <exception cref="System.ArgumentNullException">attributeRegistry</exception> public TypeDescriptorFactory(IAttributeRegistry attributeRegistry) { if (attributeRegistry == null) { throw new ArgumentNullException("attributeRegistry"); } this.attributeRegistry = attributeRegistry; }
/// <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="namingConvention">The naming convention.</param> /// <exception cref="System.ArgumentException">Type [{0}] is not a primitive</exception> public NullableDescriptor(IAttributeRegistry attributeRegistry, Type type, IMemberNamingConvention namingConvention) : base(attributeRegistry, type, false, namingConvention) { if (!IsNullable(type)) throw new ArgumentException("Type [{0}] is not a primitive"); UnderlyingType = Nullable.GetUnderlyingType(type); }
/// <summary> /// Initializes a new instance of the <see cref="TypeDescriptorFactory" /> class. /// </summary> /// <param name="attributeRegistry">The attribute registry.</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">attributeRegistry</exception> public TypeDescriptorFactory(IAttributeRegistry attributeRegistry, bool emitDefaultValues, IMemberNamingConvention namingConvention) { if (attributeRegistry == null) throw new ArgumentNullException("attributeRegistry"); if (namingConvention == null) throw new ArgumentNullException("namingConvention"); this.namingConvention = namingConvention; this.emitDefaultValues = emitDefaultValues; this.attributeRegistry = attributeRegistry; }
public TypeDescriptorFactory(IAttributeRegistry attributeRegistry, bool emitDefaultValues, IMemberNamingConvention namingConvention, IComparer<object> keyComparer) { if (attributeRegistry == null) throw new ArgumentNullException(nameof(attributeRegistry)); this.keyComparer = keyComparer; AttributeRegistry = attributeRegistry; this.emitDefaultValues = emitDefaultValues; this.namingConvention = namingConvention; }
/// <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> /// <exception cref="System.ArgumentException">Type [{0}] is not a primitive</exception> public PrimitiveDescriptor(IAttributeRegistry attributeRegistry, Type type) : base(attributeRegistry, type, false) { if (!IsPrimitive(type)) { throw new ArgumentException("Type [{0}] is not a primitive"); } }
/// <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> /// <exception cref="System.ArgumentException">Type [{0}] is not a primitive</exception> public NullableDescriptor(IAttributeRegistry attributeRegistry, Type type) : base(attributeRegistry, type, false) { if (!IsNullable(type)) { throw new ArgumentException("Type [{0}] is not a primitive"); } UnderlyingType = Nullable.GetUnderlyingType(type); }
/// <summary> /// Initializes a new instance of the <see cref="TypeDescriptorFactory" /> class. /// </summary> /// <param name="attributeRegistry">The attribute registry.</param> /// <param name="emitDefaultValues">if set to <c>true</c> [emit default values].</param> /// <exception cref="System.ArgumentNullException">attributeRegistry</exception> public TypeDescriptorFactory(IAttributeRegistry attributeRegistry, bool emitDefaultValues) { if (attributeRegistry == null) { throw new ArgumentNullException("attributeRegistry"); } this.emitDefaultValues = emitDefaultValues; this.attributeRegistry = attributeRegistry; }
public void RegisterAssembly(Assembly assembly, IAttributeRegistry attributeRegistry) { if (assembly == null) { throw new ArgumentNullException(nameof(assembly)); } if (attributeRegistry == null) { throw new ArgumentNullException(nameof(attributeRegistry)); } // Add automatically the assembly for lookup if (!lookupAssemblies.Contains(assembly)) { lookupAssemblies.Add(assembly); // Register all tags automatically. var assemblySerializers = DataSerializerFactory.GetAssemblySerializers(assembly); if (assemblySerializers != null) { foreach (var dataContractAlias in assemblySerializers.DataContractAliases) { RegisterTagMapping(dataContractAlias.Name, dataContractAlias.Type, dataContractAlias.IsAlias); } } else { Log.Warning($"Assembly [{assembly}] has not been processed by assembly processor with --serialization flags. [DataContract] aliases won't be available."); } // Automatically register YamlSerializableFactory var assemblyScanTypes = AssemblyRegistry.GetScanTypes(assembly); if (assemblyScanTypes != null) { List <Type> types; // Register serializer factories if (assemblyScanTypes.Types.TryGetValue(typeof(IYamlSerializableFactory), out types)) { foreach (var type in types) { if (typeof(IYamlSerializableFactory).IsAssignableFrom(type) && type.GetConstructor(Type.EmptyTypes) != null) { try { SerializableFactories.Add((IYamlSerializableFactory)Activator.CreateInstance(type)); } catch { // Registrying an assembly should not fail, so we are silently discarding a factory if // we are not able to load it. } } } } } } }
public TypeDescriptorFactory(IAttributeRegistry attributeRegistry, bool emitDefaultValues, IMemberNamingConvention namingConvention, IComparer <object> keyComparer) { if (attributeRegistry == null) { throw new ArgumentNullException(nameof(attributeRegistry)); } this.keyComparer = keyComparer; AttributeRegistry = attributeRegistry; this.emitDefaultValues = emitDefaultValues; this.namingConvention = namingConvention; }
/// <summary> /// Initializes a new instance of the <see cref="TypeDescriptorFactory" /> class. /// </summary> /// <param name="attributeRegistry">The attribute registry.</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">attributeRegistry</exception> public TypeDescriptorFactory(IAttributeRegistry attributeRegistry, bool emitDefaultValues, IMemberNamingConvention namingConvention) { if (attributeRegistry == null) { throw new ArgumentNullException("attributeRegistry"); } if (namingConvention == null) { throw new ArgumentNullException("namingConvention"); } this.namingConvention = namingConvention; this.emitDefaultValues = emitDefaultValues; this.attributeRegistry = attributeRegistry; }
/// <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> /// <exception cref="System.ArgumentException">Expecting arrat type;type</exception> public ArrayDescriptor(IAttributeRegistry attributeRegistry, Type type) : base(attributeRegistry, type, false) { if (!type.IsArray) throw new ArgumentException("Expecting array type", "type"); if (type.GetArrayRank() != 1) { throw new ArgumentException("Cannot support dimension [{0}] for type [{1}]. Only supporting dimension of 1".DoFormat(type.GetArrayRank(), type.FullName)); } elementType = type.GetElementType(); listType = typeof(List<>).MakeGenericType(ElementType); toArrayMethod = listType.GetMethod("ToArray"); }
/// <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="namingConvention">The naming convention.</param> /// <exception cref="System.ArgumentException">Expecting arrat type;type</exception> public ArrayDescriptor(IAttributeRegistry attributeRegistry, Type type, IMemberNamingConvention namingConvention) : base(attributeRegistry, type, false, false, namingConvention) { if (!type.IsArray) { throw new ArgumentException("Expecting array type", "type"); } if (type.GetArrayRank() != 1) { throw new ArgumentException("Cannot support dimension [{0}] for type [{1}]. Only supporting dimension of 1".DoFormat(type.GetArrayRank(), type.FullName)); } elementType = type.GetElementType(); }
/// <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> /// <exception cref="System.ArgumentException">Expecting arrat type;type</exception> public ArrayDescriptor(IAttributeRegistry attributeRegistry, Type type) : base(attributeRegistry, type, false) { if (!type.IsArray) { throw new ArgumentException("Expecting array type", "type"); } if (type.GetArrayRank() != 1) { throw new ArgumentException("Cannot support dimension [{0}] for type [{1}]. Only supporting dimension of 1".DoFormat(type.GetArrayRank(), type.FullName)); } elementType = type.GetElementType(); listType = typeof(List <>).MakeGenericType(ElementType); toArrayMethod = listType.GetMethod("ToArray"); }
/// <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; }
public void RegisterAssembly(Assembly assembly, IAttributeRegistry attributeRegistry) { if (assembly == null) { throw new ArgumentNullException("assembly"); } if (attributeRegistry == null) { throw new ArgumentNullException("attributeRegistry"); } // Add automatically the assembly for lookup if (!DefaultLookupAssemblies.Contains(assembly) && !lookupAssemblies.Contains(assembly)) { lookupAssemblies.Add(assembly); var types = new Type[0]; // Register all tags automatically. foreach (var type in assembly.GetTypes()) { var tagAttribute = attributeRegistry.GetAttribute <YamlTagAttribute>(type); if (tagAttribute != null && !string.IsNullOrEmpty(tagAttribute.Tag)) { RegisterTagMapping(tagAttribute.Tag, type); } // Automatically register YamlSerializableFactory if (typeof(IYamlSerializableFactory).IsAssignableFrom(type) && type.GetConstructor(types) != null) { try { SerializableFactories.Add((IYamlSerializableFactory)Activator.CreateInstance(type)); } catch { // Registrying an assembly should not fail, so we are silently discarding a factory if // we are not able to load it. } } } } }
/// <summary> /// Initializes a new instance of the <see cref="CollectionDescriptor" /> 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> /// <exception cref="System.ArgumentException">Expecting a type inheriting from System.Collections.ICollection;type</exception> public CollectionDescriptor(IAttributeRegistry attributeRegistry, Type type, bool emitDefaultValues) : base(attributeRegistry, type, emitDefaultValues) { if (!IsCollection(type)) throw new ArgumentException("Expecting a type inheriting from System.Collections.ICollection", "type"); // Gets the element type var collectionType = type.GetInterface(typeof(IEnumerable<>)); ElementType = (collectionType != null) ? collectionType.GetGenericArguments()[0] : typeof(object); // implements ICollection<T> Type itype; if ((itype = type.GetInterface(typeof(ICollection<>))) != null) { var add = itype.GetMethod("Add", new [] { ElementType }); CollectionAddFunction = (obj, value) => add.Invoke(obj, new [] { value }); var countMethod = itype.GetProperty("Count").GetGetMethod(); GetCollectionCountFunction = o => (int)countMethod.Invoke(o, null); var isReadOnly = itype.GetProperty("IsReadOnly").GetGetMethod(); IsReadOnlyFunction = obj => (bool)isReadOnly.Invoke(obj, null); } // implements IList else if (typeof (IList).IsAssignableFrom(type)) { CollectionAddFunction = (obj, value) => ((IList) obj).Add(value); GetCollectionCountFunction = o => ((IList) o).Count; IsReadOnlyFunction = obj => ((IList) obj).IsReadOnly; } // Finds if it is a pure list if (Contains("Capacity")) { var capacityMember = this["Capacity"] as PropertyDescriptor; HasOnlyCapacity = Count == 1 && capacityMember != null && (capacityMember.DeclaringType.Namespace ?? string.Empty).StartsWith(SystemCollectionsNamespace); } IsPureCollection = Count == 0; }
/// <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; } } }
public TypeDescriptorFactory(IAttributeRegistry attributeRegistry, bool emitDefaultValues, IMemberNamingConvention namingConvention) : this(attributeRegistry, emitDefaultValues, namingConvention, new DefaultMemberComparer()) { }
public TypeDescriptorFactory(IAttributeRegistry attributeRegistry) : this(attributeRegistry, false, new DefaultNamingConvention()) { }
/// <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="namingConvention">The naming convention.</param> /// <exception cref="System.ArgumentException">Expecting arrat type;type</exception> public ArrayDescriptor(IAttributeRegistry attributeRegistry, Type type, IMemberNamingConvention namingConvention) : this(attributeRegistry, type, false, namingConvention) { }
/// <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="DataVisitorBase"/> class. /// </summary> /// <param name="attributeRegistry">The attribute registry.</param> protected DataVisitorBase(IAttributeRegistry attributeRegistry) : this(new TypeDescriptorFactory(attributeRegistry)) { }
/// <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); } }
/// <summary> /// Initializes a new instance of the <see cref="TypeDescriptorFactory" /> class. /// </summary> /// <param name="attributeRegistry">The attribute registry.</param> /// <param name="emitDefaultValues">if set to <c>true</c> [emit default values].</param> /// <exception cref="System.ArgumentNullException">attributeRegistry</exception> public TypeDescriptorFactory(IAttributeRegistry attributeRegistry, bool emitDefaultValues) { if (attributeRegistry == null) throw new ArgumentNullException("attributeRegistry"); this.emitDefaultValues = emitDefaultValues; this.attributeRegistry = attributeRegistry; }
/// <summary> /// Initializes a new instance of the <see cref="SerializerSettings" /> class. /// </summary> public SerializerSettings(IYamlSchema schema) { PreferredIndent = 2; IndentLess = false; EmitAlias = true; EmitTags = true; SortKeyForMapping = true; EmitJsonComptible = false; EmitCapacityForList = false; SpecialCollectionMember = "~Items"; LimitPrimitiveFlowSequence = 0; DefaultStyle = YamlStyle.Block; this.schema = schema ?? new CoreSchema(); AssemblyRegistry = new AssemblyRegistry(Schema); attributeRegistry = new AttributeRegistry(); ObjectFactory = new DefaultObjectFactory(); ObjectSerializerBackend = new DefaultObjectSerializerBackend(); ComparerForKeySorting = new DefaultKeyComparer(); NamingConvention = new DefaultNamingConvention(); // Register default mapping for map and seq AssemblyRegistry.RegisterTagMapping("!!map", typeof(IDictionary<object, object>)); AssemblyRegistry.RegisterTagMapping("!!seq", typeof(IList<object>)); }
/// <summary> /// Gets the attributes associated with the specified member. /// </summary> /// <typeparam name="T">Type of the attribute</typeparam> /// <param name="attributeRegistry">The attribute registry.</param> /// <param name="memberInfo">The member information.</param> /// <param name="inherit">if set to <c>true</c> [inherit].</param> /// <returns>An enumeration of <see cref="Attribute" />.</returns> public static IEnumerable <T> GetAttributes <T>(this IAttributeRegistry attributeRegistry, MemberInfo memberInfo, bool inherit = true) where T : Attribute { return(attributeRegistry.GetAttributes(memberInfo, inherit).OfType <T>()); }
/// <summary> /// Initializes a new instance of the <see cref="TypeDescriptorFactory" /> class. /// </summary> /// <param name="attributeRegistry">The attribute registry.</param> /// <exception cref="System.ArgumentNullException">attributeRegistry</exception> public TypeDescriptorFactory(IAttributeRegistry attributeRegistry) { if (attributeRegistry == null) throw new ArgumentNullException("attributeRegistry"); this.attributeRegistry = attributeRegistry; }
public void RegisterAssembly(Assembly assembly, IAttributeRegistry attributeRegistry) { if (assembly == null) { throw new ArgumentNullException("assembly"); } if (attributeRegistry == null) { throw new ArgumentNullException("attributeRegistry"); } // Add automatically the assembly for lookup if (!DefaultLookupAssemblies.Contains(assembly) && !lookupAssemblies.Contains(assembly)) { lookupAssemblies.Add(assembly); var types = new Type[0]; // Register all tags automatically. foreach (var type in assembly.GetTypes()) { var attributes = attributeRegistry.GetAttributes(type.GetTypeInfo()); foreach (var attribute in attributes) { string name = null; bool isAlias = false; var tagAttribute = attribute as YamlTagAttribute; if (tagAttribute != null) { name = tagAttribute.Tag; } else { var yamlRemap = attribute as YamlRemapAttribute; if (yamlRemap != null) { name = yamlRemap.Name; isAlias = true; } } if (!string.IsNullOrEmpty(name)) { RegisterTagMapping(name, type, isAlias); } } // Automatically register YamlSerializableFactory if (typeof(IYamlSerializableFactory).IsAssignableFrom(type) && type.GetConstructor(types) != null) { try { SerializableFactories.Add((IYamlSerializableFactory)Activator.CreateInstance(type)); } catch { // Registrying an assembly should not fail, so we are silently discarding a factory if // we are not able to load it. } } } } }
/// <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) : this(attributeRegistry, type, emitDefaultValues, false, namingConvention) { }
public void RegisterAssembly(Assembly assembly, IAttributeRegistry attributeRegistry) { if (assembly == null) throw new ArgumentNullException("assembly"); if (attributeRegistry == null) throw new ArgumentNullException("attributeRegistry"); // Add automatically the assembly for lookup if (!DefaultLookupAssemblies.Contains(assembly) && !lookupAssemblies.Contains(assembly)) { lookupAssemblies.Add(assembly); var types = new Type[0]; // Register all tags automatically. foreach (var type in assembly.GetTypes()) { var attributes = attributeRegistry.GetAttributes(type); foreach (var attribute in attributes) { string name = null; bool isAlias = false; var tagAttribute = attribute as DataContractAttribute; if (!string.IsNullOrWhiteSpace(tagAttribute?.Alias)) { name = tagAttribute.Alias; } else { var yamlRemap = attribute as DataAliasAttribute; if (yamlRemap != null) { name = yamlRemap.Name; isAlias = true; } } if (!string.IsNullOrEmpty(name)) { RegisterTagMapping(name, type, isAlias); } } // Automatically register YamlSerializableFactory if (typeof(IYamlSerializableFactory).IsAssignableFrom(type) && type.GetConstructor(types) != null) { try { SerializableFactories.Add((IYamlSerializableFactory) Activator.CreateInstance(type)); } catch { // Registrying an assembly should not fail, so we are silently discarding a factory if // we are not able to load it. } } } } }
/// <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> /// <exception cref="System.ArgumentException">Type [{0}] is not a primitive</exception> public PrimitiveDescriptor(IAttributeRegistry attributeRegistry, Type type) : base(attributeRegistry, type, false) { if (!IsPrimitive(type)) throw new ArgumentException("Type [{0}] is not a primitive"); }
public void RegisterAssembly(Assembly assembly, IAttributeRegistry attributeRegistry) { if (assembly == null) throw new ArgumentNullException("assembly"); if (attributeRegistry == null) throw new ArgumentNullException("attributeRegistry"); // Add automatically the assembly for lookup if (!DefaultLookupAssemblies.Contains(assembly) && !lookupAssemblies.Contains(assembly)) { lookupAssemblies.Add(assembly); var types = new Type[0]; // Register all tags automatically. foreach (var type in assembly.GetTypes()) { var tagAttribute = attributeRegistry.GetAttribute<YamlTagAttribute>(type); if (tagAttribute != null && !string.IsNullOrEmpty(tagAttribute.Tag)) { RegisterTagMapping(tagAttribute.Tag, type); } // Automatically register YamlSerializableFactory if (typeof (IYamlSerializableFactory).IsAssignableFrom(type) && type.GetConstructor(types) != null) { try { SerializableFactories.Add((IYamlSerializableFactory)Activator.CreateInstance(type)); } catch { // Registrying an assembly should not fail, so we are silently discarding a factory if // we are not able to load it. } } } } }
/// <summary> /// Gets the first attribute of type T associated with the specified member. /// </summary> /// <typeparam name="T">Type of the attribute</typeparam> /// <param name="memberInfo">The member information.</param> /// <param name="inherit">if set to <c>true</c> [inherit].</param> /// <returns>An attribute of type {T} if it was found; otherwise <c>null</c> </returns> public static T GetAttribute <T>(this IAttributeRegistry attributeRegistry, MemberInfo memberInfo, bool inherit = true) where T : Attribute { return(attributeRegistry.GetAttributes(memberInfo, inherit).OfType <T>().FirstOrDefault()); }
/// <summary> /// Gets an attribute associated with the specified member. /// </summary> /// <typeparam name="T">Type of the attribute</typeparam> /// <param name="attributeRegistry">The attribute registry.</param> /// <param name="memberInfo">The member information.</param> /// <param name="inherit">if set to <c>true</c> [inherit].</param> /// <returns>An attribute of type {T} if it was found; otherwise <c>null</c></returns> public static T GetAttribute <T>(this IAttributeRegistry attributeRegistry, [NotNull] MemberInfo memberInfo, bool inherit = true) where T : Attribute { var list = attributeRegistry.GetAttributes(memberInfo, inherit); return(list.OfType <T>().FirstOrDefault()); }
public TypeDescriptorFactory(IAttributeRegistry attributeRegistry, bool emitDefaultValues, IMemberNamingConvention namingConvention) : this(attributeRegistry, emitDefaultValues, namingConvention, new DefaultKeyComparer()) { }
public void RegisterAssembly(Assembly assembly, IAttributeRegistry attributeRegistry) { if (assembly == null) throw new ArgumentNullException("assembly"); if (attributeRegistry == null) throw new ArgumentNullException("attributeRegistry"); // Add automatically the assembly for lookup if (!DefaultLookupAssemblies.Contains(assembly) && !lookupAssemblies.Contains(assembly)) { lookupAssemblies.Add(assembly); // Register all tags automatically. foreach (var type in assembly.GetTypes()) { var tagAttribute = attributeRegistry.GetAttribute<YamlTagAttribute>(type); if (tagAttribute != null && !string.IsNullOrEmpty(tagAttribute.Tag)) { RegisterTagMapping(tagAttribute.Tag, type); } } } }
/// <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; }