Home > Blockchain >  Deserialize JSON for YARP config
Deserialize JSON for YARP config

Time:10-22

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:

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?

CodePudding user response:

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; }
}
  • Related