Search code examples
c#cloud-document-ai

Convert ProcessResponse to json and then back to a ProcessResponse object again


I'm working on a document AI implementation where we are sending multiple related requests gathering the responses and re-combining them. This is all done in C#. As part of this I need to convert a ProcessResponse to a json string and then downstream convert it back to a ProcessResponse object. What is the best way to do this?

I was looking at using JsonFormatter to create the json string but I can't find a way to convert it back to a ProcessResponse. From what I'm reading this seems like a common problem encountered with all Proto Buffer related objects.


Solution

  • The linked answer in the comments is an option.

    How to read Json response into Document object of Document AI in C#

    string json = File.ReadAllText("C:\\upload\\temp.json");
    Document document = Document.Parser.ParseJson(json);
    

    This is not entirely related, but could be useful. For Python, there is the Document AI Toolbox which creates an interface around the Document object to make it easier to handle. (Including consolidating the Document shards from Batch Processing.) You can look at the open source library code to get a sense of how to handle this and make your own in C#.