public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            IVirtualListSerializable vlist = (IVirtualListSerializable)value;

            writer.WriteStartObject();
            writer.WritePropertyName("@arr");
            writer.WriteValue("1");
            writer.WritePropertyName("ltype");
            writer.WriteValue(vlist.ListType);
            writer.WritePropertyName("UniqueID");
            writer.WriteValue(vlist.UniqueID);
            writer.WritePropertyName("Count");
            writer.WriteValue(vlist.Count);
            writer.WritePropertyName("uids");
            writer.WriteStartArray();
            var type = vlist.GetType().GetGenericArguments()[0];

            foreach (KeyValuePair <string, int> val in vlist.InitialPositionOfEachPage)
            {
                Page p = vlist.PageManager.LoadPage(val.Key);
                foreach (var item in p.GetStoredObjects())
                {
                    LazyObject lazy = (LazyObject)item;
                    if (lazy.TargetUniqueID != null)
                    {
                        writer.WriteValue(lazy.TargetUniqueID);
                    }
                    else
                    {
                        //We have to validate if the type is not a Struct
                        //because the structs shouldn't go to client at this moment.
                        if (!(!type.IsEnum && !type.IsPrimitive && type.IsValueType && type != typeof(DateTime)))
                        {
                            writer.WriteValue(lazy.Target);
                        }
                    }
                }
            }
            writer.WriteEndArray();
            writer.WriteEndObject();
        }
        public Page LoadPage(string uniqueID)
        {
            //Let's add loaded pages to a cache.
            Page p = null;

            lock (loadedPagesLock)
            {
                if (_loadedPages.TryGetValue(uniqueID, out p))
                {
                    return(p);
                }
                //Release to avoid deadlock
                p = _stateManager.GetObject(uniqueID) as Page;
                if (p == null)
                {
                    p = _stateManager.GetObject(uniqueID, includeDeleted: true, getObjectFromTemp: true) as Page;
                }
                if (p != null)
                {
                    //We add the page we just retrieved.
                    _loadedPages.Add(uniqueID, p);
                    foreach (var lazyobject in p.GetStoredObjectsSafe())
                    {
                        LazyObject obj = lazyobject as LazyObject;
                        if (obj != null && obj.StateManager == null)
                        {
                            obj.StateManager = _stateManager;
                        }
                    }
                    return(p);
                }
                else
                {
                    return(null);
                }
            }
        }
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            Page   pageToBeSerialized = (Page)value;
            string structType         = "";

            writer.WriteStartObject();
            writer.WritePropertyName("Parent");
            var parent = pageToBeSerialized.Parent as IStateObject;

            if (parent == null)
            {
                writer.WriteValue(pageToBeSerialized.Parent);
            }
            else
            {
                writer.WriteValue(parent.UniqueID);
            }
            writer.WritePropertyName("ParentType");
            writer.WriteValue(pageToBeSerialized.ParentType);
            writer.WritePropertyName("@array");
            writer.WriteStartArray();
            foreach (var item in pageToBeSerialized.GetStoredObjectsSafe())
            {
                LazyObject lazy = (LazyObject)item;
                if (lazy.TargetUniqueID != null)
                {
                    writer.WriteValue(lazy.TargetUniqueID);
                }
                else
                {
                    var i = lazy.Target;
                    if (i != null)
                    {
                        var type = i.GetType();
                        if (type == typeof(String))
                        {
                            writer.WriteValue("_@" + lazy.Target);
                        }
                        else if (!type.IsEnum && !type.IsPrimitive && type.IsValueType && type != typeof(DateTime))
                        {
                            //This is a struct
                            serializer.Serialize(writer, lazy.Target, type);
                            structType = type.AssemblyQualifiedName;
                        }
                        else
                        {
                            writer.WriteValue(lazy.Target);
                        }
                    }
                    else
                    {
                        writer.WriteNull();
                    }
                }
            }
            writer.WriteEndArray();
            if (structType != "")
            {
                writer.WritePropertyName("StructType");
                writer.WriteValue(structType);
            }
            writer.WriteEndObject();
        }