Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialize more types with ToString #502

Open
delatrie opened this issue May 13, 2024 · 2 comments
Open

Serialize more types with ToString #502

delatrie opened this issue May 13, 2024 · 2 comments
Labels
task:improvement Change that improves some user experience but can't be considered a new feature theme:core

Comments

@delatrie
Copy link
Contributor

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:

Those are obvious cases when ToString would be preferrable.

@delatrie delatrie added the task:improvement Change that improves some user experience but can't be considered a new feature label May 13, 2024
@TonEnfer
Copy link
Contributor

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

@driverog
Copy link

driverog commented Jun 6, 2024

It would be nice to have a way to add custom converters, just like we do with the TypeFormatter's

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
task:improvement Change that improves some user experience but can't be considered a new feature theme:core
Projects
None yet
Development

No branches or pull requests

3 participants