-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathCommandOrCodeActionConverter.cs
42 lines (37 loc) · 1.38 KB
/
CommandOrCodeActionConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
namespace OmniSharp.Extensions.LanguageServer.Protocol.Serialization.Converters
{
public class CommandOrCodeActionConverter : JsonConverter<CommandOrCodeAction>
{
public override void WriteJson(JsonWriter writer, CommandOrCodeAction value, JsonSerializer serializer)
{
if (value.IsCodeAction)
{
serializer.Serialize(writer, value.CodeAction);
}
else if (value.IsCommand)
{
serializer.Serialize(writer, value.Command);
}
else
{
writer.WriteNull();
}
}
public override CommandOrCodeAction ReadJson(JsonReader reader, Type objectType, CommandOrCodeAction existingValue, bool hasExistingValue, JsonSerializer serializer)
{
var result = JObject.Load(reader);
// Commands have a name, CodeActions do not
var command = result["command"];
if (command?.Type == JTokenType.String)
{
return new CommandOrCodeAction(result.ToObject<Command>(serializer));
}
return new CommandOrCodeAction(result.ToObject<CodeAction>(serializer));
}
public override bool CanRead => true;
}
}