Skip to content

Commit 11aff78

Browse files
committed
Adding detection of JSON.Net version for line info handling
1 parent 2442a3e commit 11aff78

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44
namespace Microsoft.VisualStudio.Jdt
55
{
6+
using System;
7+
using System.Reflection;
8+
using Newtonsoft.Json.Linq;
9+
610
/// <summary>
711
/// Utilities class for handling JSON files
812
/// </summary>
@@ -13,6 +17,8 @@ public static class JdtUtilities
1317
/// </summary>
1418
internal const string JdtSyntaxPrefix = "@jdt.";
1519

20+
private static LineInfoHandling? lineInfoHandling = null;
21+
1622
/// <summary>
1723
/// Wheter the given key corresponds to a JDT verb
1824
/// </summary>
@@ -36,5 +42,32 @@ public static string GetJdtSyntax(string key)
3642
// If it is a JDT verb, remove the prefix
3743
return IsJdtSyntax(key) ? key.Substring(JdtSyntaxPrefix.Length) : null;
3844
}
45+
46+
/// <summary>
47+
/// Gets the <see cref="LineInfoHandling"/> depending on the Newtonsoft version
48+
/// This is due to a bug in previous versions of JSON.Net that loaded line info on ignore and vice-versa
49+
/// See https://github.com/JamesNK/Newtonsoft.Json/pull/1250
50+
/// </summary>
51+
/// <returns>The correct line info handling</returns>
52+
internal static LineInfoHandling GetLineInfoHandling()
53+
{
54+
if (lineInfoHandling == null)
55+
{
56+
Version newtonsoftVersion = typeof(JObject).GetTypeInfo().Assembly.GetName().Version;
57+
58+
if (newtonsoftVersion >= new Version("10.0.2"))
59+
{
60+
return LineInfoHandling.Load;
61+
}
62+
else
63+
{
64+
return LineInfoHandling.Ignore;
65+
}
66+
}
67+
else
68+
{
69+
return lineInfoHandling.Value;
70+
}
71+
}
3972
}
4073
}

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))

0 commit comments

Comments
 (0)