How to do it...

  1. Start off by creating the FundamentalProgramming and Student classes we used before for  XmlSerializer. This time, remove all the attributes to produce the following code:
        public class FundamentalProgramming
{
public string Lecturer;
public double ClassAverage;
public string RoomNumber;
public List<Student> Students;
}

public class Student
{
public string StudentName;
public double SubjectMark;
}
  1. In the calling code, set up the Student object, as previously and add them to  List<Student>:
        string serializationPath = @"C:	empclassInfo.txt";
Student studentA = new Student()
{
StudentName = "John Smith"
, SubjectMark = 86.4
};
Student studentB = new Student()
{
StudentName = "Jane Smith"
, SubjectMark = 67.3
};
List<Student> students = new List<Student>();
students.Add(studentA);
students.Add(studentB);
  1. Create the subject object of type FundamentalProgramming and assign the values to the fields:
        FundamentalProgramming subject = new FundamentalProgramming();
subject.Lecturer = "Prof. Johan van Niekerk";
subject.RoomNumber = "Lecture Auditorium A121";
subject.Students = students;
subject.ClassAverage = (students.Sum(mark => mark.SubjectMark) /
students.Count());
WriteLine($"Calculated class average = {subject.ClassAverage}");
  1. Add a JsonSerializer object to your code and set the formatting to indented. Using a JsonWriter, serialize the subject to the serializationPath to the file classInfo.txt:
        JsonSerializer json = new JsonSerializer();
json.Formatting = Formatting.Indented;
using (StreamWriter sw = new StreamWriter(serializationPath))
{
using (JsonWriter wr = new JsonTextWriter(sw))
{
json.Serialize(wr, subject);
}
}
WriteLine("Serialized to file using JSON Serializer");
  1. The next section of code will read the text from the file classInfo.txt created previously and create a JObject called jobj that uses the Newtonsoft.Json.Linq namespace to query JSON objects. Use JObject to parse the string returned from the file. This is where the power of using the Newtonsoft.Json.Linq namespace becomes evident. I can query the jobj object using LINQ to return the student marks and calculate an average:
        using (StreamReader sr = new StreamReader(serializationPath))
{
string jsonString = sr.ReadToEnd();
WriteLine("JSON String Read from file");
JObject jobj = JObject.Parse(jsonString);
IList<double> subjectMarks = jobj["Students"].Select(
m => (double)m["SubjectMark"]).ToList();
var ave = subjectMarks.Sum() / subjectMarks.Count();
WriteLine($"Calculated class average using JObject = {ave}");
}
  1. If you need to deserialize the JSON object, the deserializer logic is quite easy to implement. We use a JsonReader to get the text from the file and deserialize it:
        using (StreamReader sr = new StreamReader(serializationPath))
{
using (JsonReader jr = new JsonTextReader(sr))
{
FundamentalProgramming funProg = json.Deserialize
<FundamentalProgramming>(jr);
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.223.10