3.3 Advance Concepts
1. Serialize / Deserialize Objects
In an ASP.NET REST API, when sending a student object to the client, the server doesn’t automatically include the class type (metadata) in the serialized JSON. However, if you want the client to deserialize the object with the correct class, there are several approaches to consider:
1.1 Basic JSON Response (Client Knows the Class)
Normally, the client already knows the class structure for deserializing the object. Here’s how it works:
Example: Student Class
public class Student{ public int Id { get; set; } public string Name { get; set; } public int Age { get; set; }}
API Controller Sending the Student Object:
[HttpGet]public IActionResult GetStudent(){ var student = new Student { Id = 1, Name = "John Doe", Age = 20 };
return Ok(student); // Serializes the Student object to JSON}
JSON Response:
{ "Id": 1, "Name": "John Doe", "Age": 20}
In this case, the client already knows the structure of the Student
class and can easily deserialize it using a library like Newtonsoft.Json or the built-in System.Text.Json.
1.2 Include Type Metadata with JSON (Polymorphic Scenarios)
If you need to send type information (e.g., in polymorphic scenarios where multiple object types could be sent), you can include the type metadata within the JSON using type discriminator or custom JSON converters.
Example using JSON.NET (Newtonsoft.Json):
Here’s how you can add the type metadata so the client knows how to deserialize it properly.
- Add
TypeNameHandling
in Serialization:
public IActionResult GetStudent(){ var student = new Student { Id = 1, Name = "John Doe", Age = 20 };
var settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All // Includes type metadata in JSON };
var json = JsonConvert.SerializeObject(student, settings); return Content(json, "application/json");}
JSON Response with Type Metadata:
{ "$type": "Namespace.Student, YourAssemblyName", "Id": 1, "Name": "John Doe", "Age": 20}
Here, the $type
property contains the fully qualified name of the class (including the namespace), so the client can use this information for deserialization.
On the Client Side:
- Using Newtonsoft.Json or another compatible library, the client can deserialize the object using the type information.
var student = JsonConvert.DeserializeObject<Student>(jsonString, new JsonSerializerSettings{ TypeNameHandling = TypeNameHandling.All});
1.3 Manual Type Information (Custom Key in JSON)
You can also add a custom property manually to indicate the object type:
API Response:
[HttpGet]public IActionResult GetStudent(){ var student = new Student { Id = 1, Name = "John Doe", Age = 20 };
var response = new { Type = "Student", Data = student };
return Ok(response);}
JSON Response:
{ "Type": "Student", "Data": { "Id": 1, "Name": "John Doe", "Age": 20 }}
Here, the Type
field provides a hint for the client on what type of object to deserialize.
On the Client Side:
The client would inspect the Type
field and determine how to deserialize the Data
object based on its value.
4. Using System.Text.Json
Polymorphic Deserialization (ASP.NET Core)
If you are using ASP.NET Core with System.Text.Json
(the default JSON serializer), polymorphic serialization/deserialization is more limited but possible. You would need a custom converter.