Skip to content

Commit

Permalink
Allow our DeserializeJsonResource method to accept customized setti…
Browse files Browse the repository at this point in the history
…ngs.
  • Loading branch information
Tkael committed Nov 20, 2022
1 parent 1bbe49b commit dabe46c
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions Tests/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using EddiConfigService;
using EddiCore;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO;

Expand Down Expand Up @@ -34,19 +35,22 @@ internal void MakeSafe()
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times")]
public static T DeserializeJsonResource<T>(byte[] data) where T : class
public static T DeserializeJsonResource<T>(byte[] data, JsonSerializerSettings settings = null) where T : class
{
using (var stream = new MemoryStream(data))
{
using (var reader = new StreamReader(stream, System.Text.Encoding.UTF8))
{
var jsonSerializer = settings is null
? JsonSerializer.Create()
: JsonSerializer.Create(settings);
if (typeof(T) == typeof(string))
{
return Newtonsoft.Json.JsonSerializer.Create().Deserialize(reader, typeof(JObject)).ToString() as T;
return jsonSerializer.Deserialize(reader, typeof(JObject)).ToString() as T;
}
else
{
return Newtonsoft.Json.JsonSerializer.Create().Deserialize(reader, typeof(T)) as T;
return jsonSerializer.Deserialize(reader, typeof(T)) as T;
}
}
}
Expand Down

0 comments on commit dabe46c

Please sign in to comment.