1

I have a class with a public List property. This class is stored in a azure SQL DB using the entity framework - as such it cannot store the variable. To get around this issue i've created the backing field 'iUpdatedIdentifiers' which is a list seperated by '###', the code is as follows:

        [JsonProperty("UpdatedIdentifiers")]
        [System.ComponentModel.DataAnnotations.Schema.NotMapped]
        public List<string> UpdatedIdentifiers { 
            get{
                try{
                    return iUpdatedIdentifiers.Split(@"###").ToList();
                }catch{
                    return new List<string>();
                }
            }
            set{
                string s = "";
                foreach (string x in value)
                {
                    s = s + x + "###";
                }
                iUpdatedIdentifiers = s;
            }
         }

        [JsonIgnore]
        public string iUpdatedIdentifiers {get;set;}       

The code above works fine when retrieveing objects from the database using the entity framework and serializing the object to JSON. However when i attempt to deserialize the below JSON into the class the list is always empty.

{
   "UpdatedIdentifiers":[
      "edu308",
      "edu310",
      "edu312",
      ""
   ]
}

Any ideas on how to get around this issue?

zaza
  • 892
  • 1
  • 18
  • 37
  • Debug and Step through the code. if that property is coming back empty, look at the exception handler that returns the empty list. something about the setter may be causing an issue. – Nkosi May 23 '18 at 00:52
  • @Nkosi Stepping into the code it seems like the setter isnt even run.. "Newtonsoft.Json.Serialization.ExpressionValueProvider.SetValue" seems to run over the getter... – zaza May 23 '18 at 01:34
  • 1
    Looks like a duplicate of [Set accessor not being called when I deserialise object from Json.net](https://stackoverflow.com/q/33608226), so either 1) Change `UpdatedIdentifiers` to a `string []` array property, or 2) Mark it with `[JsonProperty(ObjectCreationHandling = ObjectCreationHandling.Replace)]`. Agree? – dbc May 23 '18 at 03:09
  • @dbc Thats fixed it! Thanks – zaza May 23 '18 at 03:18

0 Answers0