-
Notifications
You must be signed in to change notification settings - Fork 105
/
AbsoluteUriConverter.cs
57 lines (51 loc) · 2.24 KB
/
AbsoluteUriConverter.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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// #see https://github.com/NuGet/NuGet.Server
using System;
using Newtonsoft.Json;
namespace OmniSharp.Extensions.LanguageServer.Protocol.Serialization.Converters
{
/// <summary>
/// This is necessary because Newtonsoft.Json creates <see cref="Uri"/> instances with
/// <see cref="UriKind.RelativeOrAbsolute"/> which treats UNC paths as relative. NuGet.Core uses
/// <see cref="UriKind.Absolute"/> which treats UNC paths as absolute. For more details, see:
/// https://github.com/JamesNK/Newtonsoft.Json/issues/2128
/// </summary>
class AbsoluteUriConverter : JsonConverter<Uri>
{
public override Uri ReadJson(JsonReader reader, Type objectType, Uri existingValue, bool hasExistingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
{
return null;
}
else if (reader.TokenType == JsonToken.String)
{
var uri = new Uri((string)reader.Value, UriKind.RelativeOrAbsolute);
if (!uri.IsAbsoluteUri)
{
throw new JsonSerializationException($"The Uri must be absolute. Given: {reader.Value}");
}
return uri;
}
throw new JsonSerializationException("The JSON value must be a string.");
}
public override void WriteJson(JsonWriter writer, Uri value, JsonSerializer serializer)
{
if (value == null)
{
writer.WriteNull();
return;
}
if (!(value is Uri uriValue))
{
throw new JsonSerializationException("The value must be a URI.");
}
if (!uriValue.IsAbsoluteUri)
{
throw new JsonSerializationException("The URI value must be an absolute Uri. Relative URI instances are not allowed.");
}
writer.WriteValue(uriValue.ToString());
}
}
}