// Copyright (C) 2024, The Duplicati Team // https://duplicati.com, hello@duplicati.com // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Threading.Tasks; using Duplicati.Library.Interface; using Newtonsoft.Json; using Newtonsoft.Json.Converters; using Newtonsoft.Json.Serialization; namespace Duplicati.Library.Modules.Builtin.ResultSerialization { /// /// Implements serialization of results to JSON /// public class JsonFormatSerializer : IResultFormatSerializer { /// /// Helper to filter the result classes properties /// private class DynamicContractResolver : DefaultContractResolver { /// /// List of names to exclude /// private readonly HashSet m_excludes; /// /// Initializes a new instance of the /// class. /// /// The names to exclude. public DynamicContractResolver(params string[] names) { m_excludes = new HashSet(names); } /// /// Creates a filtered list of properties /// /// The filtered properties. /// The type to create the list for. /// Member serialization parameter. protected override IList CreateProperties(Type type, MemberSerialization memberSerialization) { return base .CreateProperties(type, memberSerialization) .Where(x => !m_excludes.Contains(x.PropertyName)) .Where(x => !typeof(Task).IsAssignableFrom(x.PropertyType)) .ToList(); } } /// /// Serialize the specified result and logLines. /// /// The serialized result string. /// The result to serialize. /// The exception, if any /// The log lines to serialize. /// Additional parameters to include public string Serialize(object result, Exception exception, IEnumerable loglines, Dictionary additional) { return JsonConvert.SerializeObject( new { Data = result, Extra = additional, LogLines = loglines, Exception = exception?.ToString() }, new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore, ContractResolver = new DynamicContractResolver( nameof(IBasicResults.Warnings), nameof(IBasicResults.Errors), nameof(IBasicResults.Messages), "TaskReader" ), Converters = new List() { new StringEnumConverter() } } ); } public string SerializeResults(IBasicResults result) { return JsonConvert.SerializeObject( result, new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore, ContractResolver = new DynamicContractResolver( nameof(IBackendStatstics), "TaskReader" ), Converters = new List() { new StringEnumConverter() } } ); } /// /// Returns the format that the serializer represents /// public ResultExportFormat Format => ResultExportFormat.Json; } }