Skip to content

Commit

Permalink
feat: migrate from newtonsoft to system.text.json
Browse files Browse the repository at this point in the history
  • Loading branch information
paviad committed Jul 23, 2024
1 parent 4a31535 commit 12d167b
Show file tree
Hide file tree
Showing 56 changed files with 9,538 additions and 334 deletions.
2 changes: 1 addition & 1 deletion ExampleCoreProject/ExampleCoreProject.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<OutputType>Exe</OutputType>
<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down
18 changes: 16 additions & 2 deletions SendGrid.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29519.87
# Visual Studio Version 17
VisualStudioVersion = 17.10.35027.167
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{71C09624-020B-410E-A8FE-1FD216A1FE31}"
EndProject
Expand Down Expand Up @@ -38,6 +38,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
USE_CASES.md = USE_CASES.md
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SendGrid.Extensions.DependencyInjection.Net21Tests", "tests\SendGrid.Extensions.DependencyInjection.Net21Tests\SendGrid.Extensions.DependencyInjection.Net21Tests.csproj", "{24BE2F5A-2B03-4493-B518-6722556C6995}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SendGrid.Net21Tests", "tests\SendGrid.Net21Tests\SendGrid.Net21Tests.csproj", "{2F9B46FF-923A-47C4-AC52-6A621A8E0850}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -68,6 +72,14 @@ Global
{C65468A6-BDE2-4FD4-9400-B668B1395B26}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C65468A6-BDE2-4FD4-9400-B668B1395B26}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C65468A6-BDE2-4FD4-9400-B668B1395B26}.Release|Any CPU.Build.0 = Release|Any CPU
{24BE2F5A-2B03-4493-B518-6722556C6995}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{24BE2F5A-2B03-4493-B518-6722556C6995}.Debug|Any CPU.Build.0 = Debug|Any CPU
{24BE2F5A-2B03-4493-B518-6722556C6995}.Release|Any CPU.ActiveCfg = Release|Any CPU
{24BE2F5A-2B03-4493-B518-6722556C6995}.Release|Any CPU.Build.0 = Release|Any CPU
{2F9B46FF-923A-47C4-AC52-6A621A8E0850}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2F9B46FF-923A-47C4-AC52-6A621A8E0850}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2F9B46FF-923A-47C4-AC52-6A621A8E0850}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2F9B46FF-923A-47C4-AC52-6A621A8E0850}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -79,6 +91,8 @@ Global
{3B3F2699-F720-4498-8044-262EFE110A22} = {D3201F71-A289-4136-AC7E-E5204ACB9183}
{AF9D60DF-EF22-40FD-9C56-7E7F2BCB9683} = {71C09624-020B-410E-A8FE-1FD216A1FE31}
{C65468A6-BDE2-4FD4-9400-B668B1395B26} = {D06BDAE9-BE83-448F-8AD4-3044BB187C11}
{24BE2F5A-2B03-4493-B518-6722556C6995} = {D06BDAE9-BE83-448F-8AD4-3044BB187C11}
{2F9B46FF-923A-47C4-AC52-6A621A8E0850} = {D06BDAE9-BE83-448F-8AD4-3044BB187C11}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {CB90618B-6B7B-4D09-9D35-467325708BAA}
Expand Down
43 changes: 42 additions & 1 deletion src/SendGrid/BaseClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using Newtonsoft.Json;
#if NETSTANDARD2_0
using System.Text.Json;
#else
using Newtonsoft.Json;
#endif
using SendGrid.Helpers.Errors;
using SendGrid.Helpers.Mail;
using SendGrid.Helpers.Reliability;
Expand Down Expand Up @@ -328,6 +332,42 @@ private Dictionary<string, List<object>> ParseJson(string json)
{
var dict = new Dictionary<string, List<object>>();

#if NETSTANDARD2_0
var reader = System.Text.Json.Nodes.JsonNode.Parse(json).AsObject();
foreach (var v in reader)
{
var propertyName = v.Key;
switch (v.Value.GetValueKind())
{
case JsonValueKind.Undefined:
case JsonValueKind.Object:
case JsonValueKind.Array:
case JsonValueKind.Null:
break;
case JsonValueKind.String:
dict[propertyName].Add((string)v.Value);
break;
case JsonValueKind.Number:
if (v.Value.ToString().Contains("."))
{
dict[propertyName].Add((double)v.Value);
}
else
{
dict[propertyName].Add((int)v.Value);
}
break;
case JsonValueKind.True:
dict[propertyName].Add(true);
break;
case JsonValueKind.False:
dict[propertyName].Add(false);
break;
default:
throw new ArgumentOutOfRangeException();
}
}
#else
using (var sr = new StringReader(json))
using (var reader = new JsonTextReader(sr))
{
Expand Down Expand Up @@ -360,6 +400,7 @@ private Dictionary<string, List<object>> ParseJson(string json)
}
}
}
#endif

