Skip to content

Commit 2f3ca52

Browse files
authored
feat: add SendGridMessage deserialization (#1115)
1 parent 69261a6 commit 2f3ca52

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/SendGrid/Helpers/Mail/SendGridMessage.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,5 +1106,28 @@ public string Serialize(bool useDefaultSerialization = true)
11061106

11071107
return textWriter.ToString();
11081108
}
1109+
1110+
/// <summary>
1111+
/// Creates a SendGrid.Helpers.Mail.SendGridMessage instance from a JSON object.
1112+
/// </summary>
1113+
/// <param name="json">The JSON object to deserialize.</param>
1114+
/// <returns>The SendGrid.Helpers.Mail.SendGridMessage instance created from the JSON object.</returns>
1115+
public static SendGridMessage Deserialize(string json)
1116+
{
1117+
var jsonSerializerSettings = new JsonSerializerSettings
1118+
{
1119+
NullValueHandling = NullValueHandling.Ignore,
1120+
DefaultValueHandling = DefaultValueHandling.Include,
1121+
StringEscapeHandling = StringEscapeHandling.EscapeHtml,
1122+
Formatting = Formatting.None
1123+
};
1124+
1125+
var jsonSerializer = JsonSerializer.Create(jsonSerializerSettings);
1126+
1127+
JsonTextReader reader = new JsonTextReader(new StringReader(json));
1128+
SendGridMessage message = jsonSerializer.Deserialize<SendGridMessage>(reader);
1129+
1130+
return message;
1131+
}
11091132
}
11101133
}

