zz
JSON Serialization/Deserialization using C#.NET
Many a times, we have a need to call third party API or web services and these apis and web services return us data in xml format or JSON format.
The .NET Framework doesn’t give you the tools to do it out-of-the-box.
We can implement JSON Serialization/Deserialization in the three ways:
- Using JavaScriptSerializer class
- Using DataContractJsonSerializer class
- Using JSON.NET library
This demo would highlight how can we deserialize these objects using
JSON.NET package in nuget package manager.
Json.NET is a package which helps conversion between JSON text and .NET object using the JsonSerializer. It converts .NET objects into their JSON equivalent text and back again by mapping the .NET object property names to the JSON property names.
We will install this package in our project.
Go to Tools Menu -> Choose Library Package Manager -> Package Manager Console. It opens a command window to put the following command.
Install-Package Newtonsoft.Json
In our project we are calling fitbit api and getting heart rate data as httpwebresponse in JSON format. Screenshots below.




More information on Newtonsoft Json – https://www.newtonsoft.com/json
C# – why Dictionary is ahead of lists(look ups)
A quick example to demonstrate why the difference between dictionary “look up” versus lists.
The list of list of students and grades in memory, they have StudentId in common.
In a first way I tried to find Grade of a student using LINQ on a list that takes near 7 seconds on my machine and in another way first I converted List into a dictionary then finding grades of the student from the dictionary using a key.
internal class Program
{
private static void Main(string[] args)
{
var stopwatch = new Stopwatch();
List<Grade> grades = Grade.GetData().ToList();
List<Student> students = Student.GetStudents().ToList();
stopwatch.Start();
foreach (Student student in students)
{
student.Grade = grades.Single(x => x.StudentId == student.Id).Value;
}
stopwatch.Stop();
Console.WriteLine("Using list {0}", stopwatch.Elapsed);
stopwatch.Reset();
students = Student.GetStudents().ToList();
stopwatch.Start();
Dictionary<Guid, string> dic = Grade.GetData().ToDictionary(x => x.StudentId, x => x.Value);
foreach (Student student in students)
{
student.Grade = dic[student.Id];
}
stopwatch.Stop();
Console.WriteLine("Using dictionary {0}", stopwatch.Elapsed);
Console.ReadKey();
}
}
public class GuidHelper
{
public static List<Guid> ListOfIds=new List<Guid>();
static GuidHelper()
{
for (int i = 0; i < 10000; i++)
{
ListOfIds.Add(Guid.NewGuid());
}
}
}
public class Grade
{
public Guid StudentId { get; set; }
public string Value { get; set; }
public static IEnumerable<Grade> GetData()
{
for (int i = 0; i < 10000; i++)
{
yield return new Grade
{
StudentId = GuidHelper.ListOfIds[i], Value = "Value " + i
};
}
}
}
public class Student
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Grade { get; set; }
public static IEnumerable<Student> GetStudents()
{
for (int i = 0; i < 10000; i++)
{
yield return new Student
{
Id = GuidHelper.ListOfIds[i],
Name = "Name " + i
};
}
}
}
The Magic Console-

Make your singleton class thread safe
In this post, we try to get an idea about making thread-safe singleton.
public class Singleton { private static readonly Object s_lock = new Object(); private static Singleton singleton = null; private Singleton() { } public static Singleton GetSingleton() { //check 1 if (singleton != null) return singleton; Monitor.Enter(s_lock); //check 2 if (singleton == null) { Singleton temp = new Singleton(); } Monitor.Exit(s_lock); return singleton; } }
z
Back
z
Dependency Injection – Ninject vs Unity
This is your very first post. Click the Edit link to modify or delete it, or start a new post. If you like, use this post to tell readers why you started this blog and what you plan to do with it.