-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
RazorDiagnosticJsonConverter.cs
73 lines (59 loc) · 3.11 KB
/
RazorDiagnosticJsonConverter.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Globalization;
using Microsoft.AspNetCore.Razor.Language;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Microsoft.CodeAnalysis.Razor.Serialization;
internal class RazorDiagnosticJsonConverter : JsonConverter
{
public static readonly RazorDiagnosticJsonConverter Instance = new RazorDiagnosticJsonConverter();
private const string RazorDiagnosticMessageKey = "Message";
public override bool CanConvert(Type objectType)
{
return typeof(RazorDiagnostic).IsAssignableFrom(objectType);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType != JsonToken.StartObject)
{
return null;
}
var diagnostic = JObject.Load(reader);
var id = diagnostic[nameof(RazorDiagnostic.Id)].Value<string>();
var severity = diagnostic[nameof(RazorDiagnostic.Severity)].Value<int>();
var message = diagnostic[RazorDiagnosticMessageKey].Value<string>();
var span = diagnostic[nameof(RazorDiagnostic.Span)].Value<JObject>();
var filePath = span[nameof(SourceSpan.FilePath)].Value<string>();
var absoluteIndex = span[nameof(SourceSpan.AbsoluteIndex)].Value<int>();
var lineIndex = span[nameof(SourceSpan.LineIndex)].Value<int>();
var characterIndex = span[nameof(SourceSpan.CharacterIndex)].Value<int>();
var length = span[nameof(SourceSpan.Length)].Value<int>();
var descriptor = new RazorDiagnosticDescriptor(id, message, (RazorDiagnosticSeverity)severity);
var sourceSpan = new SourceSpan(filePath, absoluteIndex, lineIndex, characterIndex, length);
return RazorDiagnostic.Create(descriptor, sourceSpan);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var diagnostic = (RazorDiagnostic)value;
writer.WriteStartObject();
WriteProperty(writer, nameof(RazorDiagnostic.Id), diagnostic.Id);
WriteProperty(writer, nameof(RazorDiagnostic.Severity), (int)diagnostic.Severity);
WriteProperty(writer, RazorDiagnosticMessageKey, diagnostic.GetMessage(CultureInfo.CurrentCulture));
writer.WritePropertyName(nameof(RazorDiagnostic.Span));
writer.WriteStartObject();
WriteProperty(writer, nameof(SourceSpan.FilePath), diagnostic.Span.FilePath);
WriteProperty(writer, nameof(SourceSpan.AbsoluteIndex), diagnostic.Span.AbsoluteIndex);
WriteProperty(writer, nameof(SourceSpan.LineIndex), diagnostic.Span.LineIndex);
WriteProperty(writer, nameof(SourceSpan.CharacterIndex), diagnostic.Span.CharacterIndex);
WriteProperty(writer, nameof(SourceSpan.Length), diagnostic.Span.Length);
writer.WriteEndObject();
writer.WriteEndObject();
}
private void WriteProperty<T>(JsonWriter writer, string key, T value)
{
writer.WritePropertyName(key);
writer.WriteValue(value);
}
}