public static string WriteToString(this YamlObjectStore yaml) { var doc = new YamlDotNet.RepresentationModel.YamlDocument(yaml.MappingNode); var sb = new System.Text.StringBuilder(); var strm = new YamlDotNet.RepresentationModel.YamlStream(); strm.Add(doc); strm.Save(new System.IO.StringWriter(sb)); return(sb.ToString()); }
public static void Serialize(this IObjectStore storage, object val) { var yamlStore = storage as YamlObjectStore; if (yamlStore == null) { throw new System.NotImplementedException("Only YamlStore supported for serializing"); } var typeInfo = val.GetType(); var properties = typeInfo.GetProperties(); yamlStore.SetValue(SERIALIZED_TYPE_KEY, typeInfo.FullName); foreach (var prop in properties) { var attribute = prop.GetCustomAttribute <ObjectStoreAttribute>(); if (attribute == null) { continue; } var propType = prop.PropertyType.Name.ToLower(); var propertyValue = prop.GetValue(val); if (propertyValue == null) { continue; } switch (propType) { case "boolean": yamlStore.SetValue(attribute.PropertyName, (bool)propertyValue); break; case "int32": yamlStore.SetValue(attribute.PropertyName, (int)propertyValue); break; case "single": yamlStore.SetValue(attribute.PropertyName, (float)propertyValue); break; case "string": yamlStore.SetValue(attribute.PropertyName, propertyValue.ToString()); break; case "string[]": yamlStore.SetValue(attribute.PropertyName, (string[])propertyValue); break; case "object[]": var objectArray = (object[])propertyValue; var store = new List <YamlObjectStore>(); foreach (var o in objectArray) { var s = new YamlObjectStore(); s.Serialize(o); store.Add(s); } yamlStore.SetValue(attribute.PropertyName, store); break; case "cup": default: if (prop.PropertyType.IsEnum) { yamlStore.SetValue(attribute.PropertyName, propertyValue.ToString()); } else { ShortLog.DebugFormat("Attempting to serialize: {0} {1}", attribute.PropertyName, prop.PropertyType); var newStore = new YamlObjectStore(); newStore.Serialize(propertyValue); yamlStore.SetValue(attribute.PropertyName, newStore); } break; } } }
private string GuessFileType(YamlObjectStore yaml) { return(yaml.GetTag()); }
public void SetValue(string key, YamlObjectStore val) { this.mappingNode.Children[new YamlScalarNode(key)] = val.MappingNode; }