Beispiel #1
0
        /// <summary>
        /// Converts this instance to a collection containing <see cref="object"/> type instances corresponding to the underlying
        /// elements of this instance.
        /// </summary>
        /// <param name="jsonValue">The <see cref="JsonValue"/> instance to convert to the object collection.</param>
        /// <param name="type">The <see cref="Type"/> of the collection to convert this instance to.</param>
        /// <returns>An object representing a CLR collection depending on the <see cref="JsonType"/> value of this instance and the specified type value.</returns>
        private static object ToClrCollection(JsonValue jsonValue, Type type)
        {
            object collection = null;

            if (CanConvertToClrCollection(jsonValue, type))
            {
                JsonValue parentValue = jsonValue;
                Queue <KeyValuePair <string, JsonValue> > childValues = null;
                Stack <ToClrCollectionStackInfo>          stackInfo   = new Stack <ToClrCollectionStackInfo>();
                int currentIndex = 0;

                collection = JsonValueExtensions.CreateClrCollection(parentValue);

                do
                {
                    if (childValues == null)
                    {
                        childValues = new Queue <KeyValuePair <string, JsonValue> >(parentValue);
                    }

                    while (childValues != null && childValues.Count > 0)
                    {
                        KeyValuePair <string, JsonValue> item = childValues.Dequeue();
                        JsonValue childValue = item.Value;

                        switch (childValue.JsonType)
                        {
                        case JsonType.Array:
                        case JsonType.Object:
                            object childCollection = JsonValueExtensions.CreateClrCollection(childValue);

                            InsertClrItem(collection, ref currentIndex, item.Key, childCollection);

                            stackInfo.Push(new ToClrCollectionStackInfo(parentValue, collection, currentIndex, childValues));
                            parentValue  = item.Value;
                            childValues  = null;
                            collection   = childCollection;
                            currentIndex = 0;
                            break;

                        default:
                            InsertClrItem(collection, ref currentIndex, item.Key, item.Value.Read());
                            break;
                        }
                    }

                    if (childValues != null && stackInfo.Count > 0)
                    {
                        ToClrCollectionStackInfo info = stackInfo.Pop();
                        collection   = info.Collection;
                        childValues  = info.JsonValueChildren;
                        parentValue  = info.ParentJsonValue;
                        currentIndex = info.CurrentIndex;
                    }
                }while (stackInfo.Count > 0 || childValues == null || childValues.Count > 0);
            }

            return(collection);
        }
        private static object ToClrCollection(JsonValue jsonValue, Type type)
        {
            object collection = null;

            if (CanConvertToClrCollection(jsonValue, type))
            {
                JsonValue parentJsonValue = jsonValue;
                Queue <KeyValuePair <string, JsonValue> > iterator = null;
                Stack <ToClrCollectionStackInfo>          stack    = new Stack <ToClrCollectionStackInfo>();
                int index = 0;
                collection = CreateClrCollection(parentJsonValue);
                do
                {
                    if (iterator == null)
                    {
                        iterator = new Queue <KeyValuePair <string, JsonValue> >(parentJsonValue);
                    }

                    while ((iterator != null) && (iterator.Count > 0))
                    {
                        KeyValuePair <string, JsonValue> pair = iterator.Dequeue();
                        JsonValue value3 = pair.Value;
                        switch (value3.JsonType)
                        {
                        case JsonType.Object:
                        case JsonType.Array:
                        {
                            object obj3 = CreateClrCollection(value3);
                            InsertClrItem(collection, ref index, pair.Key, obj3);
                            stack.Push(new ToClrCollectionStackInfo(parentJsonValue, collection, index, iterator));
                            parentJsonValue = pair.Value;
                            iterator        = null;
                            collection      = obj3;
                            index           = 0;
                            break;
                        }

                        default:
                            InsertClrItem(collection, ref index, pair.Key, ((JsonPrimitive)pair.Value).Value);
                            break;
                        }
                    }

                    if ((iterator != null) && (stack.Count > 0))
                    {
                        ToClrCollectionStackInfo info = stack.Pop();
                        collection      = info.Collection;
                        iterator        = info.JsonValueChildren;
                        parentJsonValue = info.ParentJsonValue;
                        index           = info.CurrentIndex;
                    }
                }while (((stack.Count > 0) || (iterator == null)) || (iterator.Count > 0));
            }

            return(collection);
        }