Conversion and Reconversion of JSON and XML in RestAssured.net
The Concept
Conversion or serialization is a process of creating JSON and XML visualization of an object in C#.
RestAssured.net functionality supports both JSON and XML. Here are some small examples of these both options.
So first, we need to take a C# object which is a typical blog post:
Then, to develop a JSON or XML representation of the copy for the given class, you can use different approaches. For example, you can make a JSON or XML representation manually. But there may be unexpected defects and a lot of concomitant technical issues.
Also, you can use some libraries like json.net / system.xml to put all the hard work into them.
RestAssured.net does all this for the user by default.
If you want to formalize JSON submission of an object (post in blog), you need to paste a copy of it to the Body() method:
If you start logging the request classically, you can see that the POST copy will be converted to JSON:
The process is completed successfully. By the way, this method will work correctly with anonymous objects as well:
When performing software testing services (e.g. stress testing), you can convert the object to HML instead of JSON, you will need to use the Content type request header as application/XML:
Eventually, the XML content used will be passed to the request completion:
Inverse conversion parameters
If necessary, the entire contents of JSON/XML responses can be transformed into types C# objects – such actions are called inverse conversion. In RestaAssured.net, this is performed based on the As() method:
This method is also suitable for JSON/XML. RestAssured.net can examine the value of the header in the ContentType response and try to convert the response to exactly match this value – if the value of the header being searched for is not found, then it will be JSON by default.