return dict;
}
Expand Down
39 changes: 38 additions & 1 deletion src/SendGrid/Helpers/Errors/ErrorHandler.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
using Newtonsoft.Json.Linq;
#if NETSTANDARD2_0
using System.Text.Json;
using System.Text.Json.Nodes;
#else
using Newtonsoft.Json.Linq;
#endif
using SendGrid.Helpers.Errors.Model;
using System.Net.Http;
using System.Threading.Tasks;
using System;
using System.Linq;

namespace SendGrid.Helpers.Errors
{
Expand Down Expand Up @@ -103,6 +110,31 @@ private static async Task<string> GetErrorMessage(HttpResponseMessage message)
try
{
// Check for the presence of property called 'errors'
#if NETSTANDARD2_0
var jObject = JsonNode.Parse(responseContent);
var errorsArray = (JsonArray)jObject["errors"];
if (errorsArray != null && errorsArray.Count > 0) {
// Get the first error message
errorValue = (string)errorsArray[0]["message"];

// Check for the presence of property called 'field'
if (errorsArray[0]["field"] != null) {
fieldValue = (string)errorsArray[0]["field"];
}

// Check for the presence of property called 'help'
if (errorsArray[0]["help"] != null) {
helpValue = (string)errorsArray[0]["help"];
}
}
else {
// Check for the presence of property called 'error'
var errorProperty = jObject["error"];
if (errorProperty != null) {
errorValue = (string)errorProperty;
}
}
#else
var jObject = JObject.Parse(responseContent);
var errorsArray = (JArray)jObject["errors"];
if (errorsArray != null && errorsArray.Count > 0)
Expand Down Expand Up @@ -131,6 +163,7 @@ private static async Task<string> GetErrorMessage(HttpResponseMessage message)
errorValue = errorProperty.Value<string>();
}
}
#endif
}
catch
{
Expand All @@ -148,7 +181,11 @@ private static async Task<string> GetErrorMessage(HttpResponseMessage message)
HelpLink = helpValue
};

#if NETSTANDARD2_0
return JsonSerializer.Serialize(errorResponse);
#else
return Newtonsoft.Json.JsonConvert.SerializeObject(errorResponse);
#endif
}
}
}
16 changes: 16 additions & 0 deletions src/SendGrid/Helpers/Mail/Model/ASM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,44 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// </copyright>

#if NETSTANDARD2_0
using System.Text.Json.Serialization;
#else
using Newtonsoft.Json;
#endif
using System.Collections.Generic;

namespace SendGrid.Helpers.Mail
{
/// <summary>
/// An object allowing you to specify how to handle unsubscribes.
/// </summary>
#if NETSTANDARD2_0
// Globally defined by setting ReferenceHandler on the JsonSerializerOptions object
#else
[JsonObject(IsReference = false)]
#endif
public class ASM
{
/// <summary>
/// Gets or sets the unsubscribe group to associate with this email.
/// </summary>
#if NETSTANDARD2_0
[JsonPropertyName("group_id")]
#else
[JsonProperty(PropertyName = "group_id")]
#endif
public int GroupId { get; set; }

/// <summary>
/// Gets or sets an array containing the unsubscribe groups that you would like to be displayed on the unsubscribe preferences page.
/// https://sendgrid.com/docs/User_Guide/Suppressions/recipient_subscription_preferences.html.
/// </summary>
#if NETSTANDARD2_0
[JsonPropertyName("groups_to_display")]
#else
[JsonProperty(PropertyName = "groups_to_display", IsReference = false)]
#endif
public List<int> GroupsToDisplay { get; set; }
}
}
28 changes: 28 additions & 0 deletions src/SendGrid/Helpers/Mail/Model/Attachment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,72 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// </copyright>

#if NETSTANDARD2_0
using System.Text.Json.Serialization;
#else
using Newtonsoft.Json;
#endif

