Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Deserialize JSON for YARP config

The following JSON is my request body:

{
    "Routes": {
        "route1": {
            "ClusterId": "cluster1",
            "Match": {
                "Path": "{**catch-all}",
                "Hosts": ["www.aaaaa.com", "www.bbbbb.com"]
            }
        }
    },
    "Clusters": {
        "cluster1": {
            "Destinations": {
                "cluster1/destination1": {
                    "Address": "https://example.com/"
                }
            }
        }
    }
}

I am trying to cast it as objects but it is not working

var reqItems1 = JsonConvert.DeserializeObject<Req>(jsonData);

Where Req is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public class Req
{
    public RouteConfig Routes { get; set; }
    public ClusterConfig Clusters { get; set; }
}

RouteConfig and ClusterConfig are defined:

https://microsoft.github.io/reverse-proxy/api/Yarp.ReverseProxy.Configuration.RouteConfig.html

https://microsoft.github.io/reverse-proxy/api/Yarp.ReverseProxy.Configuration.ClusterConfig.html

Everything is null. How can I parse the request directly to objects?

>Solution :

From the attached JSON, the Routes and Clusters properties are key-value pairs.

Hence your Req class should be as below:

public class Req
{
    public Dictionary<string, RouteConfig> Routes { get; set; }
    public Dictionary<string, ClusterConfig> Clusters { get; set; }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading