10
10
11
11
namespace LanguageServer . Infrastructure . JsonDotNet
12
12
{
13
+ /// <summary>
14
+ /// Converts an Either-derived types to and from JSON.
15
+ /// </summary>
13
16
public class EitherConverter : JsonConverter
14
17
{
15
18
private readonly Dictionary < Type , Func < JToken , object > > table ;
16
19
20
+ /// <summary>
21
+ /// Initializes a new instance of the EitherConverter class.
22
+ /// </summary>
17
23
public EitherConverter ( )
18
24
{
19
25
table = new Dictionary < Type , Func < JToken , object > > ( ) ;
20
26
table [ typeof ( NumberOrString ) ] = token => ( object ) ToNumberOrString ( token ) ;
21
27
table [ typeof ( LocationSingleOrArray ) ] = token => ( object ) ToLocationSingleOrArray ( token ) ;
22
28
table [ typeof ( TextDocumentSync ) ] = token => ( object ) ToTextDocumentSync ( token ) ;
29
+ table [ typeof ( Documentation ) ] = token => ( object ) ToDocumentation ( token ) ;
23
30
table [ typeof ( CompletionResult ) ] = token => ( object ) ToCompletionResult ( token ) ;
24
31
table [ typeof ( HoverContents ) ] = token => ( object ) ToHoverContents ( token ) ;
25
32
}
26
33
34
+ /// <summary>
35
+ /// Determines whether this instance can convert the specified object type.
36
+ /// </summary>
37
+ /// <param name="objectType"></param>
38
+ /// <returns></returns>
27
39
public override bool CanConvert ( Type objectType )
28
40
{
29
41
return typeof ( Either ) . GetTypeInfo ( ) . IsAssignableFrom ( objectType . GetTypeInfo ( ) ) ;
30
42
}
31
43
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>
32
52
public override object ReadJson ( JsonReader reader , Type objectType , object existingValue , JsonSerializer serializer )
33
53
{
34
54
var convert = table [ objectType ] ??
@@ -84,6 +104,21 @@ private TextDocumentSync ToTextDocumentSync(JToken token)
84
104
}
85
105
}
86
106
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
+
87
122
private CompletionResult ToCompletionResult ( JToken token )
88
123
{
89
124
switch ( token . Type )
@@ -108,9 +143,21 @@ private HoverContents ToHoverContents(JToken token)
108
143
case JTokenType . String :
109
144
return new HoverContents ( token . ToObject < string > ( ) ) ;
110
145
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
+ }
112
159
case JTokenType . Array :
113
- var array = ( JArray ) token ;
160
+ var array = ( JArray ) token ;
114
161
if ( array . Count == 0 )
115
162
{
116
163
return new HoverContents ( new string [ 0 ] ) ;
@@ -134,9 +181,15 @@ private HoverContents ToHoverContents(JToken token)
134
181
throw new JsonSerializationException ( ) ;
135
182
}
136
183
}
137
-
184
+
138
185
#endregion
139
186
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>
140
193
public override void WriteJson ( JsonWriter writer , object value , JsonSerializer serializer )
141
194
{
142
195
var either = ( Either ) value ;
0 commit comments