.NET NewtonSoft JSON deserialize map to a different property name

后端 未结 5 665
情歌与酒
情歌与酒 2020-11-22 09:20

I have following JSON string which is received from an external party.

{
   \"team\":[
      {
         \"v1\":\"\",
         \"attributes\":{
            \"         


        
5条回答
  •  抹茶落季
    2020-11-22 09:42

    Adding to Jacks solution. I need to Deserialize using the JsonProperty and Serialize while ignoring the JsonProperty (or vice versa). ReflectionHelper and Attribute Helper are just helper classes that get a list of properties or attributes for a property. I can include if anyone actually cares. Using the example below you can serialize the viewmodel and get "Amount" even though the JsonProperty is "RecurringPrice".

        /// 
        /// Ignore the Json Property attribute. This is usefule when you want to serialize or deserialize differently and not 
        /// let the JsonProperty control everything.
        /// 
        /// 
        public class IgnoreJsonPropertyResolver : DefaultContractResolver
        {
            private Dictionary PropertyMappings { get; set; }
    
            public IgnoreJsonPropertyResolver()
            {
                this.PropertyMappings = new Dictionary();
                var properties = ReflectionHelper.GetGetProperties(false)();
                foreach (var propertyInfo in properties)
                {
                    var jsonProperty = AttributeHelper.GetAttribute(propertyInfo);
                    if (jsonProperty != null)
                    {
                        PropertyMappings.Add(jsonProperty.PropertyName, propertyInfo.Name);
                    }
                }
            }
    
            protected override string ResolvePropertyName(string propertyName)
            {
                string resolvedName = null;
                var resolved = this.PropertyMappings.TryGetValue(propertyName, out resolvedName);
                return (resolved) ? resolvedName : base.ResolvePropertyName(propertyName);
            }
        }
    

    Usage:

            var settings = new JsonSerializerSettings();
            settings.DateFormatString = "YYYY-MM-DD";
            settings.ContractResolver = new IgnoreJsonPropertyResolver();
            var model = new PlanViewModel() {Amount = 100};
            var strModel = JsonConvert.SerializeObject(model,settings);
    

    Model:

    public class PlanViewModel
    {
    
        /// 
        ///     The customer is charged an amount over an interval for the subscription.
        /// 
        [JsonProperty(PropertyName = "RecurringPrice")]
        public double Amount { get; set; }
    
        /// 
        ///     Indicates the number of intervals between each billing. If interval=2, the customer would be billed every two
        ///     months or years depending on the value for interval_unit.
        /// 
        public int Interval { get; set; } = 1;
    
        /// 
        ///     Number of free trial days that can be granted when a customer is subscribed to this plan.
        /// 
        public int TrialPeriod { get; set; } = 30;
    
        /// 
        /// This indicates a one-time fee charged upfront while creating a subscription for this plan.
        /// 
        [JsonProperty(PropertyName = "SetupFee")]
        public double SetupAmount { get; set; } = 0;
    
    
        /// 
        /// String representing the type id, usually a lookup value, for the record.
        /// 
        [JsonProperty(PropertyName = "TypeId")]
        public string Type { get; set; }
    
        /// 
        /// Billing Frequency
        /// 
        [JsonProperty(PropertyName = "BillingFrequency")]
        public string Period { get; set; }
    
    
        /// 
        /// String representing the type id, usually a lookup value, for the record.
        /// 
        [JsonProperty(PropertyName = "PlanUseType")]
        public string Purpose { get; set; }
    }
    

提交回复
热议问题