/// <summary> /// Creates an instance of the <see cref="Manifest"/> class based on /// the provided JSON string. /// </summary> /// <param name="json">A string of JSON in the correct format.</param> /// <param name="dependencies">The host provided dependencies.</param> /// <returns></returns> internal static Manifest FromJson(string json, IDependencies dependencies) { try { LibraryIdToNameAndVersionConverter.Instance.EnsureInitialized(dependencies); ManifestOnDisk manifestOnDisk = JsonConvert.DeserializeObject <ManifestOnDisk>(json); var manifestConverter = new ManifestToFileConverter(); Manifest manifest = manifestConverter.ConvertToManifest(manifestOnDisk, dependencies); manifest._hostInteraction = dependencies.GetHostInteractions(); return(manifest); } catch (Exception) { return(null); } }
/// <summary> /// Saves the manifest file to disk. /// </summary> /// <param name="fileName">The absolute file path to save the <see cref="Manifest"/> to.</param> /// <param name="cancellationToken">A token that allows for cancellation of the operation.</param> /// <returns></returns> public async Task SaveAsync(string fileName, CancellationToken cancellationToken) { var settings = new JsonSerializerSettings { Formatting = Formatting.Indented, NullValueHandling = NullValueHandling.Ignore, }; foreach (ILibraryInstallationState library in _libraries) { if (library is LibraryInstallationState state) { if (state.IsUsingDefaultDestination) { state.DestinationPath = null; } if (state.IsUsingDefaultProvider) { state.ProviderId = null; } } } ManifestOnDisk manifestOnDisk = new ManifestToFileConverter().ConvertToManifestOnDisk(this); string json = JsonConvert.SerializeObject(manifestOnDisk, settings); UpdateLibraryProviderAndDestination(this); byte[] buffer = Encoding.UTF8.GetBytes(json); using (FileStream writer = File.Create(fileName, 4096, FileOptions.Asynchronous)) { await writer.WriteAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false); } }