0

I have a class as follows.

[Datacontract]
public class Customer
{
    [DataMember(Name="p1")]
    public string Name { get; set; }

    [DataMember(Name = "p2")]
    public string SurName { get; set; }        
}

Case 1:

json:{"Name":"John","SurName":"Asdf"}

Deserialized object: customer.Name => null and customer.SurName => null

Case 2

Json: {"p1":"John","p2":"Asdf"}

Deserialized object: customer.Name => John and customer.SurName=> Asdf

Problem:

I have the json in Case1 => {"Name":"John","SurName":"Asdf"}

I wait the deserialized object in Case2 =>
customer.Name => John and customer.SurName=> Asdf

So, How I can provide during deserialization that ignore DataMember(Name) attribute?

Solved:

    protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
    {
        var property = base.CreateProperty(member, memberSerialization);
        property.PropertyName = member.Name;

        return property;
    }

Thank you @dbc

Qwer Sense
  • 487
  • 1
  • 5
  • 12
  • Looks like a duplicate of [Configure JSON.NET to ignore DataContract/DataMember attributes](https://stackoverflow.com/q/11055225/3744182). Does that answer your question sufficiently? – dbc Jul 11 '17 at 08:55
  • it is not exactly. I try to ignore DataMember attribute's Name field. [DataMember(Name="p1")] In here, Name="p1" is make deserialization wrong. – Qwer Sense Jul 11 '17 at 10:36
  • So you want to respect the `[DataContract]` and `[DataMember]` attributes *except that you want to ignore the `DataMember.Name` value*? (Oh, by the way, your `Customer` type must also be marked with `[DataContract]`; can you confirm?) – dbc Jul 11 '17 at 10:55
  • Yes. Because I dont have a chance to change Customer Class. Therefore I can not add JsonObject attribute to CustomerClass Properties. – Qwer Sense Jul 11 '17 at 11:01
  • Yes, Customer class marked with [Datacontract] – Qwer Sense Jul 11 '17 at 11:02
  • 1
    And ignoring data contract attributes completely, as shown [here](https://stackoverflow.com/q/11055225/3744182) isn't good enough? That should be sufficient for example class `Customer`. Do you have an example where this is not good enough? – dbc Jul 11 '17 at 11:08
  • yes, it is solved. it is my fault. thank you @dbc – Qwer Sense Jul 11 '17 at 12:20
  • Possible duplicate of [Configure JSON.NET to ignore DataContract/DataMember attributes](https://stackoverflow.com/questions/11055225/configure-json-net-to-ignore-datacontract-datamember-attributes) – dbc Jul 11 '17 at 18:34
  • Rather than adding the solution to the problem into the question itself, could you please [answer your own question](https://stackoverflow.com/help/self-answer) or [mark it as a duplicate](https://meta.stackexchange.com/questions/79916/is-it-possible-to-mark-my-own-question-as-duplicate-of-another) so others can tell at a glance that it has been answered? – dbc Jul 11 '17 at 19:27

2 Answers2

1

Can you deserialize it with below ? :

var customerModelData = JsonConvert.DeserializeObject<Customer>(jsonCustomerData);
Nikunj Patel
  • 249
  • 3
  • 13
  • Yes. But although Name or Surname property in json have a value, deserialized object's values is Null. Because properties have datamember attribute with Name field. Json deserializer wait p1 or p2 property. But my Json have Name and Surname property. – Qwer Sense Jul 11 '17 at 10:38
0

Define class property this way :

[JsonProperty(Required = Newtonsoft.Json.Required.AllowNull)]
public string Name  { get; set;}
Dilip Oganiya
  • 1,504
  • 12
  • 20