Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using AndreasReitberger.Shared.Core.Database.Service;
using AndreasReitberger.Shared.Core.SourceGeneration;

namespace AndreasReitberger.Shared.Core.Database.SourceGeneration
{
[JsonSerializable(typeof(SqlDatabaseService))]
[JsonSourceGenerationOptions(WriteIndented = true)]
public partial class SqlDatabaseSourceGenerationContext : MauiSourceGenerationContext { }
public partial class SqlDatabaseSourceGenerationContext : JsonSerializerContext { }

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using AndreasReitberger.Shared.Core.Database.Service;
using AndreasReitberger.Shared.Core.SourceGeneration;

namespace AndreasReitberger.Shared.Core.Database.SourceGeneration
{
[JsonSerializable(typeof(SqliteDatabaseService))]
[JsonSourceGenerationOptions(WriteIndented = true)]
public partial class SqliteDatabaseSourceGenerationContext : MauiSourceGenerationContext { }
public partial class SqliteDatabaseSourceGenerationContext : JsonSerializerContext { }

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ namespace AndreasReitberger.Shared.Core.Licensing.SourceGeneration
[JsonSerializable(typeof(WooActivationResponse[]))]
[JsonSerializable(typeof(WooCodeVersionResponse[]))]
[JsonSourceGenerationOptions(WriteIndented = true)]
public partial class LicenseSourceGenerationContext : CoreSourceGenerationContext { }
public partial class LicenseSourceGenerationContext : JsonSerializerContext { }

}
30 changes: 29 additions & 1 deletion src/SharedMauiCoreLibrary.Test/CoreTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using AndreasReitberger.Shared.Core.Utilities;
using AndreasReitberger.Shared.Core.SourceGeneration;
using AndreasReitberger.Shared.Core.Utilities;
using SharedMauiCoreLibrary.Test.Models;

namespace SharedMauiCoreLibrary.Test;

Expand Down Expand Up @@ -108,4 +110,30 @@ public void UsePasswordDoubleEncryptionTest()
Assert.Fail(ex.Message);
}
}

[Test]
public void JsonSerializerCombineTest()
{
try
{
TestModel tm = new()
{
Name = "test",
IsActive = true,
Start = DateTime.Now.AddHours(-1),
End = DateTime.Now,
Items = ["Item1", "Item2", "Item3"]
};
var combinedOptions = JsonOptionsProvider.CombinedOptions(SourceGeneration.TestContext.Default, CoreSourceGenerationContext.Default, MauiSourceGenerationContext.Default);
string? json = JsonConvertHelper.ToSettingsString(tm, options: combinedOptions);
Assert.That(!string.IsNullOrEmpty(json));

TestModel? tm2 = JsonConvertHelper.ToObject<TestModel>(json, options: combinedOptions);
Assert.That(tm2, Is.Not.Null);
}
catch (Exception ex)
{
Assert.Fail(ex.Message);
}
}
}
27 changes: 27 additions & 0 deletions src/SharedMauiCoreLibrary.Test/Models/TestModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using CommunityToolkit.Mvvm.ComponentModel;
using System.Collections.ObjectModel;

namespace SharedMauiCoreLibrary.Test.Models
{
public partial class TestModel : ObservableObject
{
#region Properties

[ObservableProperty]
public partial string Name { get; set; } = string.Empty;

[ObservableProperty]
public partial bool IsActive { get; set; }

[ObservableProperty]
public partial DateTimeOffset Start { get; set; }

[ObservableProperty]
public partial DateTimeOffset End { get; set; }

[ObservableProperty]
public partial ObservableCollection<string> Items { get; set; } = [];

#endregion
}
}
11 changes: 11 additions & 0 deletions src/SharedMauiCoreLibrary.Test/SourceGeneration/TestContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using SharedMauiCoreLibrary.Test.Models;
using System.Text.Json.Serialization;

namespace SharedMauiCoreLibrary.Test.SourceGeneration
{
[JsonSerializable(typeof(TestModel))]
[JsonSourceGenerationOptions(WriteIndented = true)]
public partial class TestContext : JsonSerializerContext
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ namespace AndreasReitberger.Shared.Core.SourceGeneration
[JsonSerializable(typeof(ThemeColorInfo))]
[JsonSerializable(typeof(ColorPickerElement))]
[JsonSourceGenerationOptions(WriteIndented = true)]
public partial class MauiSourceGenerationContext : CoreSourceGenerationContext { }
public partial class MauiSourceGenerationContext : JsonSerializerContext { }

}
60 changes: 54 additions & 6 deletions src/SharedNetCoreLibrary/Utilities/JsonConvertHelper.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,65 @@


