Exemplo n.º 1
0
                public ListEnumerator(Type type, JsonArray array)
                {
                    var creator = ReflectiveOperation.GetObjectCreator(type);

                    DstObject   = creator.Creator();
                    _enumerator = array.GetEnumerator();
                    Element     = creator.Element;
                }
Exemplo n.º 2
0
                public SetterEnumerator(Type type, JsonDictionary dict)
                {
                    var creator = ReflectiveOperation.GetObjectCreator(type);

                    DstObject = creator.Creator();
                    _setters  = creator.Setters;
                    _dict     = dict;
                }
Exemplo n.º 3
0
            private static object ConvertToObject(InternalObject obj, Type type)
            {
                var    stack   = new Stack <Context>();
                var    context = new Context();
                object result;

Convert:
                if (type == typeof(object))
                {
                    result = ToValue(obj);
                    goto Return;
                }
                switch (obj.Type)
                {
                case JsonType.Null:
                    result = ChangeType(ToValue(obj), type, InvariantCulture);
                    break;

                case JsonType.True:
                case JsonType.False:
                    result = type == typeof(bool)
                            ? obj.Type == JsonType.True
                            : ChangeType(ToValue(obj), type, InvariantCulture);
                    break;

                case JsonType.String:
                    result = type == typeof(string) ? obj.String : ChangeType(ToValue(obj), type, InvariantCulture);
                    break;

                case JsonType.Array:
                    stack.Push(context);
                    if (!type.IsArray && !IsGenericList(type))
                    {
                        throw InvalidCastException(obj, type);
                    }
                    if (type.IsArray)
                    {
                        var element = type.GetElementType();
                        context = new Context
                        {
                            Mode     = ConvertMode.Array,
                            Type     = element,
                            DstArray = Array.CreateInstance(element, obj.Array.Count)
                        };
                    }
                    else
                    {
                        context = new Context
                        {
                            Mode     = ConvertMode.List,
                            Type     = type.GetGenericArguments()[0],
                            DstArray = ReflectiveOperation.GetObjectCreator(type)()
                        };
                    }
                    context.JsonArrayEnumerator = obj.Array.GetEnumerator();
                    goto ArrayNext;

                case JsonType.Object:
                    if (type.IsArray)
                    {
                        throw InvalidCastException(obj, type);
                    }
                    stack.Push(context);
                    context = new Context
                    {
                        Mode             = ConvertMode.Object,
                        DstObject        = ReflectiveOperation.GetObjectCreator(type)(),
                        SetterEnumerator = new SetterEnumerator(type, obj.Dictionary)
                    };
                    goto ObjectNext;

                default:
                    result = type == typeof(double)
                            ? obj.Number
                            : type == typeof(int)
                                ? (int)obj.Number
                                : type == typeof(float)
                                    ? (float)obj.Number
                                    : ChangeType(ToValue(obj), type, InvariantCulture);
                    break;
                }

Return:
                if (stack.Count == 0)
                {
                    return(result);
                }
                switch (context.Mode)
                {
                case ConvertMode.Array:
                    // ReSharper disable once RedundantCast
                    context.DstArray[context.JsonArrayEnumerator.Position] = (dynamic)result;
                    break;

                case ConvertMode.List:
                    context.DstArray.Add((dynamic)result);
                    break;

                case ConvertMode.Object:
                    context.SetterEnumerator.Current.Invoke(context.DstObject, result);
                    goto ObjectNext;
                }

ArrayNext:
                if (!context.JsonArrayEnumerator.MoveNext())
                {
                    result  = context.DstArray;
                    context = stack.Pop();
                    goto Return;
                }
                type = context.Type;
                obj  = context.JsonArrayEnumerator.Current;
                goto Convert;

ObjectNext:
                if (!context.SetterEnumerator.MoveNext())
                {
                    result  = context.DstObject;
                    context = stack.Pop();
                    goto Return;
                }
                var current = context.SetterEnumerator.Current;

                type = current.Type;
                obj  = current.Value;
                goto Convert;
            }
Exemplo n.º 4
0
 public SetterEnumerator(Type type, JsonDictionary dict)
 {
     _setters = ReflectiveOperation.GetSetterList(type);
     _dict    = dict;
 }
Exemplo n.º 5
0
 public GetterEnumerator(object target)
 {
     _target  = target;
     _getters = ReflectiveOperation.GetGetterList(_target.GetType());
 }