Tutorialspoint
Problem
Solution
Submissions

Read and Write Objects Using Basic Serialization

Certification: Intermediate Level Accuracy: 50% Submissions: 2 Points: 15

Write a C# program to implement a simple serialization system that can save objects to a text file and load them back. Create a serializer class that converts object properties to text format and reconstructs objects from that text.

Example 1
  • Input: Create a Product object with Id=101, Name="Laptop", Price=999.99, InStock=true, serialize to file, deserialize back
  • Output: The deserialized object has the same property values as the original
  • Explanation:
    • Step 1: Create a Product object with specified property values.
    • Step 2: Serialize the object to text format with property:value pairs.
    • Step 3: Save the serialized text to a file.
    • Step 4: Read the text from the file.
    • Step 5: Deserialize the text back into a Product object.
    • Step 6: Verify that the deserialized object has the same property values as the original.
Example 2
  • Input: Create a Person object with FirstName="John", LastName="Smith", Age=30, IsEmployed=true, serialize to text, display serialized text, deserialize back
  • Output: Serialized text shows property:value pairs, deserialized object has same values
  • Explanation:
    • Step 1: Create a Person object with specified property values.
    • Step 2: Serialize the Person object to text format showing each property and its value.
    • Step 3: Display the serialized text to confirm its format.
    • Step 4: Parse the text to recreate a Person object.
    • Step 5: Verify that the recreated object has the same property values as the original.
Constraints
  • Your solution should work with public properties only
  • Support serialization of string, numeric, boolean, and DateTime properties
  • Time Complexity: O(n) where n is the number of properties in the object
  • Space Complexity: O(n) for storing the serialized representation
Object-Oriented ProgrammingFile Handling FacebookTech Mahindra
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use Reflection (System.Reflection namespace) to inspect an object's properties
  • Define a consistent format for the serialized text (e.g., PropertyName:Value with each pair on a new line)
  • Use Type.GetProperties() to get all public properties of an object
  • For deserialization, use Activator.CreateInstance() to create a new instance of the generic type
  • Use appropriate conversion methods (e.g., Convert.ChangeType) when setting property values during deserialization

Steps to solve by this approach:

 Step 1: Define the serialization format (property:value pairs separated by newlines)
 Step 2: Use reflection to iterate through object properties for serialization
 Step 3: Create a string builder to construct the serialized text
 Step 4: For deserialization, parse the text and extract property name-value pairs
 Step 5: Use reflection to set property values on a new object instance

Submitted Code :