简体   繁体   中英

Newtonsoft change JSON property name dynamically with enum

How can I change a property name with Newtonsoft JSON.net I need that my JSON looks like this. The PaymentMethod has an inner property and this property changes whit an enum PaymentMethodEnum {Cash, Card, Debit, Check }

Cash case
"PaymentMethod": {
    "Cash": { }
}

Card case
"PaymentMethod": {
    "Card" : {
        "Authorization": "####",
        "Bin": "####",
        "Reference": "00#####"
    }
}

Debit case
"PaymentMethod": {
    "Debit": {
        "DocumentType": "DNI",
        "DocumentNumber": "##########3",
        "Account": "xxxx-####"
    }
}

Check case
"PaymentMethod": {
    "Check": {
        "BankCode": "MMMmmm",
        "Number": "xxxx-xxxx-####",
        "Account": "###########"
    }
}

This is my class

public class ChargeRq
{
    [Required]
    [JsonProperty(PropertyName = "DistributorId")]
    public int DistributorId { get; set; }

    [Required]
    [JsonProperty(PropertyName = "AgentId")]
    public int AgentId { get; set; }

    [Required]
    [JsonProperty(PropertyName = "Reference")]
    public string Reference { get; set; }

    [JsonProperty(PropertyName = "Invoices")]
    public IEnumerable<Invoice> Invoices { get; set; }

    [Required]
    [JsonProperty(PropertyName = "Total")]
    public int Total { get; set; }

    [Required]
    [JsonProperty(PropertyName = "PaymentMethod")]
    public Payment PaymentMethod { get; set; }
}
// --------------------
public class Payment
{
    // ???
}
// -------------------
public class Card
{
    [Required]
    [JsonProperty(PropertyName = "Authorization")]
    public string Authorization { get; set; }

    [Required]
    [JsonProperty(PropertyName = "Bin")]
    public string Bin { get; set; }

    [Required]
    [JsonProperty(PropertyName ="Reference")]
    public string Reference { get; set; }
}

public class Cash { }

public class Debit
{
    [Required]
    [JsonProperty(PropertyName = "DocumentType")]
    public string DocumentType { get; set; }

    [Required]
    [JsonProperty(PropertyName = "DocumentNumber")]
    public string DocumentNumber { get; set; }

    [Required]
    [JsonProperty(PropertyName = "Account")]
    public string Account { get; set; }
}

public class Check
{
    [Required]
    [JsonProperty(PropertyName = "BankCode")]
    public string BankCode { get; set; }

    [Required]
    [JsonProperty(PropertyName = "Number")]
    public string Number { get; set; }

    [Required]
    [JsonProperty(PropertyName = "Account")]
    public string Account { get; set; }
}

So when I get this JSON from a third-party service I need to validate that the JSON format content the payment methods with ChargeRq . And us it in my service like this

public HttpResponseMessage TestRequestFormatData([FromBody]ChargeRq request) { }

I'd recommend using a property for each PaymentMethod

public class Payment
{
    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public Card Card { get; set; }

    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public Debit Debit { get; set; }

    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public Check Check { get; set; }

    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public Cash Cash { get; set; }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM