Skip to content

Commit

Permalink
GitHubContent: simplify json conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
meee1 committed Jul 24, 2023
1 parent 95b77f7 commit 162b622
Showing 1 changed file with 2 additions and 26 deletions.
28 changes: 2 additions & 26 deletions ExtLibs/Utilities/GitHubContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,6 @@ public partial class Links
public enum TypeEnum { Dir, File };
}

static T GetObject<T>(Dictionary<string, object> dict)
{
Type type = typeof (T);
var obj = Activator.CreateInstance(type);

foreach (var kv in dict)
{
try
{
if (type.GetField(kv.Key) != null)
type.GetField(kv.Key).SetValue(obj, kv.Value);
if (type.GetProperty(kv.Key) != null)
type.GetProperty(kv.Key).SetValue(obj, kv.Value, null);
}
catch { }
}
return (T) obj;
}

public static List<FileInfo> GetDirContent(string owner, string repo, string path, string filter = "")
{
if (path != "")
Expand All @@ -87,15 +68,10 @@ public static List<FileInfo> GetDirContent(string owner, string repo, string pat
client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko");
string content = client.GetStringAsync(url).GetAwaiter().GetResult();

var output = JsonConvert.DeserializeObject<object[]>(content);
var output = JsonConvert.DeserializeObject<FileInfo[]>(content);

foreach (JObject itemjobject in output)
foreach (var fi in output)
{
var item = itemjobject.ToObject<Dictionary<string, object>>();
FileInfo fi = (FileInfo) GetObject<FileInfo>(item);
// string t1 = item["type"].ToString();
// string t2 =item["path"].ToString();

if (fi.name.ToLower().Contains(filter.ToLower()))
{
answer.Add(fi);
Expand Down

0 comments on commit 162b622

Please sign in to comment.