-
Notifications
You must be signed in to change notification settings - Fork 748
Expand file tree
/
Copy pathSafeUriStjConverter.cs
More file actions
30 lines (26 loc) · 997 Bytes
/
SafeUriStjConverter.cs
File metadata and controls
30 lines (26 loc) · 997 Bytes
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
// 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.
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace NuGet.Protocol.Converters
{
/// <remarks>NSJ equivalent: <see cref="SafeUriConverter"/>.</remarks>
internal sealed class SafeUriStjConverter : JsonConverter<Uri>
{
public override Uri? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String)
{
Uri.TryCreate(reader.GetString()?.Trim(), UriKind.Absolute, out Uri? uri);
return uri;
}
reader.Skip();
return null;
}
public override void Write(Utf8JsonWriter writer, Uri value, JsonSerializerOptions options)
{
throw new NotSupportedException();
}
}
}