private static IEnumerable <(YamlLine key, IReadOnlyList <YamlLine> value)> FormatLines(this IEnumerable <YamlLine> lines) { YamlLine resultKey = null; List <YamlLine> resultValue = null; foreach (var line in lines) { if (line.Keys.Length == 1) { if (resultKey != null) { yield return(resultKey, resultValue); resultValue = null; } resultKey = line; continue; } if (resultValue == null) { resultValue = new List <YamlLine>(); } resultValue.Add(line.RemoveFirstLevel()); } if (resultKey != null || resultValue != null) { yield return(resultKey, resultValue); } }
public static void SetValue(this object instance, YamlLine line, IReadOnlyList <YamlLine> childItems) { var type = instance.GetType(); var properties = type.ExtractProperties(); if (!properties.ContainsKey(line.Keys[0])) { return; } var propertyInfo = properties[line.Keys[0]]; try { var theValue = propertyInfo.PropertyType.ChangeType(line.Value); if (theValue is List <string> list) { SetArrayValues(list, childItems); propertyInfo.SetValue(instance, list.ToArray()); return; } propertyInfo.SetValue(instance, theValue); if (SimpleTypes.ContainsKey(propertyInfo.PropertyType) || propertyInfo.PropertyType.IsEnum) { return; } if (theValue is IDictionary dict && childItems != null) { dict.SetDictionaryValues(childItems); } if (theValue.GetType().IsClass) { SetValue(theValue, childItems); } } catch { Console.WriteLine("Can not set Value: " + line.Value + " to property: " + propertyInfo.Name + " of type " + type); } }
public bool IsHigherLevelOf(YamlLine yamlLine) { if (Keys.Length <= yamlLine.Keys.Length) { return(false); } for (var i = 0; i < yamlLine.Keys.Length; i++) { if (Keys[i] != yamlLine.Keys[i]) { return(false); } } return(true); }
public static void SetYamlValue(object o, PropertyInfo pi, YamlLine yamlLine, IReadOnlyList <YamlLine> yamlLines, List <string> notFound) { if (pi.PropertyType.IsSimpleProperty()) { o.SetYamlValue(pi, yamlLine.Value); return; } if (pi.IsPropertyDictionary()) { var dictInstance = pi.CreateDictionary(); var subYamlLines = GetSubLines(yamlLines, yamlLine); var dictItemType = pi.GetDictionaryType(); PopulateDictionary(dictInstance, subYamlLines.ToList(), dictItemType.valueType, notFound); pi.SetValue(o, dictInstance); return; } if (pi.PropertyType.IsPropertyEnumerable()) { var subYamlLines = GetSubLines(yamlLines, yamlLine); var elementType = pi.GetEnumerableElementType(); var resultEnumerable = IterateElements(elementType, subYamlLines); var result = pi.CastEnumerable(resultEnumerable.ToList(), elementType); pi.SetValue(o, result); return; } if (pi.PropertyType.IsPropertyClass()) { var classInstance = pi.PropertyType.CreateObjectByDefaultConstructor(); var subYamlLines = GetSubLines(yamlLines, yamlLine); MyYamlDeserializer.PopulateFields(classInstance, subYamlLines.ToList(), notFound); pi.SetValue(o, classInstance); } }
public YamlLine CreateAsSubItem(YamlLine yamlLine) { var keys = Keys.Skip(yamlLine.Keys.Length).ToArray(); return(new YamlLine(keys, Value)); }
private static IEnumerable <YamlLine> GetSubLines(IEnumerable <YamlLine> yamlLines, YamlLine line) { return(yamlLines .Where(itm => itm.IsHigherLevelOf(line)) .Select(itm => itm.CreateAsSubItem(line))); }