Skip to content

Commit 03e1ea9

Browse files
author
Jacob Viau
authored
Merge pull request #15 from Microsoft/line-info-detect
Adding detection of JSON.Net version for line info handling
2 parents 2442a3e + c2b4f88 commit 03e1ea9

File tree

7 files changed

+65
-35
lines changed

7 files changed

+65
-35
lines changed

src/Microsoft.VisualStudio.Jdt.Tests/Microsoft.VisualStudio.Jdt.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<ItemGroup>
1010
<PackageReference Include="MicroBuild.NonShipping" Version="2.0.40" />
1111
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0-preview-20170425-07" />
12-
<PackageReference Include="Newtonsoft.Json" Version="10.0.2" />
12+
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
1313
<PackageReference Include="xunit" Version="2.2.0" />
1414
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
1515
</ItemGroup>

src/Microsoft.VisualStudio.Jdt/JdtExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ internal static JObject CloneWithLineInfo(this JObject objectToClone)
5555
{
5656
var loadSettings = new JsonLoadSettings()
5757
{
58-
LineInfoHandling = LineInfoHandling.Load
58+
LineInfoHandling = JdtUtilities.GetLineInfoHandling()
5959
};
6060

6161
using (var objectReader = objectToClone.CreateReader())

src/Microsoft.VisualStudio.Jdt/JdtUtilities.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33

44
namespace Microsoft.VisualStudio.Jdt
55
{
6+
using System;
7+
using System.Diagnostics;
8+
using System.Reflection;
9+
using Newtonsoft.Json.Linq;
10+
611
/// <summary>
712
/// Utilities class for handling JSON files
813
/// </summary>
@@ -13,6 +18,12 @@ public static class JdtUtilities
1318
/// </summary>
1419
internal const string JdtSyntaxPrefix = "@jdt.";
1520

21+
/// <summary>
22+
/// The cached line info handling to use, based on Newtonsoft.Json version
23+
/// https://github.com/JamesNK/Newtonsoft.Json/issues/1249
24+
/// </summary>
25+
private static LineInfoHandling? lineInfoHandling = null;
26+
1627
/// <summary>
1728
/// Wheter the given key corresponds to a JDT verb
1829
/// </summary>
@@ -36,5 +47,43 @@ public static string GetJdtSyntax(string key)
3647
// If it is a JDT verb, remove the prefix
3748
return IsJdtSyntax(key) ? key.Substring(JdtSyntaxPrefix.Length) : null;
3849
}
50+
51+
/// <summary>
52+
/// Gets the <see cref="LineInfoHandling"/> depending on the Newtonsoft version
53+
/// This is due to a bug in previous versions of JSON.Net that loaded line info on ignore and vice-versa
54+
/// See https://github.com/JamesNK/Newtonsoft.Json/pull/1250
55+
/// </summary>
56+
/// <returns>The correct line info handling</returns>
57+
internal static LineInfoHandling GetLineInfoHandling()
58+
{
59+
if (lineInfoHandling == null)
60+
{
61+
try
62+
{
63+
string newtonsoftLocation = typeof(JObject).GetTypeInfo().Assembly.Location;
64+
FileVersionInfo fileVersion = FileVersionInfo.GetVersionInfo(newtonsoftLocation);
65+
66+
// The version the line ending bug was fixed in. We want to target a lower
67+
// version of Newtonsoft to give consumers more flexibility in what version
68+
// they consume theirselves. However, we still want line endings to work in
69+
// both new and old versions.
70+
if (new Version(fileVersion.ProductVersion) < new Version("10.0.2"))
71+
{
72+
lineInfoHandling = LineInfoHandling.Ignore;
73+
}
74+
else
75+
{
76+
lineInfoHandling = LineInfoHandling.Load;
77+
}
78+
}
79+
catch (Exception e) when (!e.IsCriticalException())
80+
{
81+
// we will default to the "correct" value in the instance of any issues.
82+
lineInfoHandling = LineInfoHandling.Load;
83+
}
84+
}
85+
86+
return lineInfoHandling.Value;
87+
}
3988
}
4089
}

