|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | +using Newtonsoft.Json; |
| 5 | + |
| 6 | +namespace OmniSharp.Extensions.LanguageServer.Protocol.Serialization.Converters |
| 7 | +{ |
| 8 | + class AbsoluteUriKeyConverter<TValue> : JsonConverter<Dictionary<Uri, TValue>> |
| 9 | + { |
| 10 | + public override Dictionary<Uri, TValue> ReadJson( |
| 11 | + JsonReader reader, |
| 12 | + Type objectType, |
| 13 | + Dictionary<Uri, TValue> existingValue, |
| 14 | + bool hasExistingValue, |
| 15 | + JsonSerializer serializer) |
| 16 | + { |
| 17 | + if (reader.TokenType != JsonToken.StartObject) |
| 18 | + { |
| 19 | + throw new JsonException(); |
| 20 | + } |
| 21 | + |
| 22 | + var dictionary = new Dictionary<Uri, TValue>(); |
| 23 | + |
| 24 | + while (reader.Read()) |
| 25 | + { |
| 26 | + if (reader.TokenType == JsonToken.EndObject) |
| 27 | + { |
| 28 | + return dictionary; |
| 29 | + } |
| 30 | + |
| 31 | + // Get the key. |
| 32 | + if (reader.TokenType != JsonToken.PropertyName) |
| 33 | + { |
| 34 | + throw new JsonSerializationException($"The token type must be a property name. Given {reader.TokenType.ToString()}"); |
| 35 | + } |
| 36 | + |
| 37 | + // Get the stringified Uri. |
| 38 | + var key = new Uri((string)reader.Value, UriKind.RelativeOrAbsolute); |
| 39 | + if (!key.IsAbsoluteUri) |
| 40 | + { |
| 41 | + throw new JsonSerializationException($"The Uri must be absolute. Given: {reader.Value}"); |
| 42 | + } |
| 43 | + |
| 44 | + // Get the value. |
| 45 | + reader.Read(); |
| 46 | + var value = serializer.Deserialize<TValue>(reader); |
| 47 | + |
| 48 | + // Add to dictionary. |
| 49 | + dictionary.Add(key, value); |
| 50 | + } |
| 51 | + |
| 52 | + throw new JsonException(); |
| 53 | + } |
| 54 | + |
| 55 | + public override void WriteJson( |
| 56 | + JsonWriter writer, |
| 57 | + Dictionary<Uri, TValue> value, |
| 58 | + JsonSerializer serializer) |
| 59 | + { |
| 60 | + writer.WriteStartObject(); |
| 61 | + |
| 62 | + foreach (var kvp in value) |
| 63 | + { |
| 64 | + var uri = kvp.Key; |
| 65 | + if (!uri.IsAbsoluteUri) |
| 66 | + { |
| 67 | + throw new JsonSerializationException("The URI value must be an absolute Uri. Relative URI instances are not allowed."); |
| 68 | + } |
| 69 | + |
| 70 | + if (uri.IsFile) |
| 71 | + { |
| 72 | + // First add the file scheme and :// |
| 73 | + var builder = new StringBuilder(uri.Scheme) |
| 74 | + .Append("://"); |
| 75 | + |
| 76 | + // UNC file paths use the Host |
| 77 | + if (uri.HostNameType != UriHostNameType.Basic) |
| 78 | + { |
| 79 | + builder.Append(uri.Host); |
| 80 | + } |
| 81 | + |
| 82 | + // Paths that start with a drive letter don't have a slash in the PathAndQuery |
| 83 | + // but they need it in the final result. |
| 84 | + if (uri.PathAndQuery[0] != '/') |
| 85 | + { |
| 86 | + builder.Append('/'); |
| 87 | + } |
| 88 | + |
| 89 | + // Lastly add the remaining parts of the URL |
| 90 | + builder.Append(uri.PathAndQuery); |
| 91 | + writer.WritePropertyName(builder.ToString()); |
| 92 | + } |
| 93 | + else |
| 94 | + { |
| 95 | + writer.WritePropertyName(uri.AbsoluteUri); |
| 96 | + } |
| 97 | + |
| 98 | + serializer.Serialize(writer, kvp.Value); |
| 99 | + } |
| 100 | + |
| 101 | + writer.WriteEndObject(); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments