GraphQLHttpClient nested json string c#

27 views Asked by At

I am recieving this response from a GraphQL endpoint:

"container": {
                            "id": "X",
                            "containerId": "XYZ",
                            "metaData":"{\"postnummer\": \"9000 Aalborg\", \"ejendomsnr\": \"12345\", \"kundenr\": \"12345\", \"address\": \"\Ågade 5 C, 9000 Aalborg\", \"vej\": \"\Ågade\", \"nr\": \"5 C\"}",
                            "wasteFraction": {
                                "wasteCategory": "Combustion",
                                "wasteSubstance": "Gaseous",
                                "wasteTypes": {
                                    "edges": [
                                        {
                                            "node": {
                                                "name": "Residual waste"
                                            }
                                        }
                                    ]
                                }
                            }
                        }

My problem is with the metaData attribute, which itself is a json string, which the GraphQLHttpClient/NewtonsoftJsonSerializer does not seem to handle.

If I define the property as an object with the correct properties, serialization fails. If I define metaData like a string, I then have to manually deserialize it afterwards.

This works, but then I have to manually desialize the string for each result:

public class Container
{
    public string id { get; set; }
    public string containerId { get; set; }
    public string metaData { get; set; }
    public Wastefraction wasteFraction { get; set; }
}

public class MetaData
{
    public string postnummer { get; set; }

    public string husnr { get; set; }
    public string postdistrikt { get; set; }

    public string ejendomsnr { get; set; }
    public string kundenr { get; set; }
    public string address { get; set; }
    public string vej { get; set; }
    public string nr { get; set; }
}

This does NOT work:

public class Container
{
    public string id { get; set; }
    public string containerId { get; set; }
    public MetaData metaData { get; set; }
    public Wastefraction wasteFraction { get; set; }
}

public class MetaData
{
    public string postnummer { get; set; }

    public string husnr { get; set; }
    public string postdistrikt { get; set; }

    public string ejendomsnr { get; set; }
    public string kundenr { get; set; }
    public string address { get; set; }
    public string vej { get; set; }
    public string nr { get; set; }
}

Is it possible to get the GraphQLHttpClient/NewtonsoftJsonSerializer to handle this nested jsonstring automaticly?

0

There are 0 answers