Ejemplo n.º 1
0
        public static HierarchicalItem GetItems(string value, HierarchicalItem item = null)
        {
            item ??= new HierarchicalItem();
            var obj    = GetValue(value, '{', '}');
            var result = GetValues(obj, ',');

            result = NormalizeSplit(result, '{', '}', ',');

            foreach (var child in result)
            {
                if (!child.Contains('{'))
                {
                    var(name, dataValue, type) = GetDataFromValue(child);
                    item.Children.Add(
                        new HierarchicalItem
                    {
                        Name  = name,
                        Value = type == TypeEnum.Integer ? int.Parse(dataValue) : (object)dataValue,
                        Type  = type
                    });
                }
                else
                {
                    item.Children.Add(GetItems(string.Join(":", child.Split(':').Skip(1)), new HierarchicalItem
                    {
                        Type = TypeEnum.Object,
                        Name = child.Split(':')[0]
                    }));
                }
            }

            return(item);
        }
Ejemplo n.º 2
0
        public static object Parse(HierarchicalItem mainItem, Type type)
        {
            var mainObject = Activator.CreateInstance(type);
            var mainType   = type;

            foreach (var item in mainItem.Children)
            {
                var prop = mainType.GetProperty(item.Name.Replace("\"", ""));
                prop.SetValue(mainObject,
                              item.Type != TypeEnum.Object
                        ? item.Value
                        : Parse(item, prop.PropertyType));
            }
            return(mainObject);
        }