1
+ using System ;
2
+ using System . Reflection ;
3
+ using Newtonsoft . Json ;
4
+ using Newtonsoft . Json . Linq ;
5
+ using OmniSharp . Extensions . JsonRpc . Server . Messages ;
6
+
7
+ namespace OmniSharp . Extensions . JsonRpc
8
+ {
9
+ public class RpcErrorConverter : JsonConverter
10
+ {
11
+ public override void WriteJson ( JsonWriter writer , object value , JsonSerializer serializer )
12
+ {
13
+ throw new NotImplementedException ( ) ;
14
+ }
15
+
16
+ public override object ReadJson ( JsonReader reader , Type objectType , object existingValue , JsonSerializer serializer )
17
+ {
18
+ var obj = JObject . Load ( reader ) ;
19
+
20
+ var messageDataType = objectType == typeof ( RpcError )
21
+ ? typeof ( object )
22
+ : objectType . GetTypeInfo ( ) . GetGenericArguments ( ) [ 0 ] ;
23
+
24
+ object requestId = null ;
25
+ if ( obj . TryGetValue ( "id" , out var id ) )
26
+ {
27
+ var idString = id . Type == JTokenType . String ? ( string ) id : null ;
28
+ var idLong = id . Type == JTokenType . Integer ? ( long ? ) id : null ;
29
+ requestId = idString ?? ( idLong . HasValue ? ( object ) idLong . Value : null ) ;
30
+ }
31
+
32
+ object data = null ;
33
+ if ( obj . TryGetValue ( "error" , out var dataToken ) )
34
+ {
35
+ var errorMessageType = typeof ( ErrorMessage < > ) . MakeGenericType ( messageDataType ) ;
36
+ data = dataToken . ToObject ( errorMessageType ) ;
37
+ }
38
+
39
+ return Activator . CreateInstance ( objectType , requestId , data , obj [ "protocolVersion" ] . ToString ( ) ) ;
40
+ }
41
+
42
+ public override bool CanConvert ( Type objectType )
43
+ {
44
+ return objectType == typeof ( RpcError ) ||
45
+ ( objectType . GetTypeInfo ( ) . IsGenericType && objectType . GetTypeInfo ( ) . GetGenericTypeDefinition ( ) == typeof ( RpcError < > ) ) ;
46
+ }
47
+
48
+ public override bool CanWrite { get ; } = false ;
49
+ public override bool CanRead { get ; } = true ;
50
+ }
51
+ }
0 commit comments