// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Microsoft.PowerToys.Settings.UI.Library
{
///
/// Base class for all PowerToys module settings.
///
///
/// IMPORTANT for Native AOT compatibility:
/// When creating a new class that inherits from ,
/// you MUST register it in by adding a
/// [JsonSerializable(typeof(YourNewSettingsClass))] attribute.
/// Failure to register the type will cause to throw
/// at runtime.
/// See for registration instructions.
///
public abstract class BasePTModuleSettings
{
// Cached JsonSerializerOptions for Native AOT compatibility
private static readonly JsonSerializerOptions _jsonSerializerOptions = new JsonSerializerOptions
{
TypeInfoResolver = SettingsSerializationContext.Default,
};
// Gets or sets name of the powertoy module.
[JsonPropertyName("name")]
public string Name { get; set; }
// Gets or sets the powertoys version.
[JsonPropertyName("version")]
public string Version { get; set; }
///
/// Converts the current settings object to a JSON string.
///
/// JSON string representation of this settings object.
///
/// Thrown when the runtime type is not registered in .
/// All derived types must be registered with [JsonSerializable(typeof(YourType))] attribute.
///
///
/// This method uses Native AOT-compatible JSON serialization. The runtime type must be
/// registered in for serialization to work.
///
public virtual string ToJsonString()
{
// By default JsonSerializer will only serialize the properties in the base class. This can be avoided by passing the object type (more details at https://stackoverflow.com/a/62498888)
var runtimeType = GetType();
// For Native AOT compatibility, get JsonTypeInfo from the TypeInfoResolver
var typeInfo = _jsonSerializerOptions.TypeInfoResolver?.GetTypeInfo(runtimeType, _jsonSerializerOptions);
if (typeInfo == null)
{
throw new InvalidOperationException($"Type {runtimeType.FullName} is not registered in SettingsSerializationContext. Please add it to the [JsonSerializable] attributes.");
}
// Use AOT-friendly serialization
return JsonSerializer.Serialize(this, typeInfo);
}
public override int GetHashCode()
{
return ToJsonString().GetHashCode();
}
public override bool Equals(object obj)
{
var settings = obj as BasePTModuleSettings;
return settings?.ToJsonString() == ToJsonString();
}
}
}