#if NET6_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
#endif

namespace AndreasReitberger.Shared.Core.Utilities
{
public partial class JsonConvertHelper
{
#region Converts
public static T? ToObject<T>(string? jsonString, T? defaultValue = default, Action<Exception>? OnError = null, JsonSerializerContext? settings = null)
public static T? ToObject<T>(string? jsonString, T? defaultValue = default, Action<Exception>? OnError = null, JsonSerializerContext? context = null)
{
try
{
if (jsonString is null)
return defaultValue;
settings ??= CoreSourceGenerationContext.Default;
context ??= CoreSourceGenerationContext.Default;
// Check if it is saved as plain string. If so, just return the string
if (typeof(T) == typeof(string) && !(jsonString.StartsWith('"') && jsonString.EndsWith('"')))
return (T)Convert.ChangeType(jsonString, typeof(T));
else
return (T?)JsonSerializer.Deserialize(jsonString, typeof(T), settings) ?? defaultValue;
return (T?)JsonSerializer.Deserialize(jsonString, typeof(T), context) ?? defaultValue;
}
catch (Exception exc)
{
OnError?.Invoke(exc);
return defaultValue;
}
}

#if NET6_0_OR_GREATER
[RequiresUnreferencedCode("This function is not AOT safe. Use the `JsonSerializerContext` instead")]
[RequiresDynamicCode("This function is not AOT safe. Use the `JsonSerializerContext` instead")]
#endif
public static T? ToObject<T>(string? jsonString, JsonSerializerOptions? options, T? defaultValue = default, Action<Exception>? OnError = null)
{
try
{
if (jsonString is null)
return defaultValue;
options ??= CoreSourceGenerationContext.Default.Options;
// Check if it is saved as plain string. If so, just return the string
if (typeof(T) == typeof(string) && !(jsonString.StartsWith('"') && jsonString.EndsWith('"')))
return (T)Convert.ChangeType(jsonString, typeof(T));
else
return JsonSerializer.Deserialize<T>(jsonString, options) ?? defaultValue;
}
catch (Exception exc)
{
OnError?.Invoke(exc);
return defaultValue;
}
}

public static string? ToSettingsString<T>(T? settingsObject, string? defaultValue = default, Action<Exception>? OnError = null, JsonSerializerContext? context = null)
{
try
{
if (settingsObject is null) return defaultValue;
context ??= CoreSourceGenerationContext.Default;
return JsonSerializer.Serialize(settingsObject, typeof(T), context) ?? defaultValue;
}
catch (Exception exc)
{
Expand All @@ -24,13 +68,17 @@ public partial class JsonConvertHelper
}
}

public static string? ToSettingsString<T>(T? settingsObject, string? defaultValue = default, Action<Exception>? OnError = null, JsonSerializerContext? settings = null)
#if NET6_0_OR_GREATER
[RequiresUnreferencedCode("This function is not AOT safe. Use the `JsonSerializerContext` instead")]
[RequiresDynamicCode("This function is not AOT safe. Use the `JsonSerializerContext` instead")]
#endif
public static string? ToSettingsString<T>(T? settingsObject, JsonSerializerOptions? options, string? defaultValue = default, Action<Exception>? OnError = null)
{
try
{
if (settingsObject is null) return defaultValue;
settings ??= CoreSourceGenerationContext.Default;
return JsonSerializer.Serialize(settingsObject, typeof(T), settings) ?? defaultValue;
options ??= CoreSourceGenerationContext.Default.Options;
return JsonSerializer.Serialize(settingsObject, options) ?? defaultValue;
}
catch (Exception exc)
{
Expand Down
31 changes: 31 additions & 0 deletions src/SharedNetCoreLibrary/Utilities/JsonOptionsProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

using System.Text.Json.Serialization.Metadata;

namespace AndreasReitberger.Shared.Core.Utilities
{
public partial class JsonOptionsProvider
{
#region Converts

public static JsonSerializerOptions CombinedOptions(params IJsonTypeInfoResolver[] resolvers)
{
return new JsonSerializerOptions
{
TypeInfoResolver = CombineResolvers(resolvers)
};
}

public static IJsonTypeInfoResolver? CombineResolvers(params IJsonTypeInfoResolver[] resolvers)
{
try
{
return JsonTypeInfoResolver.Combine(resolvers);
}
catch (Exception)
{
return null;
}
}
#endregion
}
}
Loading