namespace SendGrid.Helpers.Mail
{
/// <summary>
/// Gets or sets an array of objects in which you can specify any attachments you want to include.
/// </summary>
#if NETSTANDARD2_0
// Globally defined by setting ReferenceHandler on the JsonSerializerOptions object
#else
[JsonObject(IsReference = false)]
#endif
public class Attachment
{
/// <summary>
/// Gets or sets the Base64 encoded content of the attachment.
/// </summary>
#if NETSTANDARD2_0
[JsonPropertyName("content")]
#else
[JsonProperty(PropertyName = "content")]
#endif
public string Content { get; set; }

/// <summary>
/// Gets or sets the mime type of the content you are attaching. For example, application/pdf or image/jpeg.
/// </summary>
#if NETSTANDARD2_0
[JsonPropertyName("type")]
#else
[JsonProperty(PropertyName = "type")]
#endif
public string Type { get; set; }

/// <summary>
/// Gets or sets the filename of the attachment.
/// </summary>
#if NETSTANDARD2_0
[JsonPropertyName("filename")]
#else
[JsonProperty(PropertyName = "filename")]
#endif
public string Filename { get; set; }

/// <summary>
/// Gets or sets the content-disposition of the attachment specifying how you would like the attachment to be displayed. For example, "inline" results in the attached file being displayed automatically within the message while "attachment" results in the attached file requiring some action to be taken before it is displayed (e.g. opening or downloading the file). Defaults to "attachment". Can be either "attachment" or "inline".
/// </summary>
#if NETSTANDARD2_0
[JsonPropertyName("disposition")]
#else
[JsonProperty(PropertyName = "disposition")]
#endif
public string Disposition { get; set; }

/// <summary>
/// Gets or sets a unique id that you specify for the attachment. This is used when the disposition is set to "inline" and the attachment is an image, allowing the file to be displayed within the body of your email. Ex: <img src="cid:ii_139db99fdb5c3704"></img>.
/// </summary>
#if NETSTANDARD2_0
[JsonPropertyName("content_id")]
#else
[JsonProperty(PropertyName = "content_id")]
#endif
public string ContentId { get; set; }
}
}
16 changes: 16 additions & 0 deletions src/SendGrid/Helpers/Mail/Model/BCCSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,42 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// </copyright>

#if NETSTANDARD2_0
using System.Text.Json.Serialization;
#else
using Newtonsoft.Json;
#endif

namespace SendGrid.Helpers.Mail
{
/// <summary>
/// Gets or sets the address specified in the mail_settings.bcc object will receive a blind carbon copy (BCC) of the very first personalization defined in the personalizations array.
/// </summary>
#if NETSTANDARD2_0
// Globally defined by setting ReferenceHandler on the JsonSerializerOptions object
#else
[JsonObject(IsReference = false)]
#endif
public class BCCSettings
{
/// <summary>
/// Gets or sets a value indicating whether this setting is enabled.
/// </summary>
#if NETSTANDARD2_0
[JsonPropertyName("enable")]
#else
[JsonProperty(PropertyName = "enable")]
#endif
public bool? Enable { get; set; }

/// <summary>
/// Gets or sets the email address that you would like to receive the BCC.
/// </summary>
#if NETSTANDARD2_0
[JsonPropertyName("email")]
#else
[JsonProperty(PropertyName = "email")]
#endif
public string Email { get; set; }
}
}
12 changes: 12 additions & 0 deletions src/SendGrid/Helpers/Mail/Model/BypassBounceManagement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,32 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// </copyright>

#if NETSTANDARD2_0
using System.Text.Json.Serialization;
#else
using Newtonsoft.Json;
#endif

namespace SendGrid.Helpers.Mail
{
/// <summary>
/// Allows you to bypass the bounce list to ensure that the email is delivered to recipients. Spam report and unsubscribe lists will still be checked; addresses on these other lists will not receive the message.
/// </summary>
#if NETSTANDARD2_0
// Globally defined by setting ReferenceHandler on the JsonSerializerOptions object
#else
[JsonObject(IsReference = false)]
#endif
public class BypassBounceManagement
{
/// <summary>
/// Gets or sets a value indicating whether this setting is enabled.
/// </summary>
#if NETSTANDARD2_0
[JsonPropertyName("enable")]
#else
[JsonProperty(PropertyName = "enable")]
#endif
public bool Enable { get; set; }
}
}
Loading

0 comments on commit 12d167b

Please sign in to comment.