0

I am working with an Azure Durable Function and I am having some trouble with System.Text.Json In a real scenario I will have a Activity Function which will call an API Endpoint and will get some Json result, which will have an Array with multiple types(string and int).

I will try to describe the problem. Do you know a way to fix it?

So I am using this class to deserialize it to a C# Object.

public class JsonTest
{
    public dynamic[] Test { get; set; }
}

And this is the sample Activity Function which does the deserialize.

[FunctionName("GetJson")]
    public static JsonTest GetJson([ActivityTrigger] string id)
    {
        var jsonString = "{ \"Test\": [\"Fabrizio\", 39] }";

        var result = JsonSerializer.Deserialize<JsonTest>(jsonString);

        return result;
    }

I can see that it works.

Imgur

However once I return the Object from the Activity Function to the Orchestrator something goes wrong. I don't have the values anymore. Even if I try to expand it. I will eventually get an error. It also use Newtonsoft.Json.Linq.JToken but I don't know how related it is since I am just using System.text.json

First = '(new System.Linq.SystemCore_EnumerableDebugView<System.Collections.Generic.KeyValuePair<string, Newtonsoft.Json.Linq.JToken>>(foo.Test[0]).Items[0]).Value.First' threw an exception of type 'System.InvalidOperationException'

Imgur Imgur

1 Answers1

0

I have reproduced the code and got the expected results.

Try below code:

JsonTest.cs

  public class JsonTest
  {     
     public dynamic[] Test { get; set; }
  }

Activity function:

  [FunctionName("GetJson")]
     public static JsonTest GetJson([ActivityTrigger] string id)
     {
     var json = "{ \"Test\": [\"Fabrizio\", 39] }"; 
         var result = JsonConvert.DeserializeObject<JsonTest>(json);
         Console.WriteLine($"Deserialized JSON into {result.GetType()}");
         return result;

     }

Orchestrator function:

[FunctionName("Function")]
     public static async Task<object> RunOrchestrator(
         [OrchestrationTrigger] IDurableOrchestrationContext context)
     {
       return await context.CallActivityAsync<object>("GetJson", "London");
         
     }

Output:

enter image description here

Pravallika KV
  • 2,415
  • 2
  • 2
  • 7