Serializing a changing C# object to JSON

Viewed 115

I am trying to serialize Item object but the ActionItem object would be always changing.

C# Object

public class Item
{
    public string Name { get; set; }
    public ActionItem Action { get; set; }
}

public class ActionItem
{
    public string Value { get; set; }
}

When I serialize the object above I will be getting:

{
   Name: null,
   Action: {
      Value: null
   }
}

But I am expecting it as { Name: null , Action: {} }

I would like to refactor the ActionItem object so that it can take other format of json/schema.
E.g.

{
    Name : 'MyName',
    Action : 
         {
           ActName: 'Finish',
           ActType: 1
           //...might differ because this field is always changing
         }
}

I tried serializing, using JsonConvert.Serialize(), a new Object() and it is serialized as {} but I cant assign ActionItem object as a new Object()

How can I achieve my desired result for this?

UPDATE
I made it work somehow after watching this snippet. I just changed the ActionItem to ExpandoObject.

0 Answers
Related