Author : MD TAREQ HASSAN | Updated : 2021/11/16
GitHub GraphQL API Explorer
Top panel (query):
query($number_of_repos:Int!){
viewer {
repositories(last: $number_of_repos) {
nodes {
name,
createdAt,
updatedAt,
url
}
}
}
}
Bottom panel (query variables)
{
"number_of_repos": 100
}
Deserialize from json file
Json to CSharp class
- Go to: https://jsonformatter.org/
- Paste json (
{ "nodes" : [...] }
) from GitHub GraphQL API Explorer - Format/Beautify
- Create
github-repos.json
file inwwwroot/sample-data/
- Paste formatted json string (paste only list i.e.
[ ... ]
, see sample json file below) - Go to: https://json2csharp.com/
- Get CSharp class from single json object i.e.
{ ... }
- Rename classes if needed (check if property names start with small letter -> make it capital letter if needed)
Models
Repository.cs
public class Repository
{
public string name { get; set; }
public DateTime createdAt { get; set; }
public DateTime updatedAt { get; set; }
public string url { get; set; }
}
Deserialize
using System.Threading.Tasks;
using System.Net.Http;
using System.Net.Http.Json;
protected List<Repository> Repositories { get; set; }
Repositories = await Http.GetFromJsonAsync<List<Repository>>("sample-data/github-repos.json");
Debug.WriteLine($"Repos Count: {Repositories.Count}");
Deserialize from excaped string
Json to CSharp class
- Go to: https://jsonformatter.org/
- Paste json (
{ "nodes" : [...] }
) from GitHub GraphQL API Explorer - Modify as:
{ "Repositories" : [...] }
and Format/Beautify - Go to: https://json2csharp.com/
- Get CSharp classes from formatted json string
- Rename classes if needed (check if property names start with small letter -> make it capital letter if needed)
Minify JSON and escape json string
- Go to: https://jsonformatter.org/
- Paste JSON string and Minify/Compact
- Go to: https://jsonformatter.org/json-escape
- Paste minified json string and create escaped string
Deserialize JSON to CSharp object
- Use a string readonly variable to hold escaped JSON string i.e.
string readonly JsonString = " <escaped string here> "
- Deserialize:
var repoList = JsonSerializer.Deserialize<Foo>(JsonString)
C# classes
using System;
using System.Collections.Generic;
namespace Foo.Models
{
public class Repository
{
public string name { get; set; }
public DateTime createdAt { get; set; }
public DateTime updatedAt { get; set; }
public string url { get; set; }
}
public class GithubRepoInfo
{
public List<Repository> repositories { get; set; }
}
}
Deserialize to C# object list
using System.Collections.Generic;
using System.Text.Json;
protected GithubRepoInfo GithubRepoInfo { get; set; }
protected List<Repository> RepoList { get; set; }
GithubRepoInfo = JsonSerializer.Deserialize<GithubRepoInfo>(JsonString);
RepoList = GithubRepoInfo.Repositories;
Sample json file
wwwroot/sample-data/github-repos.json
[
{
"name": "code-sharp",
"createdAt": "2017-02-11T06:33:58Z",
"updatedAt": "2020-02-15T05:48:58Z",
"url": "https://github.com/hovermind/code-sharp"
},
{
"name": "wpf-ninja",
"createdAt": "2017-03-27T05:33:54Z",
"updatedAt": "2020-09-05T08:48:09Z",
"url": "https://github.com/hovermind/wpf-ninja"
},
{
"name": "entity-flow",
"createdAt": "2017-03-27T05:46:00Z",
"updatedAt": "2019-01-03T19:50:22Z",
"url": "https://github.com/hovermind/entity-flow"
},
{
"name": "active-mvc",
"createdAt": "2017-03-30T02:20:39Z",
"updatedAt": "2017-03-31T14:21:53Z",
"url": "https://github.com/hovermind/active-mvc"
},
{
"name": "swiftly-swift",
"createdAt": "2017-09-01T05:44:40Z",
"updatedAt": "2020-04-01T09:23:32Z",
"url": "https://github.com/hovermind/swiftly-swift"
},
{
"name": "json-editor",
"createdAt": "2017-10-19T01:06:51Z",
"updatedAt": "2018-04-25T09:33:32Z",
"url": "https://github.com/hovermind/json-editor"
},
{
"name": "couchbase-lite-net",
"createdAt": "2017-12-06T01:02:16Z",
"updatedAt": "2017-12-06T01:02:20Z",
"url": "https://github.com/hovermind/couchbase-lite-net"
},
{
"name": "pi-sheet",
"createdAt": "2017-12-27T00:32:20Z",
"updatedAt": "2020-10-24T05:58:04Z",
"url": "https://github.com/hovermind/pi-sheet"
},
{
"name": "mongo-monk",
"createdAt": "2018-02-27T13:08:00Z",
"updatedAt": "2018-08-10T02:32:37Z",
"url": "https://github.com/hovermind/mongo-monk"
},
{
"name": "algos",
"createdAt": "2018-03-28T15:00:29Z",
"updatedAt": "2020-03-25T13:51:42Z",
"url": "https://github.com/hovermind/algos"
},
{
"name": "py-sheet",
"createdAt": "2018-05-31T05:00:41Z",
"updatedAt": "2020-03-19T06:55:33Z",
"url": "https://github.com/hovermind/py-sheet"
},
{
"name": "js-viz",
"createdAt": "2018-06-18T07:56:05Z",
"updatedAt": "2018-11-26T06:31:06Z",
"url": "https://github.com/hovermind/js-viz"
},
{
"name": "bootstrap-snippets",
"createdAt": "2018-08-09T08:23:19Z",
"updatedAt": "2020-08-13T13:22:43Z",
"url": "https://github.com/hovermind/bootstrap-snippets"
},
{
"name": "wpf-mvvm",
"createdAt": "2018-09-05T11:43:05Z",
"updatedAt": "2018-10-03T04:02:21Z",
"url": "https://github.com/hovermind/wpf-mvvm"
},
{
"name": "XF",
"createdAt": "2018-11-01T09:45:16Z",
"updatedAt": "2019-08-18T15:46:47Z",
"url": "https://github.com/hovermind/XF"
},
{
"name": "structured-logging",
"createdAt": "2018-11-12T06:31:49Z",
"updatedAt": "2018-11-12T07:54:42Z",
"url": "https://github.com/hovermind/structured-logging"
},
{
"name": "XamarinComponents",
"createdAt": "2018-11-28T01:22:24Z",
"updatedAt": "2018-11-28T01:22:37Z",
"url": "https://github.com/hovermind/XamarinComponents"
},
{
"name": "ef-core",
"createdAt": "2019-01-04T15:57:16Z",
"updatedAt": "2019-04-19T04:35:21Z",
"url": "https://github.com/hovermind/ef-core"
},
{
"name": "serilogging",
"createdAt": "2019-01-13T16:39:19Z",
"updatedAt": "2019-01-14T10:06:34Z",
"url": "https://github.com/hovermind/serilogging"
},
{
"name": "Plugin.SegmentedControl",
"createdAt": "2019-04-12T15:15:01Z",
"updatedAt": "2019-04-12T15:15:03Z",
"url": "https://github.com/hovermind/Plugin.SegmentedControl"
},
{
"name": "asp-net-core",
"createdAt": "2019-04-18T06:51:36Z",
"updatedAt": "2019-08-24T07:13:30Z",
"url": "https://github.com/hovermind/asp-net-core"
},
{
"name": "regex-snippets",
"createdAt": "2019-05-24T02:55:32Z",
"updatedAt": "2019-09-12T00:46:21Z",
"url": "https://github.com/hovermind/regex-snippets"
},
{
"name": "immutable-queue",
"createdAt": "2019-08-18T06:06:25Z",
"updatedAt": "2019-09-07T09:02:34Z",
"url": "https://github.com/hovermind/immutable-queue"
},
{
"name": "wealth-park-api",
"createdAt": "2019-08-25T07:54:12Z",
"updatedAt": "2019-08-25T13:34:22Z",
"url": "https://github.com/hovermind/wealth-park-api"
},
{
"name": "git-cs",
"createdAt": "2019-11-22T01:25:26Z",
"updatedAt": "2020-11-03T12:16:37Z",
"url": "https://github.com/hovermind/git-cs"
},
{
"name": "ios-wiki",
"createdAt": "2019-12-03T06:22:26Z",
"updatedAt": "2020-04-10T06:56:50Z",
"url": "https://github.com/hovermind/ios-wiki"
},
{
"name": "awesome-ios",
"createdAt": "2019-12-12T06:55:22Z",
"updatedAt": "2019-12-12T06:55:24Z",
"url": "https://github.com/hovermind/awesome-ios"
},
{
"name": "OpusKit",
"createdAt": "2020-01-07T04:49:13Z",
"updatedAt": "2020-01-07T04:49:14Z",
"url": "https://github.com/hovermind/OpusKit"
},
{
"name": "awesome-machine-learning",
"createdAt": "2020-03-01T09:36:04Z",
"updatedAt": "2020-04-14T04:32:01Z",
"url": "https://github.com/hovermind/awesome-machine-learning"
},
{
"name": "problem-solving",
"createdAt": "2020-03-13T02:22:56Z",
"updatedAt": "2020-03-13T02:22:56Z",
"url": "https://github.com/hovermind/problem-solving"
},
{
"name": "run-aspnetcore",
"createdAt": "2020-04-24T05:32:16Z",
"updatedAt": "2020-04-24T05:32:17Z",
"url": "https://github.com/hovermind/run-aspnetcore"
},
{
"name": "SecuringAspNetCore3WithOAuth2AndOIDC",
"createdAt": "2020-06-18T03:05:09Z",
"updatedAt": "2020-06-18T03:05:11Z",
"url": "https://github.com/hovermind/SecuringAspNetCore3WithOAuth2AndOIDC"
},
{
"name": "aspnet-core-identity",
"createdAt": "2020-06-22T06:05:44Z",
"updatedAt": "2020-06-22T06:05:45Z",
"url": "https://github.com/hovermind/aspnet-core-identity"
},
{
"name": "AspNetCoreOIDC",
"createdAt": "2020-06-25T09:35:00Z",
"updatedAt": "2020-06-28T18:15:40Z",
"url": "https://github.com/hovermind/AspNetCoreOIDC"
},
{
"name": "AspNetCoreModelBinding",
"createdAt": "2020-06-26T01:51:04Z",
"updatedAt": "2020-06-27T07:00:08Z",
"url": "https://github.com/hovermind/AspNetCoreModelBinding"
},
{
"name": "employee-management-blazor",
"createdAt": "2020-07-24T08:38:34Z",
"updatedAt": "2020-07-24T08:38:37Z",
"url": "https://github.com/hovermind/employee-management-blazor"
},
{
"name": "blazor-demos",
"createdAt": "2020-08-04T15:22:26Z",
"updatedAt": "2020-09-01T15:03:25Z",
"url": "https://github.com/hovermind/blazor-demos"
}
]