private IYamlTypeConverter FindConverter(SerializerContext context, Type type, bool exceptionIfNotFound) { if ( typeConverters.ContainsKey(type) ) { return typeConverters[type]; } var tagConverterAttribute = type.GetAttribute<YamlTypeConverterAttribute>(); if (tagConverterAttribute != null) { var converterType = Type.GetType(tagConverterAttribute.ConverterTypeName); var converter = Activator.CreateInstance(converterType) as IYamlTypeConverter; if (converter != null) { Register(type, converter); return converter; } } // Try to resolve it via a factory foreach (var typeConverterFactory in typeConverterFactories) { var converter = typeConverterFactory.TryCreate(context, type); if (converter != null) { Register(type, converter); return converter; } } if (exceptionIfNotFound) throw new InvalidOperationException(string.Format("No type converter registered for type [{0}]", type.FullName)); return null; }
/// <summary> /// Construct YAML node tree that represents a given C# object. /// </summary> /// <param name="node"><see cref="YamlNode"/> to be converted to C# object.</param> /// <param name="expected">Expected type for the root object.</param> /// <param name="config"><see cref="YamlConfig"/> to customize serialization.</param> /// <returns></returns> public object NodeToObject(YamlNode node, Type expected, SerializerContext context) { this.context = context; this.config = context.Config; var appeared = new Dictionary<YamlNode, object>(TypeUtils.EqualityComparerByRef<YamlNode>.Default); return NodeToObjectInternal(node, expected, appeared); }
public bool IsTypeConverterSpecified(SerializerContext context, Type type) { if (!typeConverters.ContainsKey(type)) { return FindConverter(context, type, false) != null; } return true; }
public YamlNode ObjectToNode(object obj, SerializerContext context) { if (context == null) throw new ArgumentNullException("context"); this.context = context; this.config = context.Config; appeared.Clear(); if ( config.OmitTagForRootNode ) { return ObjectToNode(obj, obj.GetType()); } else { return ObjectToNode(obj, (Type)null); } }
public IYamlTypeConverter TryCreate(SerializerContext context, Type type) { var attrs = type.GetCustomAttributes(typeof (TypeConverterAttribute), true); if (attrs.Length > 0) { var converterAttr = (TypeConverterAttribute)attrs[0]; // What is the difference between these two conditions? var converterType = context.ResolveType(converterAttr.ConverterTypeName); var typeCovnerter = Activator.CreateInstance(converterType) as TypeConverter; if (typeCovnerter != null) { return new LegacyTypeConverter(typeCovnerter); } } return null; }
public IYamlSerializable FindSerializable(SerializerContext context, object value, Type type) { var serializable = value as IYamlSerializable; // Type may be null, so return null in this case if (serializable == null && type != null) { if (!typeToSerializable.TryGetValue(type, out serializable)) { foreach (var factory in factories) { serializable = factory.TryCreate(context, type); if (serializable != null) { typeToSerializable.Add(type, serializable); return serializable; } } typeToSerializable.Add(type, null); } } return serializable; }
public IYamlSerializable TryCreate(SerializerContext context, Type type) { var attribute = type.GetAttribute<YamlSerializableAttribute>(); if (attribute == null) { return null; } var typeName = attribute.SerializableTypeName; var serializableType = context.ResolveType(typeName); if (serializableType == null) { throw new InvalidOperationException(string.Format("Unable to find serializable type [{0}] from current and registered assemblies", typeName)); } if (!typeof(IYamlSerializable).IsAssignableFrom(serializableType)) { throw new InvalidOperationException(string.Format("Serializable type [{0}] is not a IYamlSerializable", typeName)); } return (IYamlSerializable)Activator.CreateInstance(serializableType); }
static YamlNode() { // Initializing order matters ! DefaultTagPrefix = "tag:yaml.org,2002:"; DefaultConfig = new YamlConfig(); DefaultParser = new YamlParser(); DefaultPresenter = new YamlPresenter(); DefaultSerializerContext = new SerializerContext(DefaultConfig); foreach (var shortToLongTag in ShortToLongTags) { LongToShortTags.Add(shortToLongTag.Value, shortToLongTag.Key); } }
/// <summary> /// Construct YAML node tree that represents a given C# object. /// </summary> /// <param name="node"><see cref="YamlNode" /> to be converted to C# object.</param> /// <param name="context">The context.</param> /// <returns>System.Object.</returns> public object NodeToObject(YamlNode node, SerializerContext context) { return NodeToObject(node, null, context); }
public object ConvertFromString(SerializerContext context, string s, Type type) { return FindConverter(context, type, true).ConvertFrom(Culture, s); }
public string ConvertToString(SerializerContext context, object obj) { if ( obj == null ) return "null"; var converter = FindConverter(context, obj.GetType(), true); return converter != null ? converter.ConvertTo(Culture, obj) : obj.ToString(); }
public IYamlTypeConverter TryCreate(SerializerContext context, Type type) { return type.IsEnum ? new EnumConverter(type) : null; }