Skip to content

Replace Newtonsoft.Json with System.Text.Json on all TFMs #5057

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
31 changes: 17 additions & 14 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@
<PackageVersion Include="System.Xml.XDocument" Version="4.3.0" />
<PackageVersion Include="System.Xml.XmlDocument" Version="4.3.0" />
<PackageVersion Include="Xamarin.AndroidX.Browser" Version="1.4.0" PrivateAssets="All" />
<PackageVersion Include="System.Formats.Asn1" Version="9.0.0"/>
<PackageVersion Include="System.CodeDom" Version="6.0.0" />
<PackageVersion Include="System.IdentityModel.Tokens.Jwt" Version="6.35.0" />
<PackageVersion Include="System.Net.Http" Version="4.3.4" />
<PackageVersion Include="System.Reflection.TypeExtensions" Version="4.7.0" />
<PackageVersion Include="System.Security.Cryptography.Cng" Version="5.0.0" />
<PackageVersion Include="System.Text.Json" Version="6.0.11" />
<PackageVersion Include="System.Threading" Version="4.3.0" />
<PackageVersion Include="System.Threading.Tasks" Version="4.3.0" />
<PackageVersion Include="System.Threading.Tasks.Parallel" Version="4.3.0" />
<PackageVersion Include="System.Threading.Thread" Version="4.3.0" />
<PackageVersion Include="System.ValueTuple" Version="4.5.0" />
<PackageVersion Include="System.Windows.Forms" Version="4.0.0" />
</ItemGroup>
<ItemGroup>
<!-- Analyzers and dev packages -->
Expand All @@ -55,6 +68,10 @@
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageVersion Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageVersion Include="MSTest.TestFramework" Version="3.1.1" />
<!-- Since MSAL uses the minimum possible System.Text.Json version,
tests are unable to move away from Newtonsoft. For example DeepEquals method is only available in System.Text.Json 9
Newtonsoft reference is preferable to bumping the version System.Text.Json in tests,
to avoid changing the product behavior. -->
<PackageVersion Include="Newtonsoft.Json" Version="13.0.1" />
<PackageVersion Include="NSubstitute" Version="5.3.0" />
<PackageVersion Include="NSubstitute.Analyzers.CSharp" Version="1.0.17" />
Expand All @@ -67,20 +84,6 @@
<PackageVersion Include="Selenium.Support" Version="4.19.0" />
<PackageVersion Include="Selenium.WebDriver" Version="4.19.0" />
<PackageVersion Include="StrongNamer" Version="0.2.5" />
<PackageVersion Include="System.CodeDom" Version="6.0.0" />
<PackageVersion Include="System.IdentityModel.Tokens.Jwt" Version="6.35.0" />
<PackageVersion Include="System.Net.Http" Version="4.3.4" />
<PackageVersion Include="System.Reflection.TypeExtensions" Version="4.7.0" />
<PackageVersion Include="System.Security.Cryptography.Cng" Version="5.0.0" />
<PackageVersion Include="System.Text.Json" Version="6.0.10" />
<PackageVersion Include="System.Threading" Version="4.3.0" />
<PackageVersion Include="System.Threading.Tasks" Version="4.3.0" />
<PackageVersion Include="System.Threading.Tasks.Parallel" Version="4.3.0" />
<PackageVersion Include="System.Threading.Thread" Version="4.3.0" />
<PackageVersion Include="System.ValueTuple" Version="4.5.0" />
<PackageVersion Include="System.Windows.Forms" Version="4.0.0" />
<PackageVersion Include="CommandLineParser" Version="2.8.0" />
<PackageVersion Include="System.Formats.Asn1" Version="9.0.0"/>

