-
Notifications
You must be signed in to change notification settings - Fork 734
System.Text.Json Migration - Adding code to parse the Project.Assets.Json file using STJ. #5558
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
Changes from 37 commits
a57aabf
05ed3db
c285f0f
c06e3f5
ef90a51
4c31cbf
b787826
c627b25
ef35381
084986d
5850754
014739f
d28a482
c9cb53f
d083e23
0b22126
e81974f
d2cc5d6
2de68c4
79e2b71
ccd6fe1
8d719c6
212b7bd
b5a82d7
82363ee
0cc4e51
c525dc8
4e5112a
2398980
1693913
e0ef66d
b4d0169
f060670
4602b44
a240062
0883567
c4adaf7
bda30bb
fb7ebd5
32aba9a
8853734
1b29455
1877448
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,13 +6,19 @@ | |
| using System.IO; | ||
| using Newtonsoft.Json; | ||
| using Newtonsoft.Json.Linq; | ||
| using NuGet.Common; | ||
| using NuGet.Packaging.Core; | ||
| using NuGet.Versioning; | ||
|
|
||
| namespace NuGet.ProjectModel | ||
| { | ||
| internal static class JsonUtility | ||
| { | ||
| private static readonly Dictionary<string, NuGetVersion> NuGetVersionCache = new(); | ||
| private static readonly Dictionary<string, VersionRange> VersionRangeCache = new(); | ||
|
|
||
| internal const string NUGET_EXPERIMENTAL_USE_NJ_FOR_FILE_PARSING = nameof(NUGET_EXPERIMENTAL_USE_NJ_FOR_FILE_PARSING); | ||
| internal static bool? UseNewtonsoftJson = null; | ||
| internal static readonly char[] PathSplitChars = new[] { LockFile.DirectorySeparatorChar }; | ||
|
|
||
| /// <summary> | ||
|
|
@@ -43,12 +49,35 @@ internal static JObject LoadJson(TextReader reader) | |
| } | ||
| } | ||
|
|
||
| internal static T LoadJson<T>(Stream stream, IUtf8JsonStreamReaderConverter<T> converter) | ||
| { | ||
| var streamingJsonReader = new Utf8JsonStreamReader(stream); | ||
| return converter.Read(ref streamingJsonReader); | ||
| } | ||
|
|
||
| internal static PackageDependency ReadPackageDependency(string property, JToken json) | ||
| { | ||
| var versionStr = json.Value<string>(); | ||
| return new PackageDependency( | ||
| property, | ||
| versionStr == null ? null : VersionRange.Parse(versionStr)); | ||
| versionStr == null ? null : JsonUtility.ParseVersionRange(versionStr)); | ||
| } | ||
|
|
||
| internal static bool UseNewstonSoftJsonForParsing(IEnvironmentVariableReader environmentVariableReader, bool bypassCache) | ||
jgonz120 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| if (!UseNewtonsoftJson.HasValue || bypassCache) | ||
| { | ||
| if (bool.TryParse(environmentVariableReader.GetEnvironmentVariable(NUGET_EXPERIMENTAL_USE_NJ_FOR_FILE_PARSING), out var useNj)) | ||
| { | ||
| UseNewtonsoftJson = useNj; | ||
| } | ||
| else | ||
| { | ||
| UseNewtonsoftJson = false; | ||
| } | ||
| } | ||
|
|
||
| return UseNewtonsoftJson.Value; | ||
| } | ||
|
|
||
| internal static JProperty WritePackageDependencyWithLegacyString(PackageDependency item) | ||
|
|
@@ -141,5 +170,36 @@ internal static JToken WriteString(string item) | |
| { | ||
| return item != null ? new JValue(item) : JValue.CreateNull(); | ||
| } | ||
|
|
||
| internal static NuGetVersion ParseNugetVersion(string value) | ||
|
||
| { | ||
| if (!NuGetVersionCache.ContainsKey(value)) | ||
| { | ||
| var result = NuGetVersion.Parse(value); | ||
| NuGetVersionCache[value] = result; | ||
| } | ||
| return NuGetVersionCache[value]; | ||
| } | ||
|
|
||
| internal static bool TryParseNugetVersion(string value, out NuGetVersion version) | ||
| { | ||
| if (!NuGetVersionCache.ContainsKey(value)) | ||
| { | ||
| _ = NuGetVersion.TryParse(value, out version); | ||
| NuGetVersionCache[value] = version; | ||
| } | ||
| version = NuGetVersionCache[value]; | ||
| return version is not null; | ||
| } | ||
|
|
||
| internal static VersionRange ParseVersionRange(string value) | ||
| { | ||
| if (!VersionRangeCache.ContainsKey(value)) | ||
| { | ||
| var result = VersionRange.Parse(value); | ||
| VersionRangeCache[value] = result; | ||
| } | ||
| return VersionRangeCache[value]; | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.