Efficient way of storing and retrieving Large Json ( 100 mb +) from a file using this article and Json.Net

爱⌒轻易说出口 提交于 2019-12-04 19:29:00

As i did not need to read the serialized file manually, Using Protocol Buffer solved the problem, It takes 6 seconds to serialize /deserialize the same amount of data.

It's available on Nuget at http://www.nuget.org/packages/protobuf-net

    public static async Task<T> DeserializeFromBinary<T>(string filename, StorageFolder storageFolder)
    {
        try
        {

            var file = await storageFolder.GetFileAsync(filename);
            if (file != null)
            {
                var stream = await file.OpenStreamForReadAsync();
                T content = Serializer.Deserialize<T>(stream);
                return content;
            }
        }

        catch (NullReferenceException nullException)
        {
            logger.LogError("Exception happened while de-serializing input object, Error: " + nullException.Message);
        }
        catch (FileNotFoundException fileNotFound)
        {
            logger.LogError("Exception happened while de-serializing input object, Error: " + fileNotFound.Message);
        }
        catch (Exception e)
        {
            logger.LogError("Exception happened while de-serializing input object, Error: " + e.Message, e.ToString());
        }
        return default(T);
    }


    public static async Task<bool> SerializeIntoBinary<T>(string fileName, StorageFolder destinationFolder, Content content)
    {
        bool shouldRetry = true;
        while (shouldRetry)
        {
            try
            {

                StorageFile file = await destinationFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);

                using (var stream = await file.OpenStreamForWriteAsync())
                {
                    Serializer.Serialize(stream, content);
                    shouldRetry = false;
                    return true;
                }
            }
            catch (UnauthorizedAccessException unAuthorizedAccess)
            {
                logger.LogError("UnauthorizedAccessException happened while serializing input object, will wait for 5 seconds, Error: " + unAuthorizedAccess.Message);
                shouldRetry = true;
            }
            catch (NullReferenceException nullException)
            {
                shouldRetry = false;
                logger.LogError("Exception happened while serializing input object, Error: " + nullException.Message);
            }
            catch (Exception e)
            {
                shouldRetry = false;
                logger.LogError("Exception happened while serializing input object, Error: " + e.Message, e.ToString());
            }

            if (shouldRetry)
                await Task.Delay(5000);
        }
        return false;
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!