allure-csharp
allure-csharp copied to clipboard
Serialize more types with ToString
Since we use Newtonsoft.JSON with the default settings to serialize test or step parameters, some values are serialized in a less-than-optimal way:
- Enums are serialized as their underlying values instead of strings (see https://github.com/orgs/allure-framework/discussions/2560)
- Types from System.Reflection (PropertyInfo, etc) blows up
- Something else?...
Those are obvious cases when ToString would be preferrable.
For types associated with network addresses, Newtonsoft.JSON does not work well:
JsonSerializerSettings SerializerSettings = new()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
Error = (_, args) => { args.ErrorContext.Handled = true; },
Converters = {new Newtonsoft.Json.Converters.StringEnumConverter()}
};
var pa = new System.Net.NetworkInformation.PhysicalAddress(new byte[]{1,2,3,4,5,6});
var strValue = pa.ToString();
var jsonValue = JsonConvert.SerializeObject(pa, SerializerSettings);
Console.WriteLine($"String value is {strValue}");
Console.WriteLine($"JSON value is {jsonValue}");
/*
String value is 010203040506
JSON value is {}
*/
var ipa = new System.Net.IPAddress(new byte[]{1,1,1,1});
var strValue = ipa.ToString();
var jsonValue = JsonConvert.SerializeObject(ipa, SerializerSettings);
Console.WriteLine($"String value is {strValue}");
Console.WriteLine($"JSON value is {jsonValue}");
/*
String value is 1.1.1.1
JSON value is {"AddressFamily":"InterNetwork","IsIPv6Multicast":false,"IsIPv6LinkLocal":false,"IsIPv6SiteLocal":false,"IsIPv6Teredo":false,"IsIPv6UniqueLocal":false,"IsIPv4MappedToIPv6":false,"Address":16843009}
*/
P.S. For enum you can use the StringEnumConverter converter in the serialization options
It would be nice to have a way to add custom converters, just like we do with the TypeFormatter's