Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
        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);
        }