2020-04-07 10:19:14 -07:00
// 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.
2025-12-02 16:31:02 +08:00
using System ;
2020-04-02 06:08:56 -07:00
using System.Text.Json ;
2020-07-21 14:06:39 -07:00
using System.Text.Json.Serialization ;
2020-04-02 06:08:56 -07:00
2020-10-22 09:45:48 -07:00
namespace Microsoft.PowerToys.Settings.UI.Library
2020-04-02 06:08:56 -07:00
{
2025-12-02 16:31:02 +08:00
/// <summary>
/// Base class for all PowerToys module settings.
/// </summary>
/// <remarks>
/// <para><strong>IMPORTANT for Native AOT compatibility:</strong></para>
/// <para>When creating a new class that inherits from <see cref="BasePTModuleSettings"/>,
/// you MUST register it in <see cref="SettingsSerializationContext"/> by adding a
/// <c>[JsonSerializable(typeof(YourNewSettingsClass))]</c> attribute.</para>
/// <para>Failure to register the type will cause <see cref="ToJsonString"/> to throw
/// <see cref="InvalidOperationException"/> at runtime.</para>
/// <para>See <see cref="SettingsSerializationContext"/> for registration instructions.</para>
/// </remarks>
2020-04-02 06:08:56 -07:00
public abstract class BasePTModuleSettings
{
2025-12-02 16:31:02 +08:00
// Cached JsonSerializerOptions for Native AOT compatibility
private static readonly JsonSerializerOptions _jsonSerializerOptions = new JsonSerializerOptions
{
TypeInfoResolver = SettingsSerializationContext . Default ,
} ;
2020-04-07 10:19:14 -07:00
// Gets or sets name of the powertoy module.
2020-07-21 14:06:39 -07:00
[JsonPropertyName("name")]
public string Name { get ; set ; }
2020-04-07 10:19:14 -07:00
// Gets or sets the powertoys version.
2020-07-21 14:06:39 -07:00
[JsonPropertyName("version")]
public string Version { get ; set ; }
2020-04-02 06:08:56 -07:00
2025-12-02 16:31:02 +08:00
/// <summary>
/// Converts the current settings object to a JSON string.
/// </summary>
/// <returns>JSON string representation of this settings object.</returns>
/// <exception cref="InvalidOperationException">
/// Thrown when the runtime type is not registered in <see cref="SettingsSerializationContext"/>.
/// All derived types must be registered with <c>[JsonSerializable(typeof(YourType))]</c> attribute.
/// </exception>
/// <remarks>
/// This method uses Native AOT-compatible JSON serialization. The runtime type must be
/// registered in <see cref="SettingsSerializationContext"/> for serialization to work.
/// </remarks>
2020-04-02 06:08:56 -07:00
public virtual string ToJsonString ( )
{
2020-07-23 16:04:04 -07:00
// 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)
2025-12-02 16:31:02 +08:00
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 ) ;
2020-04-02 06:08:56 -07:00
}
2021-03-24 16:13:33 +02:00
public override int GetHashCode ( )
{
return ToJsonString ( ) . GetHashCode ( ) ;
}
public override bool Equals ( object obj )
{
var settings = obj as BasePTModuleSettings ;
return settings ? . ToJsonString ( ) = = ToJsonString ( ) ;
}
2020-04-02 06:08:56 -07:00
}
}