0

Suppose I have 2 objects:

public class CInfo
{
    [JsonProperty(PropertyName = "Id")]
    public string cID { get; set; }

    [JsonProperty(PropertyName = "Name")]
    public string cName { get; set; }
}

public class Event
{
    [JsonProperty(PropertyName = "Time")]
    public DateTime dateTime { get; set; }

    [JsonProperty(PropertyName = "Note")]
    public string comment { get; set; } 
}

Couple of functions return the following:

CInfo cInfo = getCInfo();
List<Event> = getEvents();

Both of these meant to be combined together, into a final (hypothetical) object that needs to look this:

public class CInfoWithEvents
{
    [JsonProperty(PropertyName = "Id")]
    public string cID { get; set; }

    [JsonProperty(PropertyName = "Name")]
    public string cName { get; set; }

    [JsonProperty(PropertyName = "Events")]
    public List<Event> { get; set; }
}

At which point the intention is to do a JsonConvert.SerialzeObject(CInfoWithEvents), and get a json string representing this final object.

Question: what is the best way to combine them? There has to be a more elegant way than just creating whole new object and copying each property, or worse: serialize cInfo and List<Event> separately and do some sort of string manipulation.

Thank you for your time

concentriq
  • 359
  • 2
  • 6
  • 16
  • Include the entire `CInfo` as a single property? `class CInfoWithEvents { public CInfo CInfo {get;set;}; public List {get;set;}}` Or do something like https://stackoverflow.com/q/42836936/11683? – GSerg Dec 28 '18 at 20:02
  • @GSerg I appreciate the response, but Im not sure were talking about the same thing: final object is basically a `CInfo` with `List` property added. I considered creating a derived class `class CInfoWithEvents:cInfo { [JsonProperty(PropertyName = "Events")] public List { get; set; }}` but i'd be back to just copying properties one by one if I wanted to populate this new object. – concentriq Dec 28 '18 at 20:14

1 Answers1

1

you can implement this in various ways besides copying each property of both classes:

1) Add both class as public property in the third class.

public class CInfoWithEvents
{
    [JsonProperty(PropertyName = "CInfo")]
    public CInfo  {get; set;}

    [JsonProperty(PropertyName = "Events")]
    public List<Event> { get; set; }
}

2) Inherit from CInfo class and have a List as public property.

public class CInfoWithEvents : CInfo
{        
    [JsonProperty(PropertyName = "Events")]
    public List<Event> { get; set; }
}

3) Create on the fly:

  var javaScriptSerializer = new JavaScriptSerializer();
  var resultJson =  javaScriptSerializer.Serialize(new { CInfo = getCInfo(), Events = getEvents()});

4) Using JObject

    JObject jCInfo = JObject.FromObject(getCInfo());
    jCInfo.Add("Events", JArray.FromObject(getEvents()));
    string json = jCInfo.ToString();
Sonal Borkar
  • 531
  • 1
  • 6
  • 12
  • Thank you. I appreciate your response. 1 and 3 produce the same result and both result in a structure that is different from what I am looking for: they are not equivalent to `CInfoWithEvents` object. #2 would result in a correct structure, but in order to populate this derived object I would need to assign each property individually, which i want to avoid for the purposes of future maintenance. – concentriq Dec 28 '18 at 20:52
  • 1
    please check #4 – Sonal Borkar Dec 28 '18 at 21:08
  • this is it! thank you kindly!! one small correction to this specific example is that it shoudl be `jCInfo.Add("Events", JArray.FromObject(getEvents()));', otherwise #4 is the solution – concentriq Dec 28 '18 at 21:28
  • Welcome, Edited my answer now. Enjoy Coding! !! – Sonal Borkar Dec 28 '18 at 21:53