2025-09-27 02:22:15 -05:00
|
|
|
using Newtonsoft.Json;
|
2025-09-27 03:15:10 -05:00
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
using System;
|
2025-11-27 23:41:04 -06:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2025-09-27 02:22:15 -05:00
|
|
|
|
|
|
|
|
namespace Microsoft.Iris.Debug.Symbols;
|
|
|
|
|
|
|
|
|
|
public static class DebugSymbolsJsonParser
|
|
|
|
|
{
|
2025-11-27 23:41:04 -06:00
|
|
|
private static readonly JsonSerializerSettings _settings = new()
|
2025-09-27 03:15:10 -05:00
|
|
|
{
|
|
|
|
|
Converters = [new SourceSpanJsonConverter()],
|
|
|
|
|
};
|
2025-09-27 02:22:15 -05:00
|
|
|
|
2025-09-27 03:15:10 -05:00
|
|
|
public static ApplicationDebugSymbols ParseForApplication(string json) =>
|
|
|
|
|
JsonConvert.DeserializeObject<ApplicationDebugSymbols>(json, _settings);
|
2025-09-27 02:22:15 -05:00
|
|
|
|
2025-09-27 03:15:10 -05:00
|
|
|
public static FileDebugSymbols ParseForFile(string json) =>
|
|
|
|
|
JsonConvert.DeserializeObject<FileDebugSymbols>(json, _settings);
|
2025-09-27 02:22:15 -05:00
|
|
|
|
2025-09-27 03:15:10 -05:00
|
|
|
public static string Serialize(ApplicationDebugSymbols symbols) =>
|
|
|
|
|
JsonConvert.SerializeObject(symbols, _settings);
|
|
|
|
|
|
|
|
|
|
public static string Serialize(FileDebugSymbols symbols) =>
|
|
|
|
|
JsonConvert.SerializeObject(symbols, _settings);
|
|
|
|
|
|
|
|
|
|
private class SourceSpanJsonConverter : JsonConverter
|
|
|
|
|
{
|
2025-11-27 23:41:04 -06:00
|
|
|
public override bool CanConvert(Type objectType) => objectType == typeof(SourceSpan) || objectType == typeof(SourcePosition);
|
2025-09-27 03:15:10 -05:00
|
|
|
|
|
|
|
|
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
|
|
|
|
{
|
2025-11-27 23:41:04 -06:00
|
|
|
if (value is SourceSpan span)
|
|
|
|
|
{
|
|
|
|
|
JArray array = [$"{span.Start.Line}:{span.Start.Column}", $"{span.End.Line}:{span.End.Column}"];
|
|
|
|
|
array.WriteTo(writer);
|
|
|
|
|
}
|
|
|
|
|
else if (value is SourcePosition position)
|
|
|
|
|
{
|
|
|
|
|
writer.WriteValue($"{position.Line}:{position.Column}");
|
|
|
|
|
}
|
2025-09-27 03:15:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
|
|
|
|
{
|
2025-11-27 23:41:04 -06:00
|
|
|
if (objectType == typeof(SourcePosition))
|
|
|
|
|
{
|
|
|
|
|
if (reader.TokenType is not JsonToken.String)
|
|
|
|
|
throw new JsonReaderException($"Expected {nameof(SourcePosition)} encoded as string");
|
|
|
|
|
|
|
|
|
|
var positionString = reader.ReadAsString();
|
|
|
|
|
ParseSourcePosition(positionString);
|
|
|
|
|
}
|
|
|
|
|
else if (objectType == typeof(SourceSpan))
|
|
|
|
|
{
|
|
|
|
|
var rangeStrs = serializer.Deserialize<IEnumerable<string>>(reader);
|
|
|
|
|
var range = rangeStrs.Select(ParseSourcePosition).ToArray();
|
|
|
|
|
return new SourceSpan(range[0], range[1]);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-27 03:15:10 -05:00
|
|
|
throw new NotImplementedException();
|
2025-11-27 23:41:04 -06:00
|
|
|
|
|
|
|
|
static SourcePosition ParseSourcePosition(string positionString)
|
|
|
|
|
{
|
|
|
|
|
var delimiterIndex = positionString.IndexOf(':');
|
|
|
|
|
if (delimiterIndex <= 0)
|
|
|
|
|
throw new JsonReaderException($"Expected {nameof(SourcePosition)} formatted as line:column");
|
|
|
|
|
|
|
|
|
|
var line = int.Parse(positionString.Substring(0, delimiterIndex));
|
|
|
|
|
var column = int.Parse(positionString.Substring(delimiterIndex + 1));
|
|
|
|
|
return new SourcePosition(line, column);
|
|
|
|
|
}
|
2025-09-27 03:15:10 -05:00
|
|
|
}
|
|
|
|
|
}
|
2025-09-27 02:22:15 -05:00
|
|
|
}
|