1

I am dealing with the following (simplified) test report obtained from NUnit and I am trying to deserialize it

{
    "test-suite": {
        "@type": "TestSuite",
        "test-suite": [
            {
                "@type": "Assembly",
                "test-suite": {
                    "@type": "TestSuite",
                    "test-suite": [
                        {
                            "@type": "TestSuite",
                            "test-suite": {
                                "@type": "TestFixture",
                                "test-case": [
                                    {
                                        "@id": "1024"
                                    },
                                    {
                                        "@id": "1025"
                                    }
                                ]
                            }
                        },
                        {
                            "@type": "TestSuite",
                            "test-suite": {
                                "@type": "TestFixture",
                                "test-suite": {
                                    "@type": "ParameterizedMethod",
                                    "test-case": [
                                        {
                                            "@id": "1018"
                                        }
                                    ]
                                }
                            }
                        }
                    ]
                }
            }
        ]
    }
}

The problem is, if you notice, that the test-suite key is used to contain both the following types: either a test-suite, or an array of test-suite(s).

There's no logic to it as it depends of the assemblies and the organization of the tests within them.

How do I write the receiving json objects that can handle all the possible cases ?

Milan
  • 1,547
  • 1
  • 24
  • 47
  • Why would you do a parser? – Leandro Bardelli Mar 21 '23 at 17:10
  • Edited the original post – Milan Mar 21 '23 at 17:24
  • As a last resort use `System.Text.Json` instead – Pieterjan Mar 21 '23 at 17:29
  • 1
    Assuming you are deserializing to some c# model, declare the c# property corresponding to `test-suite` as a `List` and use one of the converters from [How to handle both a single item and an array for the same property using JSON.net](https://stackoverflow.com/q/18994685/3744182). – dbc Mar 21 '23 at 17:30
  • If you are not deserializing to a c# model but instead just parsing to a `JToken` hierarchy, then can you clarify where you are stuck? You should just be able to test whether the value of `"test-suite"` is a `JArray` or `JObject` (or null, if not present). – dbc Mar 21 '23 at 17:31
  • 1
    The JsonConverter did the trick, it's perfect, thanks ! – Milan Mar 21 '23 at 17:36

0 Answers0