14 JSON serialization / deserialization and custom model binding
This chapter covers
- Serializing and deserializing JSON data
- Using the [FromBody] argument attribute to magically deserialize JSON data
- Implementing a custom model binder using the IModelBinder interface
- Generating an OpenAPI specification on-the-fly at runtime
This is it. The last refactoring chapter. Throughout this book, you refactored an existing code base from the ground up. We learned about test-driven development, how to write clean code, and tips and tricks for C#.
Figure 14.1 In the previous chapters we implemented the database access, repository, service layers, and FlightController class. In this chapter, we finish the job and implement the BookingController class.

In this chapter, we implement the last controller: BookingController (section 14.1). After that, we do some manual testing and acceptance testing against the OpenAPI specification from FlyTomorrow. We also set up Swagger middleware to generate an OpenAPI specification on-the-fly (section 14.2). This is optional, but a very useful technique to know as Swagger helps us with our acceptance testing.