04 - JSON
JSON - JavaScript Object Notation
DataTypes:
- String
- Number
- Object – {….}
- Array (one dimensional only. Jagged is ok) – [….]
- True
- False
- Null
Example of JSON
{
"FirstName": "Andres",
"LastName": "Käver",
"Subjects": ["C#", "Android"]
}
Serialization & Deserialization
Serialization - Converting objects to bytestream
Deserialization - Converting bytestream to objects
JSON in .NET since .NET Core 3
New serialization library
!!! warning Does not support multidimensional arrays
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
class WeatherForecast {
public DateTime Date { get; set; }
public double TempC { get; set; }
public string Summary { get; set; }
}
Serialization
var jsonOptions = new JsonSerializerOptions()
{
WriteIndented = true,
AllowTrailingCommas = true,
};
var w = new WeatherForecast()
{
Date = DateTime.Now,
TempC = 31.45,
Summary = "Very hot!"
};
Console.WriteLine(JsonSerializer.Serialize(w, jsonOptions));
{
"Date": "2020-10-08T23:06:54.197136+03:00",
"TempC": 31.45,
"Summary": "Very hot!"
}
Deserialization
var jsonOptions = new JsonSerializerOptions()
{
WriteIndented = true,
AllowTrailingCommas = true,
};
var jsonStr = @"
{
""Date"": ""2020-10-08T23:06:54.197136+03:00"",
""TempC"": 31.45,
""Summary"": ""Very hot!""
}";
var ww = JsonSerializer.Deserialize<WeatherForecast>(jsonStr, jsonOptions);
Console.WriteLine(ww.Date);
Console.WriteLine(ww.TempC);
Console.WriteLine(ww.Summary);
Newtonsoft library
If you need/want to use json with multidimensional arrays – Newtonsoft library supports it.
Newtonsoft is very mature, slightly slower 3rd party library.
Newtonsoft is not allowed in this course (or next). Stick to the standards!
Circular reference serialization is also forbidden!
Directory and File IO
System.IO namespace.
Directory
Use the Directory class for typical operations such as copying, moving, renaming, creating, and deleting directories.
- To create a directory, use one of the
CreateDirectorymethods. - To delete a directory, use one of the
Deletemethods. - To get or set the current directory for an app, use the
GetCurrentDirectoryorSetCurrentDirectorymethod. - To manipulate DateTime information related to the creation, access, and writing of a directory, use methods such as
SetLastAccessTimeandSetCreationTime.
User's Home directory
string homeDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
Console.WriteLine($"User's Home Directory: {homeDirectory}");
File
Use the File class for typical operations such as copying, moving, renaming, creating, opening, deleting, and appending to a single file at a time. You can also use the File class to get and set file attributes or DateTime information related to the creation, access, and writing of a file.
System.IO.File namespace
AppendText
CreateText
using (StreamWriter writer = System.IO.File.AppendText("logfile.txt")) {
writer.WriteLine("log message");
}
Path
A path is a string that provides the location of a file or directory. A path does not necessarily point to a location on disk; for example, a path might map to a location in memory or on a device.
- ChangeExtension(String, String) - Changes the extension of a path string.
- Combine(String[]) - Combines an array of strings into a path.
- Exists(String) - Determines whether the specified file or directory exists.
- GetTempPath() - Returns the path of the current user's temporary folder.
- DirectorySeparatorChar - Provides a platform-specific character used to separate directory levels in a path string that reflects a hierarchical file system organization.