</ItemGroup>
</Project>
8 changes: 4 additions & 4 deletions build/platform_and_feature_flags.props
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<Project>
<PropertyGroup Condition="'$(TargetFramework)' == '$(TargetFrameworkNetCore)' or '$(TargetFramework)' == '$(TargetFrameworkNet)'">
<DefineConstants>$(DefineConstants);SUPPORTS_SYSTEM_TEXT_JSON;NET_CORE;SUPPORTS_CONFIDENTIAL_CLIENT;SUPPORTS_CUSTOM_CACHE;SUPPORTS_BROKER;SUPPORTS_WIN32;</DefineConstants>
<PropertyGroup Condition="'$(TargetFramework)' == '$(TargetFrameworkNet)'">
<DefineConstants>$(DefineConstants)SUPPORTS_MTLS;NET_CORE;SUPPORTS_CONFIDENTIAL_CLIENT;SUPPORTS_CUSTOM_CACHE;SUPPORTS_BROKER;SUPPORTS_WIN32;</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == '$(TargetFrameworkNet)' or '$(TargetFramework)' == '$(TargetFrameworkNetDesktop462)' or '$(TargetFramework)' == '$(TargetFrameworkNetDesktop472)' or '$(TargetFramework)' == '$(TargetFrameworkNetStandard)'">
<DefineConstants>$(DefineConstants);SUPPORTS_OTEL;</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == '$(TargetFrameworkNet)' or '$(TargetFramework)' == '$(TargetFrameworkNetDesktop472)'">
<PropertyGroup Condition="'$(TargetFramework)' == '$(TargetFrameworkNetDesktop472)'">
<DefineConstants>$(DefineConstants);SUPPORTS_MTLS;</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == '$(TargetFrameworkNetAndroid)'">
Expand All @@ -15,7 +15,7 @@
<DefineConstants>$(DefineConstants);SUPPORTS_BROKER;SUPPORTS_CONFIDENTIAL_CLIENT;SUPPORTS_CUSTOM_CACHE;SUPPORTS_WIN32</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == '$(TargetFrameworkNetIos)'">
<DefineConstants>$(DefineConstants);iOS;SUPPORTS_BROKER</DefineConstants>
<DefineConstants>$(DefineConstants);iOS;SUPPORTS_BROKER</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == '$(TargetFrameworkNetStandard)'">
<DefineConstants>$(DefineConstants);NETSTANDARD;SUPPORTS_CONFIDENTIAL_CLIENT;SUPPORTS_BROKER;SUPPORTS_CUSTOM_CACHE;SUPPORTS_WIN32;</DefineConstants>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@
using Microsoft.Identity.Client.PlatformsCommon.Interfaces;
using Microsoft.Identity.Client.Utils;
using Microsoft.IdentityModel.Abstractions;
#if SUPPORTS_SYSTEM_TEXT_JSON
using System.Text.Json;
#else
using Microsoft.Identity.Json;
#endif

namespace Microsoft.Identity.Client
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@
using Microsoft.Identity.Client.Utils;
using Microsoft.IdentityModel.Abstractions;
using Microsoft.Identity.Client.Internal;
#if SUPPORTS_SYSTEM_TEXT_JSON
using System.Text.Json;
#else
using Microsoft.Identity.Json;
#endif

namespace Microsoft.Identity.Client
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@
using System.ComponentModel;
using System.Diagnostics;
using Microsoft.Identity.Client.Utils;
#if SUPPORTS_SYSTEM_TEXT_JSON
using JObject = System.Text.Json.Nodes.JsonObject;
#else
using Microsoft.Identity.Json.Linq;
#endif

namespace Microsoft.Identity.Client
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,8 @@
using Microsoft.Identity.Client.Internal;
using Microsoft.Identity.Client.OAuth2;
using Microsoft.Identity.Client.Utils;
#if SUPPORTS_SYSTEM_TEXT_JSON
using JObject = System.Text.Json.Nodes.JsonObject;
using JToken = System.Text.Json.Nodes.JsonNode;
#else
using Microsoft.Identity.Json;
using Microsoft.Identity.Json.Linq;
#endif

namespace Microsoft.Identity.Client.AuthScheme.PoP
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#if SUPPORTS_SYSTEM_TEXT_JSON
using Microsoft.Identity.Client.Platforms.net;
using Microsoft.Identity.Client.Platforms.Json;
using JsonProperty = System.Text.Json.Serialization.JsonIncludeAttribute;
#else
using Microsoft.Identity.Json;
#endif

