admin管理员组

文章数量:1399509

Want to persist, in c#, the result of a document analysis and rehydrate back to result model at later time/date.

Generate a document analysis in the standard manner using:

Operation<AnalyzeResult> operation = await _client.AnalyzeDocumentAsync(WaitUntil.Completed, options);

AnalyzeResult result = operation.Value; 

Am processing results from this analysis, but would like to save the analysis result to rehydrate.

Have seen working examples in python and various internal methods within the AnalyzeResult and IJsonModel c# implementations, but cannot find a reference or way in to both Write and Read into the AnalyzeResult class

All possible construction methods in AnalyzeResult appear to be internal and suffer similar problem when using class as type IJsonModel

Don't really want to do via reflection.

Would expect being able to serialize/deserialize in some manner as when using batch processing associated with storage accounts the result is stored as json in the container.

Want to persist, in c#, the result of a document analysis and rehydrate back to result model at later time/date.

Generate a document analysis in the standard manner using:

Operation<AnalyzeResult> operation = await _client.AnalyzeDocumentAsync(WaitUntil.Completed, options);

AnalyzeResult result = operation.Value; 

Am processing results from this analysis, but would like to save the analysis result to rehydrate.

Have seen working examples in python and various internal methods within the AnalyzeResult and IJsonModel c# implementations, but cannot find a reference or way in to both Write and Read into the AnalyzeResult class

All possible construction methods in AnalyzeResult appear to be internal and suffer similar problem when using class as type IJsonModel

Don't really want to do via reflection.

Would expect being able to serialize/deserialize in some manner as when using batch processing associated with storage accounts the result is stored as json in the container.

Share Improve this question edited Mar 26 at 16:45 TylerH 21.1k78 gold badges79 silver badges114 bronze badges asked Mar 26 at 16:42 Alan EveryAlan Every 31 silver badge2 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

This can be done using the ModelReaderWriter type or using System.Text.Json with the JsonModelConverter registered.

An example of both approaches can be found in System.ClientModel-based ModelReaderWriter samples.

Applied to your scenario, this would look something like:

ModelReaderWriter

// Serialize the result
AnayzeResult model = operation.Value;
BinaryData data = ModelReaderWriter.Write(model);

// Deserialize
string jsonFromStorage = GetSerializedResult();

AnayzeResult? model = 
    ModelReaderWriter.Read<AnayzeResult>(BinaryData.FromString(json));

System.Text.Json

// Serialize the result
AnayzeResult model = operation.Value;

string data = JsonSerializer.Serialize(model, 
    new JsonSerializerOptions()
    {
        Converters = { new JsonModelConverter() }
    });

// Deserialize
string jsonFromStorage = GetSerializedResult();

AnayzeResult? model = 
    JsonSerializer.Deserialize<AnalyzeResult>(json, 
        new JsonSerializerOptions()
        {
            Converters = { new JsonModelConverter() }
        });

本文标签: How do I save and later load Azure DocumentIntelligence AnalyzeResult in cStack Overflow