How to deal with Microsoft.Kiota.Abstractions.Serialization.Untyped* instances #2422
-
I'm currently struggling with reading instances of customSecurityAttributeValue using Microsoft.Graph 5.40.0. This property has an AdditionalData property containing a Dictionary. The Key seems to be the AttributeSet. The value is of type So what is the suggested generic way deserialize/read data from UntypedObject, UntypedInteger, UntypedArray etc. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
Thanks for raising this @stefanboerner Ideally, the model should be defined in the metadata as type so that it shows up as a strongly typed property. However, since this is only known at runtime, you can probably parse this as below. private static void ParseUnknownObject(UntypedNode untypedNode, string indent = "")
{
switch (untypedNode)
{
case UntypedObject untypedObject:
Console.WriteLine(indent + "Found object value: ");
var properties = untypedObject.GetValue();
foreach (var (name, node) in properties)
{
Console.WriteLine(indent + "Property Name: " + name);
ParseUnknownObject(node, indent + " ");
}
break;
case UntypedArray untypedArray:
Console.WriteLine(indent + "Found array value: ");
foreach (var item in untypedArray.GetValue())
{
Console.WriteLine(indent + "New Item: ");
ParseUnknownObject(item, indent + " ");
}
break;
default: //its a scalar value like a string/integer/bool
Console.WriteLine(indent + "Found scalar value : " + KiotaJsonSerializer.SerializeAsString(untypedNode));
break;
}
} Alternatively, if you wish to get the property as a json string representation of the object, you can also call and parse it with your favorite json parsing library. var jsonString = KiotaJsonSerializer.SerializeAsString(untypedNode); Lemme know if this is helpful. |
Beta Was this translation helpful? Give feedback.
-
I don't have answer, but facing similar problems after upgrading from v5.43.0 to v5.48.0 (latest). In v5.43.0, I can simply deserialize using
In v5.48.0, the above code throws an error as So like the OP question, what is the current way to access data returned in /cc @andrueastman |
Beta Was this translation helpful? Give feedback.
-
@andrueastman Thank you for providing the sample. I think, that access to CustomSecurityAttributes should be way easier since the definition of those attributes is pretty straightforward. However, I managed to read the values of the attributes to a simple data structure by looking at returned data structures and "parse" them accordingly. When I try to update (write) security attributes I do it vice versa that means I construct UntypedObjects/Strings/Integers etc. However, there seem to be an issue with CustomSecurityAttributes with type "Integer". Although I send an "UntypedInteger" value for a particular attribute, the following exception is thrown:
Screenshot of the CustomSecurityAttribute instance "retvl" used to send the patch Edit: Found the issue: i forgot to add the odata type for the integer value. |
Beta Was this translation helpful? Give feedback.
Thanks for raising this @stefanboerner
Ideally, the model should be defined in the metadata as type so that it shows up as a strongly typed property. However, since this is only known at runtime, you can probably parse this as below.