/// <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 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); } return(set); }