1

I have a swagger doc json file and I would like to convert it to an Dictionary<string,dynamic> with key being the property name and value can be another dictionnary of type Dictionary<string,dynamic> until the value is a primitive, not a json object.

I started with var dic = JSonObject.Parse("file.json").Properties().ToDictionary(p=>p.Name, p => p.Value);

How to recursively test and convert p.Value to dictionnary when the value is not a primitive type?

Bellash
  • 7,560
  • 6
  • 53
  • 86

2 Answers2

0

I just found this package Microsoft.OpenApi.Readers which already has everything I wanted to parse swagger document and used it like this

       string url = "https://localhost:10155/swagger/v1/swagger.json";
        var httpClient = new HttpClient();
        var stream = await httpClient.GetStreamAsync(url);
        var openApiDocument = new OpenApiStreamReader().Read(stream, out var diagnostic);
        return View(openApiDocument);

Then I could get all the OpenApiDocument object in the tree

Bellash
  • 7,560
  • 6
  • 53
  • 86
0

try this, it is more robust than async concept...

private void GetServiceJSONDocument()
{
    Microsoft.OpenApi.Models.OpenApiDocument JsonDoc = new Microsoft.OpenApi.Models.OpenApiDocument();
    HttpWebResponse wresponse = GetServiceAPIRequest(false, SiteAPIURL).GetResponse() as HttpWebResponse;
    JsonDoc = new OpenApiStreamReader().Read(wresponse.GetResponseStream(), out var diagnostic); 
}

private static HttpWebRequest GetServiceAPIRequest(bool isPOST, string atvURL)
{
    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(atvURL);
    webRequest.ServicePoint.Expect100Continue = false;
    webRequest.ContentType = atvURL.ToLower().IndexOf(".json") >= 0  ? "application/json;charset=\"utf-8\"" : "text/xml;charset=\"utf-8\"";
    webRequest.Accept = "text/xml";
    webRequest.Headers.Add("SOAPAction", ActiveWSDLRequest);
    webRequest.Method = !isPOST ? "GET" : "POST";
    return webRequest;
}