33
44namespace 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}
0 commit comments