Skip to content

Commit f5ee69f

Browse files
author
Gonzalo Diaz
committed
[FIX] New utility to encapsulate JSON loading
1 parent aad35a4 commit f5ee69f

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit;
2+
3+
using System.Reflection;
4+
using System.Text;
5+
using Newtonsoft.Json;
6+
7+
public static class JsonLoader
8+
{
9+
public static T? resourceLoad<T>(string _path)
10+
{
11+
string path = _path;
12+
path = path.Replace('/', '.');
13+
path = path.Replace('\\', '.');
14+
15+
var info = Assembly.GetExecutingAssembly().GetName();
16+
var name = info.Name;
17+
18+
path = $"{name}.Resources.{path}";
19+
Log.debug($"Loading JSON from: {path}");
20+
21+
using var stream = Assembly
22+
.GetExecutingAssembly()
23+
.GetManifestResourceStream($"{path}")!;
24+
25+
26+
using var streamReader = new StreamReader(stream, Encoding.UTF8);
27+
28+
return JsonConvert.DeserializeObject<T>(
29+
streamReader.ReadToEnd()
30+
);
31+
}
32+
}

0 commit comments

Comments
 (0)