0

I have this problem when I want to deserialize a jsonstring to a model in c#. For some reason it doesn't serialize all the properties correctly.

Some properties suddenly become 0 when it is a plain text or a shortdatestring.

Below you'll find my model and some screenshots.

 public class AdvancedExportViewModel
{

    public string LastName { get; set; }
    public string FirstName { get; set; }
    public string Gender { get; set; }
    public string Email { get; set; }
    public string PhoneNumber { get; set; }
    public string IdentificationNumber { get; set; }
    public string BirthDate { get; set; }
    public string Address { get; set; }
    public string Zip { get; set; }
    public string City { get; set; }

    public string DiveLicense { get; set; }
    public string OtherLicenses { get; set; }
    public string FederationNumber { get; set; }

    public string MembershipDate { get; set; }
    public string MedicalDate { get; set; }
    public string MembershipType { get; set; }
    public List<MemberCustomFieldViewModel> MemberCustomFields { get; set; }
    public List<MembershipCustomFieldViewModel> MembershipCustomFields { get; set; }

}


public class MembershipCustomFieldViewModel
    {
        public int Id { get; set; }
        public int CustomFieldId { get; set; }
        public bool CustomFieldYearDependent { get; set; }
        public string CustomFieldName { get; set; }
        public CustomFieldsTypesDTO CustomFieldType { get; set; }
        public string AccountId { get; set; }
        public string Value { get; set; }
        public bool BoolValue
        {
            get { return Value == "1"; }
            set { Value = value ? "1" : "0"; }
        }
    }

The problem is situated in the "string Value" property of the CustomFieldViewModel.

This is my Json, and as you can see the Value property has has different values (string,shortdatetimestring and boolean as string) Json Here is my Deserialized Json on to my model As you can see, the values have become "0" deserialized model

How I deserialize my Json you'll find below: List<AdvancedExportViewModel> model = JsonConvert.DeserializeObject<List<AdvancedExportViewModel>>(exportModel);

The object (exportModel) given in the DeserializeObject method is a Json string offcourse. I hope this is all clear.

Nico
  • 3
  • 2

3 Answers3

0

Change the below code:

public bool BoolValue
{
    get { return Value == "1"; }
    set { Value = value ? "1" : "0"; }
}

to:

public bool BoolValue
{
    get;
    set;
}

and try again.

TobiasR.
  • 826
  • 9
  • 19
0
    public bool BoolValue
    {
        get { return Value == "1"; }
        set { Value = value ? "1" : "0"; }
    }

You are checking if Value equals your bool value which in your example is always false, where Value is either the string Test or a date. That statement will always be false, so it sets Value to 0. Why don't you make the BoolValue property an auto property like the rest of them...

public bool BoolValue { get; set; }

Also see this for more information as to why the result of

Value = value ? "1" : "0" 

Is returning "0"

Callback Kid
  • 708
  • 5
  • 22
  • This was the issue! I modified it a bit to this `public bool BoolValue { get { return Value == "1"; } set { if (CustomFieldType.Equals(CustomFieldsTypesDTO.checkbox)) { Value = value ? "1" : "0"; } else { Value = Value; } } }` and now i get the correct values! Thanks a lot! – Nico Jun 02 '17 at 07:09
0

The BoolValue property simply returns the value, it does not need to set.

Remove the property setter:

public bool BoolValue
{
    get { return Value == "1"; }
}

Also, I think its logic should change as follows:

public bool BoolValue
{
    get { return Value == "1" || Value == "0"; }
}
Alexander Petrov
  • 13,457
  • 2
  • 20
  • 49