Ejemplo n.º 1
0
        private static void AddSerializableStateElement(string name, object value, Type type,
                                                        Action <SerializableStateElement> listAddAction,
                                                        bool isProperty)
        {
            var isValueType = type.IsValueType || value is string || value is Type;
            var isArray     = value is IEnumerable;

            if (value is string)
            {
                isArray = false;
            }

            var element = new SerializableStateElement
            {
                Name       = name,
                Value      = isArray ? $"{type.Name}" : isValueType ? value : "",
                Type       = type,
                ObjectType = isArray ? ObjectType.Array : isValueType ? ObjectType.Value : ObjectType.Object,
                Children   = Enumerable.Empty <SerializableStateElement>().ToArray()
            };

            if (isArray)
            {
                var collectionType = type;
                element.Children = GetChildren(value as IEnumerable, ref collectionType, isProperty);
                element.Value    = $"{collectionType.Name}{(collectionType.IsArray ? "" : "[]")}";
            }
            else if (!isValueType)
            {
                element.Children = GetChildren(value, isProperty);
            }

            listAddAction(element);
        }
Ejemplo n.º 2
0
        public static string ToJson(this SerializableStateElement element)
        {
            var childrenText = element.Children == null
                ? ""
                : string.Join(",", element.Children.Select(ToJson).ToArray());

            return("{" +
                   $"\"Name\" : \"{element.Name}\", \"Value\" : \"{element.Value}\", \"Children\" : [{childrenText}], \"TypeName\" : \"{element.Type}\", \"Type\" : \"{element.ObjectType}\"" +
                   "}");
        }