1010
1111namespace LanguageServer . Infrastructure . JsonDotNet
1212{
13+ /// <summary>
14+ /// Converts an Either-derived types to and from JSON.
15+ /// </summary>
1316 public class EitherConverter : JsonConverter
1417 {
1518 private readonly Dictionary < Type , Func < JToken , object > > table ;
1619
20+ /// <summary>
21+ /// Initializes a new instance of the EitherConverter class.
22+ /// </summary>
1723 public EitherConverter ( )
1824 {
1925 table = new Dictionary < Type , Func < JToken , object > > ( ) ;
2026 table [ typeof ( NumberOrString ) ] = token => ( object ) ToNumberOrString ( token ) ;
2127 table [ typeof ( LocationSingleOrArray ) ] = token => ( object ) ToLocationSingleOrArray ( token ) ;
2228 table [ typeof ( TextDocumentSync ) ] = token => ( object ) ToTextDocumentSync ( token ) ;
29+ table [ typeof ( Documentation ) ] = token => ( object ) ToDocumentation ( token ) ;
2330 table [ typeof ( CompletionResult ) ] = token => ( object ) ToCompletionResult ( token ) ;
2431 table [ typeof ( HoverContents ) ] = token => ( object ) ToHoverContents ( token ) ;
2532 }
2633
34+ /// <summary>
35+ /// Determines whether this instance can convert the specified object type.
36+ /// </summary>
37+ /// <param name="objectType"></param>
38+ /// <returns></returns>
2739 public override bool CanConvert ( Type objectType )
2840 {
2941 return typeof ( Either ) . GetTypeInfo ( ) . IsAssignableFrom ( objectType . GetTypeInfo ( ) ) ;
3042 }
3143
44+ /// <summary>
45+ /// Reads the JSON representation of the object.
46+ /// </summary>
47+ /// <param name="reader"></param>
48+ /// <param name="objectType"></param>
49+ /// <param name="existingValue"></param>
50+ /// <param name="serializer"></param>
51+ /// <returns></returns>
3252 public override object ReadJson ( JsonReader reader , Type objectType , object existingValue , JsonSerializer serializer )
3353 {
3454 var convert = table [ objectType ] ??
@@ -84,6 +104,21 @@ private TextDocumentSync ToTextDocumentSync(JToken token)
84104 }
85105 }
86106
107+ private Documentation ToDocumentation ( JToken token )
108+ {
109+ switch ( token . Type )
110+ {
111+ case JTokenType . Null :
112+ return null ;
113+ case JTokenType . String :
114+ return new Documentation ( token . ToObject < string > ( ) ) ;
115+ case JTokenType . Object :
116+ return new Documentation ( token . ToObject < MarkupContent > ( ) ) ;
117+ default :
118+ throw new JsonSerializationException ( ) ;
119+ }
120+ }
121+
87122 private CompletionResult ToCompletionResult ( JToken token )
88123 {
89124 switch ( token . Type )
@@ -108,9 +143,21 @@ private HoverContents ToHoverContents(JToken token)
108143 case JTokenType . String :
109144 return new HoverContents ( token . ToObject < string > ( ) ) ;
110145 case JTokenType . Object :
111- return new HoverContents ( token . ToObject < MarkedString > ( ) ) ;
146+ var obj = ( JObject ) token ;
147+ if ( obj . Property ( "kind" ) != null )
148+ {
149+ return new HoverContents ( obj . ToObject < MarkupContent > ( ) ) ;
150+ }
151+ else if ( obj . Property ( "language" ) != null )
152+ {
153+ return new HoverContents ( obj . ToObject < MarkedString > ( ) ) ;
154+ }
155+ else
156+ {
157+ throw new JsonSerializationException ( ) ;
158+ }
112159 case JTokenType . Array :
113- var array = ( JArray ) token ;
160+ var array = ( JArray ) token ;
114161 if ( array . Count == 0 )
115162 {
116163 return new HoverContents ( new string [ 0 ] ) ;
@@ -134,9 +181,15 @@ private HoverContents ToHoverContents(JToken token)
134181 throw new JsonSerializationException ( ) ;
135182 }
136183 }
137-
184+
138185 #endregion
139186
187+ /// <summary>
188+ /// Writes the JSON representation of the object.
189+ /// </summary>
190+ /// <param name="writer"></param>
191+ /// <param name="value"></param>
192+ /// <param name="serializer"></param>
140193 public override void WriteJson ( JsonWriter writer , object value , JsonSerializer serializer )
141194 {
142195 var either = ( Either ) value ;
0 commit comments