public void OnResumeBookmark(NativeActivityContext context, Bookmark bookmark, object data) { Trace.TraceInformation($"Resuming : {nameof(WorkflowRestorePoint<TOut>)}, from bookmark : {bookmark.Name}"); TOut output = default(TOut); if (data is TOut) { output = (TOut)data; } else { try { //output = (TOut)Convert.ChangeType(data, typeof(TOut)); //fails when input is pure object (not this type) output = JsonUtils.Cast <TOut>(data); Trace.TraceInformation($"From : {nameof(WorkflowRestorePoint<TOut>)}. Bookmark resume data : {output.ToJson()}"); // When the Bookmark is resumed, assign its value to the Result argument Result.Set(context, output); } catch (InvalidCastException e) { Trace.TraceError($"From : {nameof(WorkflowRestorePoint<TOut>)}. Error casting data. Exception: {e.Message}"); Trace.Flush(); throw; } } Trace.Flush(); }
void CastAndVerifyResult(object obj, string expectedMessage) { var result = JsonUtils.Cast <ApprovalData>(obj); Assert.NotNull(result); Assert.Equal(result.Message, expectedMessage); }
public void Can_handle_null() { ApprovalData obj = null; var result = JsonUtils.Cast <ApprovalData>(obj); Assert.Null(result); }
public void Can_cast_string() { var approvalData = "This is a cool message"; var result = JsonUtils.Cast <string>(approvalData); Assert.NotNull(result); Assert.Equal(approvalData, result); }
public void Can_cast_quoted_string_with_special_chars() { var approvalData = "Hi \"sis\", what's up? How are kid #1 & kid #2?!!"; var result = JsonUtils.Cast <string>(approvalData); Assert.NotNull(result); Assert.Equal(approvalData, result); }
public void Can_cast_generic_dictionary_in_json_object_format() { var data = "{\"Name\":\"ac1\",\"Message\":{\"IsApproved\":false,\"Message\":\"Humans are not cool\"}}"; var result = JsonUtils.Cast <IDictionary <string, object> >(data); Assert.NotNull(result); Assert.NotNull(JsonUtils.Cast <ApprovalData>(result["Message"])); Assert.Equal(JsonUtils.Cast <ApprovalData>(result["Message"]).Message, "Humans are not cool"); }
public void Can_cast_object_with_dictionary_property_from_json() { var obj = "{\"IsApproved\":true,\"Messages\":{\"Name\":\"ac1\",\"Message\":{\"IsApproved\":false,\"Message\":\"Humans are not cool\"}}}"; var result = JsonUtils.Cast <InvoiceData>(obj); Assert.NotNull(result); var innerDictionaryType = result.Messages["Message"].GetType().FullName; //Newtonsoft.Json.Linq.JObject //Assert.IsType<ApprovalData>(result.Messages["Message"]); obviously it does not know the type Assert.NotNull(JsonUtils.Cast <ApprovalData>(result.Messages["Message"])); //Assert.Equal((result.Messages["Message"] as ApprovalData).Message, "Humans are not cool"); doesn't work for same reason Assert.Equal(JsonUtils.Cast <ApprovalData>(result.Messages["Message"]).Message, "Humans are not cool"); }
public void Can_cast_generic_dictionary() { var data = new Dictionary <string, object> { { "Name", "ac1" }, { "Message", new ApprovalData { IsApproved = false, Message = "Humans are not cool" } } }; var result = JsonUtils.Cast <IDictionary <string, object> >(data); Assert.NotNull(result); Assert.IsType <ApprovalData>(result["Message"]); Assert.Equal((result["Message"] as ApprovalData).Message, "Humans are not cool"); }
public void Can_cast_object_with_dictionary_property() { var obj = new InvoiceData { IsApproved = true, Messages = new Dictionary <string, object> { { "Name", "ac1" }, { "Message", new ApprovalData { IsApproved = false, Message = "Humans are not cool" } } } }; var result = JsonUtils.Cast <InvoiceData>(obj); Assert.NotNull(result); Assert.IsType <ApprovalData>(result.Messages["Message"]); Assert.Equal((result.Messages["Message"] as ApprovalData).Message, "Humans are not cool"); }
public void OnResumeBookmark(NativeActivityContext context, Bookmark bookmark, object data) { Trace.TraceInformation($"Resuming : {nameof(WorkflowRestorePoint<TOut>)}, from bookmark : {bookmark.Name}"); TOut output = default(TOut); try { output = JsonUtils.Cast <TOut>(data); Trace.TraceInformation($"From : {nameof(WorkflowRestorePoint<TOut>)}. Bookmark resume data : {output.ToJson()}"); } catch (InvalidCastException e) { Trace.TraceError($"From : {nameof(WorkflowRestorePoint<TOut>)}. Error casting data. Exception: {e.Message}"); Trace.Flush(); throw; } // When the Bookmark is resumed, assign its value to the Result argument Result.Set(context, output); Trace.Flush(); }
void CastAndVerifyException(object obj) { Assert.ThrowsAny <Exception>(() => JsonUtils.Cast <ApprovalData>(obj)); }