Serialization

 Serialization:

    creating Json file.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using System.Security.Cryptography;
using System.Text.Json;
using System.Threading.Tasks;
using HackerRankPractice.Model;

namespace HackerRankPractice.FilePractice
{
    public static class SerializeJson
    {
        public static void deserializing(){
            var person = new Person { Name = "Krishna", Age =30};
           
            string jsonData = JsonSerializer.Serialize(person);
            File.WriteAllText("Person.json", jsonData);
           
            var objData = File.ReadAllText("Person.json");
            var deserialing = JsonSerializer.Deserialize<Person>(objData);

            Console.WriteLine($"Name: {deserialing.Name}, Age: {deserialing.Age}");
        }
    }
}


Explanation:

  • Create a Person object with properties Name and Age.
  • Serialize the object to a JSON string.
  • Write the JSON string to a file (Person.json).
  • Read the JSON string back from the file.
  • Deserialize the JSON string into a new Person object.
  • Print the properties of the deserialized object.


Output:

Name: Krishna, Age: 30



Comments

Popular posts from this blog

Multiline to singleline IN C# - CODING

EF Core interview questions for beginners

EF Core interview questions for experienced