Azure Document Intelligence client library for .NET, Azure.AI.DocumentIntelligence 1.0.0 was released few weeks ago with some significant refactoring to existing beta packages, and it has some breaking changes.
One of the most important ones is Serialization in different options.
Consider the following for an example.
using Azure.AI.DocumentIntelligence;
using System.Text.Json;
JsonSerializerOptions jsonSerializerOptions = new()
{
WriteIndented = true
};
Dictionary<string, ClassifierDocumentTypeDetails> documentTypes = new()
{
{ "Document1", new(new BlobFileListContentSource(new Uri("https://www.some-uri.com"), "fileList1")) },
{ "Document2", new(new BlobFileListContentSource(new Uri("https://www.some-uri.com"), "fileList2")) }
};
BuildClassifierOptions buildClassifierOptions = new("someClassifierId", documentTypes);
string json = JsonSerializer.Serialize(buildClassifierOptions, jsonSerializerOptions);
Console.WriteLine(json);
//{
// "ClassifierId": "someClassifierId",
// "Description": null,
// "BaseClassifierId": null,
// "DocumentTypes": {
// "Document1": {
// "BlobSource": null,
// "BlobFileListSource": {
// "ContainerUri": "https://www.some-uri.com",
// "FileList": "fileList1"
// },
// "SourceKind": null
// },
// "Document2": {
// "BlobSource": null,
// "BlobFileListSource": {
// "ContainerUri": "https://www.some-uri.com",
// "FileList": "fileList2"
// },
// "SourceKind": null
// }
// },
// "AllowOverwrite": null
//}
buildClassifierOptions = JsonSerializer.Deserialize<BuildClassifierOptions>(json, jsonSerializerOptions);
The above will error out on Deserialize; with an error like "Unhandled exception. System.NotSupportedException: Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with 'JsonConstructorAttribute' is not supported".
Same error will happen with different types of Options.
In order for Serialization to work, we need to add JsonModelConverter to JsonSerializerOptions, a custom JsonConverter that is already available in System.ClientModel.
using System.ClientModel.Primitives;
using System.Text.Json;
JsonSerializerOptions jsonSerializerOptions = new()
{
WriteIndented = true,
Converters = { new JsonModelConverter() } // Converter to handle IJsonModel<T> types
};
More read:
System.ClientModel-based ModelReaderWriter samples
Hope this helps.
Happy Coding.
Regards,
Jaliya