src/Microsoft.VisualStudio.Jdt/JsonTransformation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ private void SetTransform(Stream transformStream)
162162
this.loadSettings = new JsonLoadSettings()
163163
{
164164
CommentHandling = CommentHandling.Ignore,
165-
LineInfoHandling = LineInfoHandling.Load
165+
LineInfoHandling = JdtUtilities.GetLineInfoHandling()
166166
};
167167

168168
using (StreamReader transformStreamReader = new StreamReader(transformStream))

src/Microsoft.VisualStudio.Jdt/Microsoft.VisualStudio.Jdt.csproj

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@
2121

2222
<ItemGroup>
2323
<PackageReference Include="MicroBuild.VisualStudio" Version="2.0.40" PrivateAssets="all" />
24-
<PackageReference Include="Newtonsoft.Json" Version="10.0.2" />
24+
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
25+
</ItemGroup>
26+
27+
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard1.5'">
28+
<PackageReference Include="System.Diagnostics.FileVersionInfo" Version="4.3.0" />
29+
<PackageReference Include="System.Runtime.Serialization.Formatters" Version="4.3.0" />
2530
</ItemGroup>
2631

2732
<ItemGroup>
@@ -39,4 +44,4 @@
3944
</EmbeddedResource>
4045
</ItemGroup>
4146

42-
</Project>
47+
</Project>

src/Microsoft.VisualStudio.Jdt/Processors/Attributes/JdtAttributeExtensions.cs

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,26 @@ namespace Microsoft.VisualStudio.Jdt
55
{
66
using System;
77
using System.Collections.Generic;
8-
using System.ComponentModel;
98
using System.Linq;
10-
using System.Reflection;
119

1210
/// <summary>
1311
/// Implements extensions for <see cref="JdtAttributes"/>
1412
/// </summary>
1513
internal static class JdtAttributeExtensions
1614
{
17-
/// <summary>
18-
/// Gets the description (name) of the attribute
19-
/// </summary>
20-
/// <param name="attribute">The attribute</param>
21-
/// <returns>The name of the attribute</returns>
22-
internal static string GetDescription(this JdtAttributes attribute)
23-
{
24-
var type = attribute.GetType();
25-
var typeInfo = type.GetTypeInfo();
26-
var name = Enum.GetName(type, attribute);
27-
var description = typeInfo.GetField(name)
28-
.GetCustomAttributes(false)
29-
.OfType<DescriptionAttribute>()
30-
.SingleOrDefault();
31-
32-
if (description == null)
33-
{
34-
throw new NotImplementedException(attribute.ToString() + " does not have a corresponding name");
35-
}
36-
37-
return description.Description;
38-
}
39-
4015
/// <summary>
4116
/// Get the full name of an attribute, with the JDT prefix
4217
/// </summary>
4318
/// <param name="attribute">The attribute</param>
4419
/// <returns>A string with the full name of the requested attribute</returns>
4520
internal static string FullName(this JdtAttributes attribute)
4621
{
47-
return JdtUtilities.JdtSyntaxPrefix + attribute.GetDescription();
22+
if (attribute == JdtAttributes.None)
23+
{
24+
return JdtUtilities.JdtSyntaxPrefix;
25+
}
26+
27+
return JdtUtilities.JdtSyntaxPrefix + Enum.GetName(typeof(JdtAttributes), attribute).ToLower();
4828
}
4929

5030
/// <summary>

src/Microsoft.VisualStudio.Jdt/Processors/Attributes/JdtAttributeValidator.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ namespace Microsoft.VisualStudio.Jdt
55
{
66
using System;
77
using System.Collections.Generic;
8-
using System.ComponentModel;
98
using System.Linq;
109
using Newtonsoft.Json.Linq;
1110

@@ -17,19 +16,16 @@ internal enum JdtAttributes
1716
/// <summary>
1817
/// Represents an non existant attribute
1918
/// </summary>
20-
[Description(null)]
2119
None = 0,
2220

2321
/// <summary>
2422
/// The JDT path attribute
2523
/// </summary>
26-
[Description("path")]
2724
Path,
2825

2926
/// <summary>
3027
/// The JDT path attribute
3128
/// </summary>
32-
[Description("value")]
3329
Value,
3430
}
3531

0 commit comments

Comments
 (0)