Share via

Problem deserializing Microsoft.Graph objects

Dmitriy Reznik 241 Reputation points
2023-08-31T14:58:40.98+00:00

I use Microsoft.Graph apis in my app. I make a request and get back the json data. But I am really confused trying to deserialize it. E.g. for this json:

{
  "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#Collection(microsoft.graph.scheduleInformation)",
  "value": [
    {
      "scheduleId": "******@company.com",
      "availabilityView": "000000002022000",
      "scheduleItems": [
        {
          "isPrivate": false,
          "status": "busy",
          "subject": "Standup",
          "location": "Microsoft Teams Meeting",
          "isMeeting": true,
          "isRecurring": true,
          "isException": false,
          "isReminderSet": true,
          "start": {
            "dateTime": "2023-08-30T13:00:00.0000000",
            "timeZone": "UTC"
          },
          "end": {
            "dateTime": "2023-08-30T13:30:00.0000000",
            "timeZone": "UTC"
          }
        } 
      ],
      "workingHours": {
        "daysOfWeek": [
          "monday",
          "tuesday",
          "wednesday",
          "thursday",
          "friday"
        ],
        "startTime": "08:00:00.0000000",
        "endTime": "17:00:00.0000000",
        "timeZone": {
          "name": "Eastern Standard Time"
        }
      }
    },
    {
      "scheduleId": "******@company.com",
      "availabilityView": "000000000000000",
      "scheduleItems": [],
      "workingHours": {
        "daysOfWeek": [
          "monday",
          "tuesday",
          "wednesday",
          "thursday",
          "friday"
        ],
        "startTime": "08:00:00.0000000",
        "endTime": "17:00:00.0000000",
        "timeZone": {
          "name": "Eastern Standard Time"
        }
      }
    }    
  ]
}

I created a class for the root object:

public class ScheduleInformationRootObject
{
    [JsonProperty("@odata.context")]
    public string ODataContext { get; set; }
    [JsonProperty("value")]
    public List<ScheduleInformation> Value { get; set; }
}

Maybe Microsoft.Graph has already such a class, but I could not find it. If it exists, please let me know. So I tried to deserialize like this:

ScheduleInformationRootObject outlookScheduleInfo = JsonConvert.DeserializeObject<ScheduleInformationRootObject>(responseJson);

But this gives me an exception:

Newtonsoft.Json.JsonSerializationException: 'Error converting value "08:00:00.0000000" to type 'System.Nullable`1[Microsoft.Kiota.Abstractions.Time]'. Path 'value[0].workingHours.startTime', line 1, position 1344.'

I also tried to deserialize using another library:

        var options = new JsonSerializerOptions
        {
            PropertyNameCaseInsensitive = true // Enable case-insensitive property matching
        };

        ScheduleInformationRootObject outlookScheduleInfo = System.Text.Json.JsonSerializer.Deserialize<ScheduleInformationRootObject>(responseJson, options);

This gives me another exception:

The JSON value could not be converted to System.Nullable`1[Microsoft.Graph.Models.FreeBusyStatus]. Path: $.value[0].scheduleItems[0].status | LineNumber: 0 | BytePositionInLine: 248.

In this case, it cannot convert the string value of "status" field to an object of class FreeBusyStatus.

I can re-write Microsoft.Graph's classes, but it doesn't seem to be the right approach.

I noticed that class ScheduleInformation has

    /// <summary>
    /// The deserialization information for the current model
    /// </summary>
    public IDictionary<string, Action<IParseNode>> GetFieldDeserializers() {
        return new Dictionary<string, Action<IParseNode>> {
            {"availabilityView", n => { AvailabilityView = n.GetStringValue(); } },
            {"error", n => { Error = n.GetObjectValue<FreeBusyError>(FreeBusyError.CreateFromDiscriminatorValue); } },
            {"@odata.type", n => { OdataType = n.GetStringValue(); } },
            {"scheduleId", n => { ScheduleId = n.GetStringValue(); } },
            {"scheduleItems", n => { ScheduleItems = n.GetCollectionOfObjectValues<ScheduleItem>(ScheduleItem.CreateFromDiscriminatorValue)?.ToList(); } },
            {"workingHours", n => { WorkingHours = n.GetObjectValue<Microsoft.Graph.Models.WorkingHours>(Microsoft.Graph.Models.WorkingHours.CreateFromDiscriminatorValue); } },
        };
    }

But cannot figure out how this should be used.

Microsoft Security | Microsoft Graph
0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.