1
0

Updated to C# 12

This commit is contained in:
2024-01-14 13:10:33 +01:00
parent 9cd1344266
commit 27cd54fb30
51 changed files with 637 additions and 379 deletions

View File

@@ -13,18 +13,16 @@ namespace Newtonsoft.Json
/// <summary>
/// List of known types to use this converver.
/// </summary>
public static readonly Type[] KnownTypes = new[]
{
public static readonly Type[] KnownTypes =
[
typeof(byte[]),
typeof(List<byte>),
typeof(IEnumerable<byte>)
};
];
/// <inheritdoc/>
public override bool CanConvert(Type objectType)
{
return KnownTypes.Contains(objectType);
}
=> KnownTypes.Contains(objectType);
/// <inheritdoc/>
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)

View File

@@ -14,19 +14,17 @@ namespace Newtonsoft.Json
/// <summary>
/// List of known types to use this converver.
/// </summary>
public static readonly Type[] KnownTypes = new[]
{
public static readonly Type[] KnownTypes =
[
typeof(IPAddress),
typeof(IPAddress[]),
typeof(List<IPAddress>),
typeof(IEnumerable<IPAddress>)
};
];
/// <inheritdoc/>
public override bool CanConvert(Type objectType)
{
return KnownTypes.Contains(objectType);
}
=> KnownTypes.Contains(objectType);
/// <inheritdoc/>
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)

View File

@@ -2,7 +2,12 @@
using System.Collections.Generic;
using System.Linq;
using System.Net;
#if NET8_0_OR_GREATER
using IPNetwork = System.Net.IPNetwork;
#else
using Microsoft.AspNetCore.HttpOverrides;
using IPNetwork = Microsoft.AspNetCore.HttpOverrides.IPNetwork;
#endif
namespace Newtonsoft.Json
{
@@ -10,24 +15,22 @@ namespace Newtonsoft.Json
/// Converts an <see cref="IPNetwork"/> from and to JSON.
/// </summary>
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public class IPNetworkConverter : JsonConverter
public class IpNetworkConverter : JsonConverter
{
/// <summary>
/// List of known types to use this converver.
/// </summary>
public static readonly Type[] KnownTypes = new[]
{
public static readonly Type[] KnownTypes =
[
typeof(IPNetwork),
typeof(IPNetwork[]),
typeof(List<IPNetwork>),
typeof(IEnumerable<IPNetwork>)
};
];
/// <inheritdoc/>
public override bool CanConvert(Type objectType)
{
return KnownTypes.Contains(objectType);
}
=> KnownTypes.Contains(objectType);
/// <inheritdoc/>
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
@@ -40,7 +43,7 @@ namespace Newtonsoft.Json
if (typeof(IPNetwork) == objectType)
return Parse(str);
var networks = str.Split(';').Select(s => Parse(s));
var networks = str.Split(';').Select(Parse);
if (typeof(IPNetwork[]) == objectType)
return networks.ToArray();
@@ -65,23 +68,27 @@ namespace Newtonsoft.Json
str = ToString(net);
if (value is IPNetwork[] netArray)
str = string.Join(";", netArray.Select(n => ToString(n)));
str = string.Join(";", netArray.Select(ToString));
if (value is List<IPNetwork> netList)
str = string.Join(";", netList.Select(n => ToString(n)));
str = string.Join(";", netList.Select(ToString));
if (value is IEnumerable<IPNetwork> netEnum)
str = string.Join(";", netEnum.Select(n => ToString(n)));
str = string.Join(";", netEnum.Select(ToString));
writer.WriteValue(str);
}
private string ToString(IPNetwork net)
private static string ToString(IPNetwork net)
{
#if NET8_0_OR_GREATER
return $"{net.BaseAddress}/{net.PrefixLength}";
#else
return $"{net.Prefix}/{net.PrefixLength}";
#endif
}
private IPNetwork Parse(string str)
private static IPNetwork Parse(string str)
{
string[] parts = str.Split('/');
var prefix = IPAddress.Parse(parts.First());