Let us Learn Tuple-Part6 (Tuple Serialization and DeSerialization)

Rajnilari2015
Posted by in C# category on for Beginner level | Points: 250 | Views : 800 red flag
Rating: 5 out of 5  
 1 vote(s)

This is the sixth part of the series where we will learn about Tuple Serialization and DeSerialization.


 Download source code for Let us Learn Tuple-Part6 (Tuple Serialization and DeSerialization)

Recommendation
Read Let us Learn Tuple-Part5 ( LAMBDA with Tuple) before this article.

Introduction

This is the sixth part of the series Let us Learn Tuple. In this part we will learn about Tuple Serialization and DeSerialization.

You can read the entire series of article as listed down

Let us Learn Tuple-Part1

Let us Learn Tuple-Part2 ( Creation of Generic Collections and Accessing the same using Tuple )

Let us Learn Tuple-Part3 ( Tuple and Arrays )

Let us Learn Tuple-Part4 ( Create MultiKey dictionary using Tuple )

Let us Learn Tuple-Part5 ( LAMBDA with Tuple )

Let us Learn Tuple-Part6 (Tuple Serialization and DeSerialization)

Let us Learn Tuple-Part7 (Last but not the least part)

Using the code

Tuple objects can be serialize and deserialize. For the sake of demonstration, we will use Newtonsoft.Json which is a popular high-performance JSON framework for .NET. So let us go ahead and install the package from Package Manager Console by issuing the below command for Newtonsoft.Json

PM > Install-Package Newtonsoft.Json -Version 7.0.1

Let us first create a Student object and expose a property of type List<Tuple<string, int, List<string>>>. Next populate the data to the object by using the expression-bodied property of C#6.0 as demonstrated under

public class Student
{
    //Kindly note that the StudentTuple has been populated by using expression-bodied property
    public List<Tuple<string, int, List<string>>> StudentTuple => new List<Tuple<string, int, List<string>>>
    {
        Tuple.Create(
                        "Rajlakshmi Biswas"
                        , 4
                        , new List<string> { "Mathematics","English",
                                            "Science and Social Science",
                                                "Hindi"
                                            })
        ,
        Tuple.Create(
                        "Niladri Biswas"
                        , 36
                        , new List<string> { "Mathematics","English",
                                            "Computer Science",
                                                "History","Physics"
                                            })
        ,
        Tuple.Create(
                        "Arina Biswas"
                        , 26
                        , new List<string> { "Mathematics","English",
                                            "Accountancy",
                                                "Finance"
                                            })
        ,
        Tuple.Create(
                        "RNA Team"
                        , 4
                        , new List<string> { "Mathematics","English",
                                            "Accountancy",
                                                "Finance","Hindi",
                                                "History","Physics",
                                            "Science and Social Science",
                                            "Computer Science"
                                            })
    };
}

Next we will serialize the Student object by using the JsonConvert.SerializeObject of Newtonsoft.Json as demonstrated under

//Serialize the studentTuple object to JSON string
string serializedStudentTuple = JsonConvert.SerializeObject(new Student().StudentTuple, Formatting.Indented);
Console.WriteLine(serializedStudentTuple);

We will receive the below output

We can figure out that, the object has been serialized to the collection of string of {"Item1": Value(s),"Item2":Value(s),"Item3":Collection of Value(s)}

We can deserialize the serialized data by using JsonConvert.DeserializeObject of Newtonsoft.Json by type casting to the List<Student> object as demonstrated under

var deserializedStudentTuple = JsonConvert.DeserializeObject<List<Student>>(serializedStudentTuple);

//Iterate the Deserialize Student Object
deserializedStudentTuple
.Select(s => s.StudentTuple)
.Select((s, i) => s[i])
.ToList()
.ForEach
(
    //print the deserialize student object
    i =>                    
        {
            Console.WriteLine("Name :" + i.Item1);
            Console.WriteLine("Age :" + i.Item2);
            Console.WriteLine("Is Studying :" + string.Join(",", i.Item3));
            Console.WriteLine("-------------------------------------------------------------" + Environment.NewLine);
        }
);

The resultant output will be

Conclusion

In this article we learnt about Tuple Serialization and DeSerialization. Hope this will be helpful. More to come in the following series. Zipped file attached.

Recommendation
Read Let us Learn Tuple-Part7 (Last but not the least part) after this article.
Page copy protected against web site content infringement by Copyscape

About the Author

Rajnilari2015
Full Name: Niladri Biswas (RNA Team)
Member Level: Platinum
Member Status: Member,Microsoft_MVP,MVP
Member Since: 3/17/2015 2:41:06 AM
Country: India
-- Thanks & Regards, RNA Team


Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)