Beispiel #1
0
        public byte[] Serialize(IDistributedPropertySet data)
        {
            var targetData = new Dictionary <string, KeyValuePair <string, object> >();

            data.ToList().ForEach(pair => targetData.Add(pair.Key, new KeyValuePair <string, object>(pair.Value.GetType().FullName, pair.Value)));
            return(JsonSerializer.SerializeToUtf8Bytes(targetData, Options));
        }
Beispiel #2
0
        public string WriteFile(string name, string path, IDistributedPropertySet data)
        {
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            var filePath = Path.Combine(path, name + $".{FileTypes.Binary}");

            var serializer = Provider.GetService <BinarySettingsSerializer>();

            var bytes = serializer.Serialize(data);

            Logger.LogInformation($"Serialized Length: {bytes.Length}");

            try
            {
                System.IO.File.WriteAllBytes(filePath, bytes);

                Logger.LogDebug($"Write {bytes.Length} bytes to {filePath}.");

                return(filePath);
            }
            catch (Exception e)
            {
                Logger.LogCritical(e, "Exception caught writing Binary file.");
                throw;
            }
        }
Beispiel #3
0
        private DistributedApplicationDataContainer GetContainer(
            IDistributedPropertySet data
            , SettingsScopes settingsScope
            , string name = "Temporary")
        {
            switch (settingsScope)
            {
            case SettingsScopes.Local:
                DistributedApplicationData.Current.LocalSettings.Values.SetValues(data);
                return(DistributedApplicationData.Current.LocalSettings);

            case SettingsScopes.Roaming:
                DistributedApplicationData.Current.RoamingSettings.Values.SetValues(data);
                return(DistributedApplicationData.Current.RoamingSettings);

            case SettingsScopes.Both:
                return(null);

            default:
            {
                var container = DistributedApplicationData.Current.LocalSettings.CreateContainer(name,
                                                                                                 DistributedApplicationDataLocality.Temporary);

                container.Values.SetValues(data);
                return(container);
            }
            }
        }
        public void DeserializeTest(DistributedPropertySet data, ISettingsSerializer serializer)
        {
            IDistributedPropertySet result = null;
            var config = TestStartup.Configuration;

            try
            {
                result = serializer.Deserialize(serializer.Serialize(data));
            }
            catch (Exception e)
            {
                _output.WriteLine(e.ToString());
                e.Data.Keys.Cast <string>().ToList().ForEach(key => _output.WriteLine(e.Data[key].ToString()));
                throw;
            }

            Assert.NotNull(result);

            foreach (var key in data.Keys)
            {
                Assert.True(result.ContainsKey(key));
                _output.WriteLine($"{key}: {data[key]} {(data[key].Equals(result[key]) ? "==" : "!=")} {result[key]}");
                Assert.Equal(data[key], result[key]);
            }
        }
Beispiel #5
0
        public DistributedApplicationDataContainer WriteFile(string name, string path, SettingsScopes settingsScope,
                                                             IDistributedPropertySet currentValues)
        {
            Writer.WriteFile(name, path, currentValues);

            return(settingsScope == SettingsScopes.Local
                ? DistributedApplicationData.Current.LocalSettings
                : DistributedApplicationData.Current.RoamingSettings);
        }
Beispiel #6
0
        public Metadata([CallerMemberName] string appName            = null
                        , IDistributedPropertySet defaultLocalData   = null
                        , IDistributedPropertySet defaultRoamingData = null)
        {
            AppName = appName;

            var blankFiles = new ConcurrentDictionary <SettingsScopes, IDistributedPropertySet>();

            blankFiles.TryAdd(SettingsScopes.Local, defaultLocalData);
            blankFiles.TryAdd(SettingsScopes.Roaming, defaultRoamingData);

            BlankFile = blankFiles;
        }
Beispiel #7
0
        public DistributedApplicationDataContainer CreateFile(string name, string path,
                                                              SettingsScopes settingsScope,
                                                              IDistributedPropertySet seedValues = null)
        {
            var filePath = Path.Combine(path, $"{name}.{FileExtension}");

            if (!File.Exists(filePath))
            {
                Writer.WriteFile(name, path, seedValues);
            }

            return(OpenFile(name, path, settingsScope));
        }
        public byte[] Serialize(IDistributedPropertySet data)
        {
            using var memoryStream = new MemoryStream();
            using var xmlWriter    = new XmlTextWriter(memoryStream, Encoding.Default)
                  {
                      Formatting = Formatting.Indented
                  };

            data.WriteXml(xmlWriter);

            memoryStream.Seek(0, SeekOrigin.Begin);

            return(memoryStream.GetBuffer());
        }
        public byte[] Serialize(IDistributedPropertySet data)
        {
            var targetData = new Dictionary <string, Pair>();

            data.ToList().ForEach(pair => targetData.Add(pair.Key, new Pair {
                Key = pair.Value.GetType().FullName, Value = pair.Value
            }));

            var sb = new StringBuilder();

            using var textWriter = new StringWriter(sb);
            new Serializer().Serialize(textWriter, targetData);

            return(Encoding.UTF8.GetBytes(sb.ToString()));
        }
        public byte[] Serialize(IDistributedPropertySet data)
        {
            using var stream = new MemoryStream();

            var formatter = new BinaryFormatter
            {
                AssemblyFormat = FormatterAssemblyStyle.Simple,
                FilterLevel    = TypeFilterLevel.Full,
                TypeFormat     = FormatterTypeStyle.TypesWhenNeeded
            };

            formatter.Serialize(stream, data);

            stream.Seek(0, SeekOrigin.Begin);

            return(stream.GetBuffer());
        }
        public void SerializeTest(IDistributedPropertySet data, ISettingsSerializer serializer)
        {
            byte[] result = null;

            try
            {
                result = serializer.Serialize(data);

                _output.WriteLine(Encoding.UTF8.GetString(result));
            }
            catch (Exception e)
            {
                _output.WriteLine(e.ToString());
                e.Data.Keys.Cast <string>().ToList().ForEach(key => _output.WriteLine(e.Data[key].ToString()));
                throw;
            }

            Assert.NotNull(result);
            Assert.NotEmpty(result);
        }
Beispiel #12
0
        public string WriteFile(string name, string path, IDistributedPropertySet data)
        {
            var filePath = Path.Combine(path, name + ".yaml");

            var serializer = Provider.GetService <YamlSettingsSerializer>();

            try
            {
                var yaml = serializer.Serialize(data);

                File.WriteAllBytes(filePath, yaml);

                Logger.LogDebug($"Wrote {yaml.Length} bytes to {filePath}.");
                Logger.LogDebug(Encoding.UTF8.GetString(yaml));

                return(filePath);
            }
            catch (Exception e)
            {
                Logger.LogCritical(e, "Exception serializing to Yaml.");
                throw;
            }
        }
        public string WriteFile(string name, string path, IDistributedPropertySet data)
        {
            var filePath = Path.Combine(path, name + ".json");

            var serializer = Provider.GetService <JsonSettingsSerializer>();

            var bytes = serializer.Serialize(data);

            Logger.LogInformation($"Serialized Length: {bytes.Length}");

            try
            {
                System.IO.File.WriteAllBytes(filePath, bytes);

                Logger.LogDebug($"Write {bytes.Length} bytes to {filePath}.");

                return(filePath);
            }
            catch (Exception e)
            {
                Logger.LogCritical(e, "Exception caught writing JSON file.");
                throw;
            }
        }