/// <summary>
        /// Serialize a container set instance
        /// </summary>
        private static async Task SerializeAsync(ContainerSet set, string fileName, string directoryFullPath, ISerializerFactory serializerFactory = default, BaseOrchestrator orchestrator = null)
        {
            if (set == null)
            {
                return;
            }

            var fullPath = Path.Combine(directoryFullPath, fileName);

            var fi = new FileInfo(fullPath);

            if (!Directory.Exists(fi.Directory.FullName))
            {
                Directory.CreateDirectory(fi.Directory.FullName);
            }

            if (serializerFactory == default)
            {
                serializerFactory = SerializersCollection.JsonSerializer;
            }

            var serializer = serializerFactory.GetSerializer <ContainerSet>();

            using var f = new FileStream(fullPath, FileMode.CreateNew, FileAccess.ReadWrite);

            byte[] serializedBytes = null;

            if (orchestrator != null)
            {
                var interceptorArgs = new SerializingSetArgs(orchestrator.GetContext(), set, serializerFactory, fileName, directoryFullPath);
                await orchestrator.InterceptAsync(interceptorArgs, default);

                serializedBytes = interceptorArgs.Result;
            }

            if (serializedBytes == null)
            {
                serializedBytes = await serializer.SerializeAsync(set);
            }


            f.Write(serializedBytes, 0, serializedBytes.Length);

            //await f.FlushAsync();
        }
Beispiel #2
0
        private static async Task <ContainerSet> DeserializeAsync(string fileName, string directoryFullPath, BaseOrchestrator orchestrator = null)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                throw new ArgumentNullException(fileName);
            }
            if (string.IsNullOrEmpty(directoryFullPath))
            {
                throw new ArgumentNullException(directoryFullPath);
            }

            var fullPath = Path.Combine(directoryFullPath, fileName);

            if (!File.Exists(fullPath))
            {
                throw new MissingFileException(fullPath);
            }

            var jsonConverter = new JsonConverter <ContainerSet>();

            //var jsonConverter = new Utf8JsonConverter<ContainerSet>();

            using var fs = new FileStream(fullPath, FileMode.Open, FileAccess.Read);

            ContainerSet set = null;

            if (orchestrator != null)
            {
                var interceptorArgs = new DeserializingSetArgs(orchestrator.GetContext(), fs, fileName, directoryFullPath);
                await orchestrator.InterceptAsync(interceptorArgs, default);

                set = interceptorArgs.Result;
            }

            if (set == null)
            {
                set = await jsonConverter.DeserializeAsync(fs);
            }

            //await fs.FlushAsync();

            return(set);
        }
Beispiel #3
0
        /// <summary>
        /// Serialize a container set instance
        /// </summary>
        private static async Task SerializeAsync(ContainerSet set, string fileName, string directoryFullPath, BaseOrchestrator orchestrator = null)
        {
            if (set == null)
            {
                return;
            }

            var fullPath = Path.Combine(directoryFullPath, fileName);

            var fi = new FileInfo(fullPath);

            if (!Directory.Exists(fi.Directory.FullName))
            {
                Directory.CreateDirectory(fi.Directory.FullName);
            }

            // Serialize on disk.
            // var jsonConverter = new JsonConverter<ContainerSet>();
            var jsonConverter = new Utf8JsonConverter <ContainerSet>();

            using var f = new FileStream(fullPath, FileMode.CreateNew, FileAccess.ReadWrite);

            byte[] serializedBytes = null;

            if (orchestrator != null)
            {
                var interceptorArgs = new SerializingSetArgs(orchestrator.GetContext(), set, fileName, directoryFullPath);
                await orchestrator.InterceptAsync(interceptorArgs, default);

                serializedBytes = interceptorArgs.Result;
            }

            if (serializedBytes == null)
            {
                serializedBytes = await jsonConverter.SerializeAsync(set);
            }


            f.Write(serializedBytes, 0, serializedBytes.Length);
        }
        private async Task <ContainerSet> DeserializeAsync(string fileName, string directoryFullPath, ISerializerFactory serializerFactory = default, BaseOrchestrator orchestrator = null)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                throw new ArgumentNullException(fileName);
            }
            if (string.IsNullOrEmpty(directoryFullPath))
            {
                throw new ArgumentNullException(directoryFullPath);
            }

            var fullPath = Path.Combine(directoryFullPath, fileName);

            if (!File.Exists(fullPath))
            {
                throw new MissingFileException(fullPath);
            }

            // backward compatibility
            if (serializerFactory == default)
            {
                serializerFactory = SerializersCollection.JsonSerializer;
            }

            // backward compatibility
            if (this.SerializedType == default)
            {
                this.SerializedType = typeof(ContainerSet);
            }

            Debug.WriteLine($"Deserialize file {fileName}");

            using var fs = new FileStream(fullPath, FileMode.Open, FileAccess.Read);

            ContainerSet set = null;

            if (orchestrator != null)
            {
                var interceptorArgs = new DeserializingSetArgs(orchestrator.GetContext(), fs, serializerFactory, fileName, directoryFullPath);
                await orchestrator.InterceptAsync(interceptorArgs, default);

                set = interceptorArgs.Result;
            }


            if (set == null)
            {
                if (this.SerializedType == typeof(ContainerSet))
                {
                    var serializer = serializerFactory.GetSerializer <ContainerSet>();
                    set = await serializer.DeserializeAsync(fs);
                }
                else
                {
                    var serializer = serializerFactory.GetSerializer <ContainerSetBoilerPlate>();
                    var jobject    = await serializer.DeserializeAsync(fs);

                    set = jobject.Changes;
                }
            }

            return(set);
        }