tests/SendGrid.Tests/Integration.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,11 @@ private void TestKitchenSinkInternal(bool useDefaultSerialization = true)
336336
"some term");
337337
msg.SetReplyTo(new EmailAddress("[email protected]", "Reply To Me"));
338338
Assert.Equal("{\"from\":{\"name\":\"Example User1\",\"email\":\"[email protected]\"},\"subject\":\"Hello World from the Twilio SendGrid CSharp Library\",\"personalizations\":[{\"to\":[{\"name\":\"Example User2\",\"email\":\"[email protected]\"},{\"name\":\"Example User-2\",\"email\":\"[email protected]\"},{\"name\":\"Example User3\",\"email\":\"[email protected]\"},{\"name\":\"Example User4\",\"email\":\"[email protected]\"},{\"name\":\"Example User5\",\"email\":\"[email protected]\"}],\"cc\":[{\"name\":\"Example User6\",\"email\":\"[email protected]\"},{\"name\":\"Example User7\",\"email\":\"[email protected]\"},{\"name\":\"Example User8\",\"email\":\"[email protected]\"},{\"name\":\"Example User9\",\"email\":\"[email protected]\"},{\"name\":\"Example User-9\",\"email\":\"[email protected]\"}],\"bcc\":[{\"name\":\"Example User10\",\"email\":\"test10example.com\"},{\"name\":\"Example User11\",\"email\":\"[email protected]\"},{\"name\":\"Example User12\",\"email\":\"[email protected]\"},{\"name\":\"Example User13\",\"email\":\"[email protected]\"},{\"name\":\"Example User-13\",\"email\":\"[email protected]\"}],\"subject\":\"Thank you for signing up, % name %\",\"headers\":{\"X-Test1\":\"True1\",\"X-Test2\":\"Test2\",\"X-Test3\":\"True3\",\"X-Test4\":\"True4\"},\"substitutions\":{\"%name1%\":\"Example User1\",\"%city2%\":\"Denver1\",\"%name3%\":\"Example User2\",\"%city4%\":\"Orange1\"},\"custom_args\":{\"marketing1\":\"false\",\"transactional1\":\"true\",\"marketing2\":\"true\",\"transactional2\":\"false\"},\"send_at\":1461775051},{\"to\":[{\"name\":\"Example User14\",\"email\":\"[email protected]\"},{\"name\":\"Example User15\",\"email\":\"[email protected]\"},{\"name\":\"Example User16\",\"email\":\"[email protected]\"},{\"name\":\"Example User17\",\"email\":\"[email protected]\"}],\"cc\":[{\"name\":\"Example User18\",\"email\":\"[email protected]\"},{\"name\":\"Example User19\",\"email\":\"[email protected]\"},{\"name\":\"Example User20\",\"email\":\"[email protected]\"},{\"name\":\"Example User21\",\"email\":\"[email protected]\"}],\"bcc\":[{\"name\":\"Example User22\",\"email\":\"test22example.com\"},{\"name\":\"Example User23\",\"email\":\"[email protected]\"},{\"name\":\"Example User24\",\"email\":\"[email protected]\"},{\"name\":\"Example User25\",\"email\":\"[email protected]\"}],\"subject\":\"Thank you for signing up, % name % 2\",\"headers\":{\"X-Test5\":\"True5\",\"X-Test6\":\"Test6\",\"X-Test7\":\"True7\",\"X-Test8\":\"True8\"},\"substitutions\":{\"%name5%\":\"Example User5\",\"%city6%\":\"Denver6\",\"%name7%\":\"Example User7\",\"%city8%\":\"Orange8\"},\"custom_args\":{\"marketing3\":\"false\",\"transactional3\":\"true\",\"marketing4\":\"true\",\"transactional4\":\"false\"},\"send_at\":1461775052},{\"to\":[{\"name\":\"Example User26\",\"email\":\"[email protected]\"},{\"name\":\"Example User27\",\"email\":\"[email protected]\"},{\"name\":\"Example User28\",\"email\":\"[email protected]\"},{\"name\":\"Example User29\",\"email\":\"[email protected]\"}],\"cc\":[{\"name\":\"Example User30\",\"email\":\"[email protected]\"},{\"name\":\"Example User31\",\"email\":\"[email protected]\"},{\"name\":\"Example User32\",\"email\":\"[email protected]\"},{\"name\":\"Example User33\",\"email\":\"[email protected]\"}],\"bcc\":[{\"name\":\"Example User34\",\"email\":\"test34example.com\"},{\"name\":\"Example User35\",\"email\":\"[email protected]\"},{\"name\":\"Example User36\",\"email\":\"[email protected]\"},{\"name\":\"Example User37\",\"email\":\"[email protected]\"}],\"subject\":\"Thank you for signing up, % name % 3\",\"headers\":{\"X-Test7\":\"True7\",\"X-Test8\":\"Test8\",\"X-Test9\":\"True9\",\"X-Test10\":\"True10\"},\"substitutions\":{\"%name9%\":\"Example User9\",\"%city10%\":\"Denver10\",\"%name11%\":\"Example User11\",\"%city12%\":\"Orange12\"},\"custom_args\":{\"marketing5\":\"false\",\"transactional5\":\"true\",\"marketing6\":\"true\",\"transactional6\":\"false\"},\"send_at\":1461775053}],\"content\":[{\"type\":\"text/plain\",\"value\":\"Textual content\"},{\"type\":\"text/html\",\"value\":\"HTML content\"},{\"type\":\"text/calendar\",\"value\":\"Party Time!!\"},{\"type\":\"text/calendar2\",\"value\":\"Party Time2!!\"}],\"attachments\":[{\"content\":\"TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12\",\"type\":\"application/pdf\",\"filename\":\"balance_001.pdf\",\"disposition\":\"attachment\",\"content_id\":\"Balance Sheet\"},{\"content\":\"BwdW\",\"type\":\"image/png\",\"filename\":\"banner.png\",\"disposition\":\"inline\",\"content_id\":\"Banner\"},{\"content\":\"BwdW2\",\"type\":\"image/png\",\"filename\":\"banner2.png\",\"disposition\":\"inline\",\"content_id\":\"Banner 2\"}],\"template_id\":\"13b8f94f-bcae-4ec6-b752-70d6cb59f932\",\"headers\":{\"X-Day\":\"Monday\",\"X-Month\":\"January\",\"X-Year\":\"2017\"},\"sections\":{\"%section1\":\"Substitution for Section 1 Tag\",\"%section2%\":\"Substitution for Section 2 Tag\",\"%section3%\":\"Substitution for Section 3 Tag\"},\"categories\":[\"customer\",\"vip\",\"new_account\"],\"custom_args\":{\"campaign\":\"welcome\",\"sequence2\":\"2\",\"sequence3\":\"3\"},\"send_at\":1461775051,\"asm\":{\"group_id\":3,\"groups_to_display\":[1,4,5]},\"batch_id\":\"some_batch_id\",\"ip_pool_name\":\"23\",\"mail_settings\":{\"bcc\":{\"enable\":true,\"email\":\"[email protected]\"},\"bypass_list_management\":{\"enable\":true},\"footer\":{\"enable\":true,\"text\":\"Some Footer Text\",\"html\":\"Some Footer HTML\"},\"sandbox_mode\":{\"enable\":true},\"spam_check\":{\"enable\":true,\"threshold\":1,\"post_to_url\":\"https://gotchya.example.com\"}},\"tracking_settings\":{\"click_tracking\":{\"enable\":true,\"enable_text\":false},\"open_tracking\":{\"enable\":true,\"substitution_tag\":\"Optional tag to replace with the open image in the body of the message\"},\"subscription_tracking\":{\"enable\":true,\"text\":\"text to insert into the text/plain portion of the message\",\"html\":\"HTML to insert into the text / html portion of the message\",\"substitution_tag\":\"substitution tag\"},\"ganalytics\":{\"enable\":true,\"utm_source\":\"some source\",\"utm_medium\":\"some medium\",\"utm_term\":\"some term\",\"utm_content\":\"some content\",\"utm_campaign\":\"some campaign\"}},\"reply_to\":{\"name\":\"Reply To Me\",\"email\":\"[email protected]\"}}", msg.Serialize(useDefaultSerialization));
339+
340+
// Ensure serializing a SendGridMessage from deserialization generates the same JSON object to send to Twilio SendGrid.
341+
var json = msg.Serialize(useDefaultSerialization);
342+
var msg2 = SendGridMessage.Deserialize(json);
343+
Assert.Equal(json, msg2.Serialize(useDefaultSerialization));
339344
}
340345

341346
[Fact]

0 commit comments

Comments
 (0)