-
Notifications
You must be signed in to change notification settings - Fork 748
Expand file tree
/
Copy pathSafeBoolStjConverter.cs
More file actions
37 lines (34 loc) · 1.31 KB
/
SafeBoolStjConverter.cs
File metadata and controls
37 lines (34 loc) · 1.31 KB
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
// 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="SafeBoolConverter"/>.</remarks>
internal sealed class SafeBoolStjConverter : JsonConverter<bool>
{
public override bool Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
switch (reader.TokenType)
{
case JsonTokenType.True:
return true;
case JsonTokenType.False:
case JsonTokenType.Null:
return false;
case JsonTokenType.String:
return bool.TryParse(reader.GetString()?.Trim(), out bool flag) && flag;
case JsonTokenType.Number:
return reader.TryGetInt64(out long l) && l == 1;
default:
reader.Skip();
return false;
}
}
public override void Write(Utf8JsonWriter writer, bool value, JsonSerializerOptions options)
{
throw new NotSupportedException();
}
}
}