C# API serialize/deserialize json

I am using the C# API client (version 15.10.0) to query ScheduleEventService. That all works fine and I’m able to get the records I need. My problem is that I want to be able to serialize that data and save it locally in case of a network outage when my application is restarted. To accomplish this, I want to serialize the ScheduleEvent data to json using

JsonConvert.SerializeObject(myScheduleEventItem, Formatting.Indented).

Eventually I will want to serialize an entire list of ScheduleEvent objects, but I haven’t been able to get that far, so in the meantime I’m just serializing individual objects for testing purposes. This works except the json nodes end up matching the Pascal case of the class names. This appears to be a problem because the deserialization process is looking for camel case node names. Using the following lines generates empty objects.

dynamic jToken = JToken.Parse(json);
ScheduleEvent myDeserializedScheduleEvent = new ScheduleEvent(JToken.Parse(json));

I assume this is the correct way to deserialize the json data. I would prefer to use the following built-in deserialization process, but that doesn’t seem to work, either.

myDeserializedScheduleEvent = JsonConvert.DeserializeObject(json);

I get error message “Unable to find a constructor to use for type Kaltura.Enums.ScheduleEventRecurrenceFrequency.”

So, next I tried to modify the Kaltura client to specify camel case json node names in the JsonProperty attribute (example [JsonProperty(“startDate”)] on each of the properties of the ScheduleEvent class and its base class hierarchy. This seems to have worked for the most part, but I now get an error message at the ObjectFactory class, indicating “Cannot access child value on Newtonsoft.Json.Linq.JValue.”

Have I broken something by declaring the json property names to something different? If so, and if those need to be removed, how do I serialize the data so it can be deserialized later? Json seemed to be the preferred method based on the API client code.

I think I found the answer. The correct way to serialize and deserialize are built into the Kaltura client code.

To serialize:
string json = scheduleEvent.ToParams().ToJson();

To deserialize:
ScheduleEvent scheduleEvent = new ScheduleEvent(JToken.Parse(json));

So I’m able to serialize and deserialize the individual ScheduleEvent objects, but now I want to serialize the ListResponse<ScheduleEvent> list returned by the OnCompletedHandler. I tried

string json = result.ToParams().ToJson();

but it doesn’t work. It doesn’t produce any errors, but the resulting json is empty {}. I then tried to roll my own with

string[] values = new string[result.TotalCount];
int index = 0;

foreach (ScheduleEvent scheduleEvent in result.Objects)
	values[index++] = scheduleEvent.ToParams().ToJson();

string json = string.Format("{{\"objects\":[{0}],\"objectType\":\"KalturaScheduleEventListResponse\"}}", string.Join(",", values));

There is surely a method in the API client that does this for me. Regardless, this didn’t work because I couldn’t deserialize it with

ListResponse<ScheduleEvent> deserializedListResponse = new ListResponse<ScheduleEvent>(JToken.Parse(json));

I get error message

‘Cannot access child value on Newtonsoft.Json.Linq.JValue.’

on the following line of the client code in the ObjectFactory class.

if (jToken["objectType"] == null)

Are there built-in serialize/deserialize methods for the ListResponse<T> objects? Surely there are.

I believe the error message was a result of a bug in the API client code. There appears to be two class names for the ScheduleEvent type; ScheduleEvent and RecordScheduleEvent. The bug appears to be that RecordScheduleEvent inherits EntryScheduleEvent rather than ScheduleEvent. After fixing that, the following works to deserialize ListResponse<ScheduleEvent>

ListResponse deserializedListResponse = new ListResponse(JToken.Parse(json));

I now need to learn how to properly serialize the list. In the meantime, I’ll use the following snippet. As it turns out, including the objectType of KalturaScheduleEvent for the ListResponse doesn’t appear to be necessary.

List<string> values = new List<string>();

foreach (ScheduleEvent scheduleEvent in result.Objects)

string json = string.Format("{{\"objects\":[{0}]}}", string.Join(",", values));

I hope this information is useful to someone.

I’m wrong. The API appears to be correct. I was trying to deserialize to a ScheduleEvent type rather than a RecordScheduleEvent type. Once I corrected that, the deserialization process seems to work correctly with the unmodified API.

The underlying problem, which made me think I needed to deserialize to a ScheduleEvent type, was that the results of a ScheduleEventService.List request are of the ScheduleEvent type, not RecordScheduleEvent nor EntryScheduleEvent, and I can’t seem to change that. The properties of the class EntryScheduleEvent, which is child of ScheduleEvent and parent of RecordScheduleEvent, are lost in the process. Those properties include templateEntryId, entryIds, categoryIds, and blackoutConflicts. Casting ScheduleEvent to RecordScheduleEvent doesn’t seem to expose the values of those properties.