-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathNullableDiagnosticCodeConverter.cs
40 lines (35 loc) · 1.26 KB
/
NullableDiagnosticCodeConverter.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
using System;
using Newtonsoft.Json;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
namespace OmniSharp.Extensions.LanguageServer.Protocol.Serialization.Converters
{
class NullableDiagnosticCodeConverter : JsonConverter<DiagnosticCode?>
{
public override void WriteJson(JsonWriter writer, DiagnosticCode? value, JsonSerializer serializer)
{
if (!value.HasValue)
{
writer.WriteNull();
}
else
{
if (value.Value.IsLong) serializer.Serialize(writer, value.Value.Long);
if (value.Value.IsString) serializer.Serialize(writer, value.Value.String);
}
}
public override DiagnosticCode? ReadJson(JsonReader reader, Type objectType, DiagnosticCode? existingValue,
bool hasExistingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.String)
{
return new DiagnosticCode((string) reader.Value);
}
if (reader.TokenType == JsonToken.Integer)
{
return new DiagnosticCode((long) reader.Value);
}
return null;
}
public override bool CanRead => true;
}
}