Search code examples
c#json.netjson.net

JSON writer doesn't include properties on classes derived from ObservableCollection


I have the following classes:

public class Goal : ObservableCollection<Activity>
{

  public string Name { get; set; }

  // constructors
  public Goal() { }
  public Goal(string name)
  {

    Name = name;

  }

}

public class Activity
{

  // properties
  public string Name { get; set; }
  public string Details { get; set; }
  public bool FilterMe { get; set; }

  // constructors
  public Activity() { }
  public Activity(string name)
  {

    Name = name;

  }

}

When I write this out as JSON, the Activity list with all public Activity properties is output correctly, but the Name property for the Goal is not included. What am I doing wrong?

// create a goal
Goal goal = new("Goal 1");
for (int a = 0; a < 5; a++)
{
  Activity activity = new($"Activity {a + 1}");
  if (a % 2 == 0) { activity.FilterMe = true; }
  goal.Add(activity);
}

// write the output file
using FileStream fsWrite = File.Create("C:\\Users\\me\\Desktop\\Test.json");
JsonSerializer.Serialize<Goal>(new Utf8JsonWriter(fsWrite, new JsonWriterOptions() { Indented = true }), goal, new JsonSerializerOptions() { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, IgnoreReadOnlyProperties = true });

Here is sample output:

{
  "Goal": [
    {
      "Name": "Activity 1",
      "Details": "",
      "FilterMe": true
    },
    {
      "Name": "Activity 2",
      "Details": "",
      "FilterMe": false
    },
    {
      "Name": "Activity 3",
      "Details": "",
      "FilterMe": true
    },
    {
      "Name": "Activity 4",
      "Details": "",
      "FilterMe": false
    },
    {
      "Name": "Activity 5",
      "Details": "",
      "FilterMe": true
    }
  ]
}

As you can see in the output above, the Name property for the Goal is missing. It's a public property, so I assumed that the Serializer would pick it up.


Solution

  • it will never be working the way you want, and it is not a serializer problem, it is the way C# creates objects. Even if you try to create json the way you want manually using a text editor, it will be an invalid json. You will not be able to use it. So the only thing I can recommed you , to change your class to this

    public class Goal 
    {
        public ObservableCollection<Activity> Goals {get; set;} = new();
    
      public string Name { get; set; }
       public void Add (Activity activity)
       {
        Goals.Add(activity);
       }
    
      // constructors
      public Goal() { }
      public Goal(string name)
      {
        Name = name;
      }
    }
    

    and all your code will be working without any problem

    PS.

    I would be extremely interested to see a json you want after serialization, please fix json you posted manually the way you want and show us.