KIRA BELMONT
KIRA BELMONT
CC#
Created by KIRA BELMONT on 3/27/2025 in #help
Json Serializing And Deserializing Practice Errors
I am practicing Json Serializing rightnow, but I got stuck on this for the last day. I always get this error, and I just want 1 thing, save the ingredients, and load it.
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Cookie_Cookbook.Recipe.Ingredient
{
public static class IngredientManagement
{
public static void ShowCurrentRecipes(List<Ingredient> list)
{
if (list.Count == 0)
{
Console.WriteLine("There is no recipes added.");
}
else
{
Console.WriteLine("Recipe Added: ");
foreach (Ingredient recipe in list)
{
Console.WriteLine($"{recipe.Name}:{recipe.Instructions}");
}
}
}
public static void ShowIngredients()
{
Console.WriteLine("1. Wheat flour\n2. Coconut flour\n3. Butter\n4. Chocolate\n5. Sugar\n6. Cardamom\n7. Cinnamon\n8. Cocoa powder");
}
private static readonly string filePath = "recipe.json";
public static void Save(List<Ingredient> list)
{
string toJson = JsonSerializer.Serialize(list);
File.WriteAllText(filePath, toJson);
}
public static List<Ingredient> Load()
{
if (File.Exists(filePath))
{
string jsonData = File.ReadAllText(filePath);
return JsonSerializer.Deserialize<List<Ingredient>>(jsonData) ?? new List<Ingredient>();
}
return new List<Ingredient>();
}


}
}

System.NotSupportedException: 'Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with 'JsonConstructorAttribute' is not supported. Type 'Cookie_Cookbook.Recipe.Ingredient.Ingredient'. Path: $[0] | LineNumber: 0 | BytePositionInLine: 2.'
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Cookie_Cookbook.Recipe.Ingredient
{
public static class IngredientManagement
{
public static void ShowCurrentRecipes(List<Ingredient> list)
{
if (list.Count == 0)
{
Console.WriteLine("There is no recipes added.");
}
else
{
Console.WriteLine("Recipe Added: ");
foreach (Ingredient recipe in list)
{
Console.WriteLine($"{recipe.Name}:{recipe.Instructions}");
}
}
}
public static void ShowIngredients()
{
Console.WriteLine("1. Wheat flour\n2. Coconut flour\n3. Butter\n4. Chocolate\n5. Sugar\n6. Cardamom\n7. Cinnamon\n8. Cocoa powder");
}
private static readonly string filePath = "recipe.json";
public static void Save(List<Ingredient> list)
{
string toJson = JsonSerializer.Serialize(list);
File.WriteAllText(filePath, toJson);
}
public static List<Ingredient> Load()
{
if (File.Exists(filePath))
{
string jsonData = File.ReadAllText(filePath);
return JsonSerializer.Deserialize<List<Ingredient>>(jsonData) ?? new List<Ingredient>();
}
return new List<Ingredient>();
}


}
}

System.NotSupportedException: 'Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with 'JsonConstructorAttribute' is not supported. Type 'Cookie_Cookbook.Recipe.Ingredient.Ingredient'. Path: $[0] | LineNumber: 0 | BytePositionInLine: 2.'
60 replies