Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/SendGrid/Helpers/Mail/SendGridMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1106,5 +1106,28 @@ public string Serialize(bool useDefaultSerialization = true)

return textWriter.ToString();
}

/// <summary>
/// Creates a SendGrid.Helpers.Mail.SendGridMessage instance from a JSON object.
/// </summary>
/// <param name="json">The JSON object to deserialize.</param>
/// <returns>The SendGrid.Helpers.Mail.SendGridMessage instance created from the JSON object.</returns>
public static SendGridMessage Deserialize(string json)
{
var jsonSerializerSettings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Include,
StringEscapeHandling = StringEscapeHandling.EscapeHtml,
Formatting = Formatting.None
};

var jsonSerializer = JsonSerializer.Create(jsonSerializerSettings);

JsonTextReader reader = new JsonTextReader(new StringReader(json));
SendGridMessage message = jsonSerializer.Deserialize<SendGridMessage>(reader);

return message;
}
}
}
5 changes: 5 additions & 0 deletions tests/SendGrid.Tests/Integration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,11 @@ private void TestKitchenSinkInternal(bool useDefaultSerialization = true)
"some term");
msg.SetReplyTo(new EmailAddress("[email protected]", "Reply To Me"));
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));

// Ensure serializing a SendGridMessage from deserialization generates the same JSON object to send to Twilio SendGrid.
var json = msg.Serialize(useDefaultSerialization);
var msg2 = SendGridMessage.Deserialize(json);
Assert.Equal(json, msg2.Serialize(useDefaultSerialization));
}

[Fact]
Expand Down