1
0

Added Converters/Formatters for Serialization of Json/MessagePack

This commit is contained in:
2022-08-04 22:34:14 +02:00
parent aeaeeb16c1
commit 1522c0cbc9
7 changed files with 340 additions and 1 deletions

View File

@@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace Newtonsoft.Json
{
/// <summary>
/// Converts a byte array from and to a hex string.
/// </summary>
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public class ByteArrayHexConverter : JsonConverter
{
/// <summary>
/// List of known types to use this converver.
/// </summary>
public static readonly Type[] KnownTypes = new[]
{
typeof(byte[]),
typeof(List<byte>),
typeof(IEnumerable<byte>)
};
/// <inheritdoc/>
public override bool CanConvert(Type objectType)
{
return KnownTypes.Contains(objectType);
}
/// <inheritdoc/>
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.Value == null)
return null;
string hex = (string)reader.Value;
var byteEnum = hex.HexToBytes();
if (typeof(byte[]) == objectType)
return byteEnum.ToArray();
if (typeof(List<byte>) == objectType)
return byteEnum.ToList();
return byteEnum;
}
/// <inheritdoc/>
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
string hex = null;
if (value == null)
{
writer.WriteValue(hex);
return;
}
if (value is byte[] byteArray)
hex = byteArray.BytesToHex();
if (value is List<byte> byteList)
hex = byteList.BytesToHex();
if (value is IEnumerable<byte> byteEnum)
hex = byteEnum.BytesToHex();
writer.WriteValue(hex);
}
}
}

View File

@@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
namespace Newtonsoft.Json
{
/// <summary>
/// Converts an <see cref="IPAddress"/> from and to JSON.
/// </summary>
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public class IPAddressConverter : JsonConverter
{
/// <summary>
/// List of known types to use this converver.
/// </summary>
public static readonly Type[] KnownTypes = new[]
{
typeof(IPAddress),
typeof(IPAddress[]),
typeof(List<IPAddress>),
typeof(IEnumerable<IPAddress>)
};
/// <inheritdoc/>
public override bool CanConvert(Type objectType)
{
return KnownTypes.Contains(objectType);
}
/// <inheritdoc/>
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.Value == null)
return null;
string str = (string)reader.Value;
if (typeof(IPAddress) == objectType)
return IPAddress.Parse(str);
var ips = str.Split(';').Select(s => IPAddress.Parse(s));
if (typeof(IPAddress[]) == objectType)
return ips.ToArray();
if (typeof(List<IPAddress>) == objectType)
return ips.ToList();
return ips;
}
/// <inheritdoc/>
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
string str = null;
if (value == null)
{
writer.WriteValue(str);
return;
}
if (value is IPAddress addr)
str = addr.ToString();
if (value is IPAddress[] addrArray)
str = string.Join(";", addrArray.Select(ip => ip.ToString()));
if (value is List<IPAddress> addrList)
str = string.Join(";", addrList.Select(ip => ip.ToString()));
if (value is List<IPAddress> addrEnum)
str = string.Join(";", addrEnum.Select(ip => ip.ToString()));
writer.WriteValue(str);
}
}
}