As reported by I Programmer, Microsoft has introduced a fresh, dynamic type to Azure.Core, enabling users to handle raw JSON data as a strongly typed model. It’s a big day for Azure, as it was also reported that a vulnerability had been discovered in the Microsoft Azure Active Directory (AD) Open Authorization (OAuth).
According to the Azure SDK team, they are incorporating protocol methods into Azure SDK client libraries. These methods offer a raw Response instead of the usual Response that contains a strongly typed model. When utilizing these methods, users have the flexibility to parse the response content using any JSON parser, such as JsonDocument.
The team acknowledges that utilizing existing APIs can head to code that is challenging to read and may not effectively express one’s intentions. To address this issue, the latest release of Azure.Core introduces a solution for retrieving response content as a dynamic type. This feature allows you to write code at the protocol layer that resembles the code written using a client’s convenience layer.
The Content property on Response now uses the BinaryData type, enhanced with a new extension method called ToDynamicFromJson. By invoking this method, you receive an instance of a newly introduced type called DynamicData, which enables you to treat JSON content as a strongly typed model. The dynamic JSON returned by this method is mutable, allowing you to modify its properties or even add new properties to it.
As the raw Response may not have the same appearance as a model type, as .NET types typically do not employ camel-case property names, Microsoft provides a “ToDynamicFromJson” method to enhance its correctness. By supplying a propertyNameFormat parameter to this method, you can specify the naming convention used in your JSON property names. DynamicData will then convert your C# property names to match the JSON content’s naming convention. The developers emphasize that this is part of a broader design objective for the type, aiming to make it behave like an Azure SDK model type.