Skip to content

Commit daecbd3

Browse files
committed
Stripped Newtonsoft code broke the magic not mapped attribute ignore fix during serialization. This was fixing the issue.
1 parent eb02b95 commit daecbd3

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

Magic.IndexedDb/Helpers/MagicSerializationHelper.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,22 @@ public static string SerializeObject<T>(T value, MagicJsonSerializationSettings?
1717
if (value == null)
1818
throw new ArgumentNullException(nameof(value), "Object cannot be null");
1919

20-
return JsonSerializer.Serialize(value, settings.Options);
20+
var options = settings.GetOptionsWithResolver<T>(); // Ensure the correct resolver is applied
21+
22+
return JsonSerializer.Serialize(value, options);
2123
}
2224

23-
public static T? DeserializeObject<T>(string json)
25+
public static T? DeserializeObject<T>(string json, MagicJsonSerializationSettings? settings = null)
2426
{
2527
if (string.IsNullOrWhiteSpace(json))
2628
throw new ArgumentException("JSON cannot be null or empty.", nameof(json));
2729

28-
return JsonSerializer.Deserialize<T>(json);
30+
if (settings == null)
31+
settings = new MagicJsonSerializationSettings();
32+
33+
var options = settings.GetOptionsWithResolver<T>(); // Ensure correct resolver for deserialization
34+
35+
return JsonSerializer.Deserialize<T>(json, options);
2936
}
3037

3138
public static void PopulateObject<T>(T source, T target)

Magic.IndexedDb/Models/MagicJsonSerializationSettings.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Text.Json.Serialization;
6-
using System.Text.Json;
7-
using System.Threading.Tasks;
1+
using System.Text.Json;
82

93
namespace Magic.IndexedDb.Models
104
{
@@ -15,7 +9,7 @@ public class MagicJsonSerializationSettings
159
public JsonSerializerOptions Options
1610
{
1711
get => _options;
18-
set => _options = value ?? new JsonSerializerOptions(); // Ensure it never gets null
12+
set => _options = value ?? new JsonSerializerOptions(); // Ensure it's never null
1913
}
2014

2115
public bool UseCamelCase
@@ -29,5 +23,15 @@ public bool UseCamelCase
2923
};
3024
}
3125
}
26+
27+
/// <summary>
28+
/// Ensures the MagicContractResolver is applied for a specific type at runtime.
29+
/// </summary>
30+
public JsonSerializerOptions GetOptionsWithResolver<T>()
31+
{
32+
var newOptions = new JsonSerializerOptions(Options); // Clone settings
33+
newOptions.Converters.Add(new MagicContractResolver<T>()); // Ensure the correct resolver is added
34+
return newOptions;
35+
}
3236
}
3337
}

0 commit comments

Comments
 (0)