I am attempting to write the contents of an object to the console using Console.WriteLine(JsonSerializer.Serialize(object)); but it only writes a {} - which I assume is an empty JSON file?
Any help would be greatly appreciated.
namespace PetShopPart1
{
public class Program
{
public static void Main(string[] args)
{
//DogLeash dogLeash = new DogLeash();
//dogLeash.Quantity = 0;
//dogLeash.Material = "0";
//dogLeash.LengthInches = 0;
DogLeash dogLeash = new DogLeash
{
Quantity = 1,
Material = "0",
LengthInches = 0,
};
string userInput = "0";
while (userInput != "exit")
{
if (userInput == "1")
{
Console.WriteLine("How many leashes would you like?\n");
dogLeash.Quantity = int.Parse(Console.ReadLine());
Console.WriteLine();
Console.WriteLine("What Material would you like your leash(es) to be made of.\n");
dogLeash.Material = Console.ReadLine();
Console.WriteLine();
Console.WriteLine("And how many inches would you like your leash to be?\n");
dogLeash.LengthInches = int.Parse(Console.ReadLine());
Console.WriteLine();
Console.WriteLine(JsonSerializer.Serialize(dogLeash));
}
Console.WriteLine("Press 1 to add an item.\n");
Console.WriteLine("Type Exit to Quit.\n");
userInput = Console.ReadLine();
}
}
}
}
Dogleash.cs:
using System;
namespace PetShopPart1
{
class DogLeash : Product
{
public int LengthInches;
public string Material;
}
}
Product.cs:
using System;
using System.ComponentModel;
namespace PetShopPart1
{
public class Product
{
public string Name;
public decimal Price;
public int Quantity = 0;
public string Description;
}
}

Dogleash, which makes it really hard to help. We don't need any user input or anything - just provide a minimal but complete program. (Just instantiate a Dogleash with hard-coded values and serialize it - but do show us all the code.)Productclass, it only has fields in it, where by default the serializer will only serialize properties. I assume this is the same forDogleash.