コード例 #1
0
        public static async Task <Exception> DeserializeExceptionAsync(ContentType contentType, Stream stream)
        {
            switch (contentType)
            {
            case ContentType.Bytes:
            {
                var serializer = new ByteSerializer();
                var model      = await serializer.DeserializeAsync <ExceptionModel>(stream);

                return(new Exception(model.Message));
            }

            case ContentType.Json:
            {
                var model = await System.Text.Json.JsonSerializer.DeserializeAsync <ExceptionModel>(stream);

                return(new Exception(model.Message));
            }

            case ContentType.JsonNameless:
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    await stream.CopyToAsync(ms);

                    var model = JsonSerializer.DeserializeNameless <ExceptionModel>(ms.ToArray());
                    return(new Exception(model.Message));
                }
            }

            default:
                throw new NotImplementedException();
            }
        }
コード例 #2
0
        public static async Task <T> DeserializeAsync <T>(ContentType contentType, Stream stream)
        {
            switch (contentType)
            {
            case ContentType.Bytes:
            {
                var serializer = new ByteSerializer(true, false, true);
                return(await serializer.DeserializeAsync <T>(stream));
            }

            case ContentType.Json:
            {
                return(await System.Text.Json.JsonSerializer.DeserializeAsync <T>(stream));
            }

            case ContentType.JsonNameless:
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    await stream.CopyToAsync(ms);

                    return(JsonSerializer.DeserializeNameless <T>(ms.ToArray()));
                }
            }

            default:
                throw new NotImplementedException();
            }
        }
コード例 #3
0
        public void StreamDeserializeArray()
        {
            var serializer = new ByteSerializer();
            var model1     = Factory.GetArrayModel();
            var bytes      = serializer.Serialize(model1);

            using (var ms = new MemoryStream(bytes))
            {
                var model2 = serializer.DeserializeAsync <BasicModel[]>(ms).GetAwaiter().GetResult();
                Factory.AssertAreEqual(model1, model2);
            }
        }
コード例 #4
0
        public void StreamDeserializeByPropertyName()
        {
            var serializer = new ByteSerializer(true);
            var model1     = Factory.GetAllTypesModel();
            var bytes      = serializer.Serialize(model1);

            using (var ms = new MemoryStream(bytes))
            {
                var model2 = serializer.DeserializeAsync <AllTypesReversedModel>(ms).GetAwaiter().GetResult();
                Factory.AssertAreEqual(model1, model2);
            }
        }
コード例 #5
0
        public void StreamTypes()
        {
            var serializer = new ByteSerializer();
            var model1     = Factory.GetAllTypesModel();

            using (var ms = new MemoryStream())
            {
                serializer.SerializeAsync(ms, model1).GetAwaiter().GetResult();
                ms.Position = 0;
                var model2 = serializer.DeserializeAsync <AllTypesModel>(ms).GetAwaiter().GetResult();
                Factory.AssertAreEqual(model1, model2);
            }
        }
コード例 #6
0
ファイル: EncryptionTest.cs プロジェクト: szawaski/Zerra
        public void WithSerializer()
        {
            var key = SymmetricEncryptor.GenerateKey(SymmetricAlgorithmType.AESwithShift);

            var serializer = new ByteSerializer();
            var model1     = Factory.GetAllTypesModel();

            using (var ms = new MemoryStream())
                using (var cryptoStreamWriter = SymmetricEncryptor.Encrypt(SymmetricAlgorithmType.AESwithShift, key, ms, true, false))
                    using (var cryptoStreamReader = SymmetricEncryptor.Decrypt(SymmetricAlgorithmType.AESwithShift, key, ms, false, false))
                    {
                        var expected = serializer.Serialize(model1);
                        serializer.SerializeAsync(cryptoStreamWriter, model1).GetAwaiter().GetResult();
                        cryptoStreamWriter.FlushFinalBlock();
                        ms.Position = 0;
                        var bytes  = ms.ToArray();
                        var model2 = serializer.DeserializeAsync <AllTypesModel>(cryptoStreamReader).GetAwaiter().GetResult();
                        Factory.AssertAreEqual(model1, model2);
                    }
        }
コード例 #7
0
        public static async Task LoadState()
        {
            await stateLock.WaitAsync();

            try
            {
                var path = $"{GetAssemblyLocation()}\\relaystate.dat";
                if (File.Exists(path))
                {
                    ServiceInfo[] infoArray = null;
                    try
                    {
                        using (var file = File.OpenRead(path))
                        {
                            var serializer = new ByteSerializer();
                            infoArray = await serializer.DeserializeAsync <ServiceInfo[]>(file);
                        }
                    }
                    catch
                    {
                        try
                        {
                            File.Delete(path);
                        }
                        catch { }
                    }
                    if (infoArray != null)
                    {
                        foreach (var info in infoArray)
                        {
                            AddOrUpdate(info, false);
                        }
                    }
                }
            }
            finally
            {
                stateLock.Release();
            }
        }