-1

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;
    }
}

Console Output

4
  • 1
    You haven't shown us 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.) Commented Oct 24, 2023 at 16:59
  • 1
    Looking at your Product class, it only has fields in it, where by default the serializer will only serialize properties. I assume this is the same for Dogleash. Commented Oct 24, 2023 at 17:42
  • This is still far from minimal, although at least it's complete. (Please pay attention to formatting when posting though - use the preview, and don't post it until it's in a form that you'd be happy to read it in.) But fundamentally, Matthew's right: you don't have any properties. Commented Oct 24, 2023 at 17:43
  • Thanks so much for your help guys! I apologize for the poor post, I just started a bootcamp and am very new to all of this. I'll be more attentive in the future. Thanks again for the instruction! - Aaron Sierp Commented Oct 24, 2023 at 17:59

1 Answer 1

2

In case it's not clear from the comments on your question,

public int LengthInches;

is a field

public int LengthInches {get; set;}

is a property - and JsonSerializer only serializes properties by default.

Sign up to request clarification or add additional context in comments.

2 Comments

This "JsonSerializer only serializes properties" is a bit misleading. You can set IncludeFields in the JsonSerializerOptions - the above comments are pointing this out correctly by refering to "by default".
@Postlagerkarte Reasonable callout. I've updated my answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.