I have a class with a field called "productName". I would like to allow an alias ("name") for that field.
Now, I could do it by just adding another field and some logic, but perhaps there's a neater way?
The constructor looks like this at the moment:
[JsonConstructor]
public OsInfo(string name = null, string platform = null, string version = null) {
The goal is to accept both of these JSON chunks:
Using "name":
'os': {
'platform': 'Android-armv7-a',
'name': 'Android',
'version': 'Android 7.1.2'
}
Using "productname":
'os': {
'platform': 'Android-armv7-a',
'productname': 'Android',
'version': 'Android 7.1.2'
}
My current solution looks like this, but I was hoping to find a neater way:
[JsonConstructor]
public OsInfo(string name = null, string productname = null, string platform = null, string version = null)
{
if (name != null && productname != null)
{
throw new ArgumentException("Don't use both aliases for OS name");
}
this.Name = name != null ? name : productname;