DeleteJson - lucyberryhub/WPF.Tutorial GitHub Wiki
β¨ Welcome, cuties! Today, we will learn how to remove a cherry (data item) from our CherryJsonFile.json
using a super kawaii method! πΈπ
Youβll also meet IHasCherryId, our magical interface that makes sure every cherry (data item) has an Id
, Flavor
, and SweetnessLevel
! π¬β¨
Ready? Grab your strawberry milk ππ₯ and letβs goooooo! ππ
Before we start deleting cherries, we need to create an interface to make sure every cherry has an Id, Flavor, and SweetnessLevel! π
public interface IHasCherryId
{
/// π A unique cherry ID! Every cherry must have one! π
long CherryId { get; set; }
/// π The flavor of the cherry! (Example: "Strawberry", "Blueberry") π
string Flavor { get; set; }
/// π¬ How sweet is the cherry? ("Very Sweet", "Super Sweet", "Ultra Sweet") π
string SweetnessLevel { get; set; }
}
β¨ Now, every cherry must have an Id, Flavor, and SweetnessLevel! No boring cherries allowed! ππ
Now letβs make a super sweet method to remove a cherry from our CherryJsonFile.json
! ππ
private void RemoveCherryFromJson<T>(long cherryId, string cherryJsonFilePath) where T : class, IHasCherryId
{
if (string.IsNullOrWhiteSpace(cherryJsonFilePath) || !File.Exists(cherryJsonFilePath))
return; // π If the file is missing, no cherries to remove! π
try
{
List<T> cherryBasket; // π A cute list of cherries! π
// π Read the cherry list from the JSON file πβ¨
using (var reader = new StreamReader(cherryJsonFilePath))
{
string jsonData = reader.ReadToEnd();
if (string.IsNullOrWhiteSpace(jsonData))
return; // π¬ If the file is empty, no cherries to remove! π
cherryBasket = JsonConvert.DeserializeObject<List<T>>(jsonData) ?? new List<T>();
}
// π Find the cherry we want to remove! π
var cherryToRemove = cherryBasket.FirstOrDefault(x => x.CherryId == cherryId);
if (cherryToRemove != null)
{
cherryBasket.Remove(cherryToRemove); // π Say bye-bye to the cherry! πβ¨
// π Write the new cherry list back to JSON! π
using (var writer = new StreamWriter(cherryJsonFilePath, false, Encoding.UTF8))
{
writer.Write(JsonConvert.SerializeObject(cherryBasket, Formatting.Indented));
}
}
}
catch (JsonException ex)
{
throw new InvalidOperationException($"π Oops! Error reading the cherry file: {cherryJsonFilePath}", ex);
}
catch (IOException ex)
{
throw new InvalidOperationException($"π Oh no! Couldn't access the cherry file: {cherryJsonFilePath}", ex);
}
}
if (string.IsNullOrWhiteSpace(cherryJsonFilePath) || !File.Exists(cherryJsonFilePath))
return;
β¨ If the file is missing, donβt do anything! No cherries, no problem! π
using (var reader = new StreamReader(cherryJsonFilePath))
{
string jsonData = reader.ReadToEnd();
if (string.IsNullOrWhiteSpace(jsonData))
return;
cherryBasket = JsonConvert.DeserializeObject<List<T>>(jsonData) ?? new List<T>();
}
β¨ We open the cherry basket (JSON file) and load all the cherries inside! π¬
β¨ If the basket is empty, we just stop right there! π
var cherryToRemove = cherryBasket.FirstOrDefault(x => x.CherryId == cherryId);
if (cherryToRemove != null)
{
cherryBasket.Remove(cherryToRemove);
β¨ We search for the cherry by CherryId
and remove it from the list! π
using (var writer = new StreamWriter(cherryJsonFilePath, false, Encoding.UTF8))
{
writer.Write(JsonConvert.SerializeObject(cherryBasket, Formatting.Indented));
}
β¨ We write the updated cherry list back to the JSON file! So cute! ππ
Now, letβs create a cutie cherry class that follows the IHasCherryId
rules! π
public class Cherry : IHasCherryId
{
public long CherryId { get; set; }
public string Flavor { get; set; }
public string SweetnessLevel { get; set; }
}
And now, letβs remove a cherry from the JSON file! πβ¨
RemoveCherryFromJson<Cherry>(777, "CherryBasket.json");
β¨ This will remove the cherry with CherryId = 777
from CherryBasket.json! π
π You did it, cuties! π Youβve learned how to:
β
Use an interface (IHasCherryId
) to make sure every cherry has an ID! π
β
Read, parse, and remove data from JSON files without breaking the cuteness! π
β
Keep your code structured, safe, and super kawaii! πΈβ¨
π Want to log every cherry removal? Add a cute logging function! πβ¨
π Need faster cherry processing? Use memory-mapped files for ultra speed! π
π Thinking about saving cherries in a database? Try SQLite for super organization! π
π Happy coding, cutie developers! ππ Lucy Berry loves you! ππ