Skip to main content
edited tags
Link
skaffman
  • 404.5k
  • 96
  • 825
  • 775
Source Link
Alt_Doru
  • 335
  • 4
  • 13

JSON.NET Deserialization in C# results in empty object

I am trying to populate a C# object (ImportedProductCodesContainer) with data using JSON.NET deserialization.

ImportedProductCodesContainer.cs:

using Newtonsoft.Json;

[JsonObject(MemberSerialization.OptOut)]
public class ImportedProductCodesContainer
{
    public ImportedProductCodesContainer()
    {
            
    }

    [JsonProperty]
    public ActionType Action { get; set; }

    [JsonProperty]
    public string ProductListRaw { get; set; }
    
    
    public enum ActionType {Append=1, Replace};
}

JSON string:

{"ImportedProductCodesContainer":{"ProductListRaw":"1 23","Action":"Append"}}

C# Code:

 var serializer = new JsonSerializer();
 var importedProductCodesContainer = 
     JsonConvert.DeserializeObject<ImportedProductCodesContainer>(argument);

The problem is that importedProductCodesContainer remains empty after running the code above (Action = 0, ProductListRaw = null). Can you please help me figure out what's wrong?