namespace Microsoft.Identity.Client.Cache
{
/// <summary>
/// Contains the results of an ADAL token acquisition. Access Tokens from ADAL are not compatible
/// with MSAL, only Refresh Tokens are.
/// </summary>
[JsonObject]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These attributes are no longer needed.

[Preserve(AllMembers = true)]
internal sealed class AdalResult
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,20 @@
// Licensed under the MIT License.

using Microsoft.Identity.Client.Utils;
#if SUPPORTS_SYSTEM_TEXT_JSON
using Microsoft.Identity.Client.Platforms.net;
#else
using Microsoft.Identity.Json;
#endif
using Microsoft.Identity.Client.Platforms.Json;

namespace Microsoft.Identity.Client.Cache
{
[JsonObject]
[Preserve(AllMembers = true)]
internal class AdalResultWrapper
{

#if !SUPPORTS_SYSTEM_TEXT_JSON
[JsonProperty]
#endif
public AdalResult Result { get; set; }

#if !SUPPORTS_SYSTEM_TEXT_JSON
[JsonProperty]
#endif
public string RawClientInfo { get; set; }

/// <summary>
/// Gets the Refresh Token associated with the requested Access Token. Note: not all operations will return a Refresh Token.
/// </summary>
#if !SUPPORTS_SYSTEM_TEXT_JSON
[JsonProperty]
#endif
public string RefreshToken { get; set; }

/// <summary>
Expand All @@ -40,9 +25,6 @@ internal class AdalResultWrapper

// This is only needed for AcquireTokenByAuthorizationCode in which parameter resource is optional and we need
// to get it from the STS response.
#if !SUPPORTS_SYSTEM_TEXT_JSON
[JsonProperty]
#endif
internal string ResourceInResponse { get; set; }

/// <summary>
Expand All @@ -63,9 +45,6 @@ public string Serialize()
return JsonHelper.SerializeToJson(this);
}

#if !SUPPORTS_SYSTEM_TEXT_JSON
[JsonProperty]
#endif
public string UserAssertionHash { get; set; }

internal AdalResultWrapper Clone()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,14 @@
// Licensed under the MIT License.

using System;
#if SUPPORTS_SYSTEM_TEXT_JSON
using Microsoft.Identity.Client.Platforms.net;
using Microsoft.Identity.Client.Platforms.Json;
using JsonProperty = System.Text.Json.Serialization.JsonIncludeAttribute;
#else
using Microsoft.Identity.Json;
#endif

namespace Microsoft.Identity.Client.Cache
{
/// <summary>
/// Contains information of a single user. This information is used for token cache lookup. Also if created with userId, userId is sent to the service when login_hint is accepted.
/// </summary>
[JsonObject]
[Preserve(AllMembers = true)]
internal sealed class AdalUserInfo
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
// Licensed under the MIT License.

using System.Collections.Generic;
#if SUPPORTS_SYSTEM_TEXT_JSON
using JToken = System.Text.Json.Nodes.JsonNode;
#else
using Microsoft.Identity.Json.Linq;
#endif

namespace Microsoft.Identity.Client.Cache
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
#if SUPPORTS_SYSTEM_TEXT_JSON
using System.Text.Json;
using System.Text.Json.Nodes;
using JObject = System.Text.Json.Nodes.JsonObject;
using JToken = System.Text.Json.Nodes.JsonNode;
#else
using Microsoft.Identity.Json;
using Microsoft.Identity.Json.Linq;
#endif

namespace Microsoft.Identity.Client.Cache.Items
{
internal class CacheSerializationContract
{
#if SUPPORTS_SYSTEM_TEXT_JSON
private static readonly JsonSerializerOptions NeverIgnoreJsonOptions = new()
{
DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.Never
};
#endif

private static readonly IEnumerable<string> s_knownPropertyNames = new[] {
StorageJsonValues.CredentialTypeAccessToken,
Expand Down Expand Up @@ -56,14 +49,11 @@ public CacheSerializationContract(IDictionary<string, JToken> unknownNodes)

internal static CacheSerializationContract FromJsonString(string json)
{
#if SUPPORTS_SYSTEM_TEXT_JSON
var root = JsonNode.Parse(json, documentOptions: new JsonDocumentOptions
{
AllowTrailingCommas = true
}).AsObject();
#else
var root = JObject.Parse(json);
#endif

var unknownNodes = ExtractUnknownNodes(root);

var contract = new CacheSerializationContract(unknownNodes);
Expand Down Expand Up @@ -136,32 +126,19 @@ internal static CacheSerializationContract FromJsonString(string json)
return contract;

// private method for enumerating collection
#if SUPPORTS_SYSTEM_TEXT_JSON
static IEnumerable<JsonObject> GetElement(JsonObject root, string key)
{
foreach (var token in root[key].AsObject())
{
yield return token.Value as JObject;
}
}
#else
static IEnumerable<JObject> GetElement(JObject root, string key)
{
foreach (var token in root[key].Values())
{
yield return token as JObject;
}
}
#endif

}

private static IDictionary<string, JToken> ExtractUnknownNodes(JObject root)
{
#if SUPPORTS_SYSTEM_TEXT_JSON
return root
#else
return (root as IDictionary<string, JToken>)
#endif
.Where(kvp => !s_knownPropertyNames.Any(p => string.Equals(kvp.Key, p, StringComparison.OrdinalIgnoreCase)))
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
}
Expand Down Expand Up @@ -218,23 +195,10 @@ internal string ToJsonString()
// Anything else
foreach (var kvp in UnknownNodes)
{
#if SUPPORTS_SYSTEM_TEXT_JSON
root[kvp.Key] = kvp.Value != null ? JToken.Parse(kvp.Value.ToJsonString()) : null;
#else
root[kvp.Key] = kvp.Value;
#endif
}
#if SUPPORTS_SYSTEM_TEXT_JSON

return root.ToJsonString(NeverIgnoreJsonOptions);
#else
return JsonConvert.SerializeObject(
root,
Formatting.None,
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Include
});
#endif
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@
using Microsoft.Identity.Client.Internal;
using Microsoft.Identity.Client.OAuth2;
using Microsoft.Identity.Client.Utils;
#if SUPPORTS_SYSTEM_TEXT_JSON
using System.Text.Json;
using JObject = System.Text.Json.Nodes.JsonObject;
#else
using Microsoft.Identity.Json.Linq;
#endif

namespace Microsoft.Identity.Client.Cache.Items
{
Expand Down Expand Up @@ -57,11 +53,7 @@ internal MsalAccessTokenCacheItem(
#if !MOBILE
private IDictionary<string, string> AcquireCacheParametersFromResponse(
IEnumerable<string> persistedCacheParameters,
#if SUPPORTS_SYSTEM_TEXT_JSON
Dictionary<string, JsonElement> extraDataFromResponse)
#else
Dictionary<string, JToken> extraDataFromResponse)
#endif
{
if (persistedCacheParameters == null || !persistedCacheParameters.Any())
{
Expand All @@ -70,17 +62,11 @@ private IDictionary<string, string> AcquireCacheParametersFromResponse(

var cacheParameters = extraDataFromResponse?
.Where(x => persistedCacheParameters.Contains(x.Key, StringComparer.InvariantCultureIgnoreCase))
#if SUPPORTS_SYSTEM_TEXT_JSON
.ToDictionary(x => x.Key, x => x.Value.ToString());
#else
//Avoid formatting arrays because it adds new lines after every element
.ToDictionary(x => x.Key, x => x.Value.Type == JTokenType.Array || x.Value.Type == JTokenType.Object ?
x.Value.ToString(Json.Formatting.None) :
x.Value.ToString());
#endif
return cacheParameters;
}
#endif

internal /* for test */ MsalAccessTokenCacheItem(
string preferredCacheEnv,
string clientId,
Expand Down
Loading
Loading