locked
Problems initialising variables through standard constructor (data model for JSON) RRS feed

  • Question

  • User690865889 posted

    Hi,

    I am currently trying to deserialise a JSON through the use of www.newtonsoft.com/json. Everyting works fine and I can initialise my data model ok through the use of Jsonmodel = JsonConvert.DeserializeObject<JsonModel>(json.ToString()); I can also access all elements as expected even in lists.

    However, my data model class constructors are never called when the new object is created. Through these I wanted to initialise some variables to var = null or even "null" in order to store them later on on in a DB. I would then basically hand over the "null" string in the SQL query instead of a specific value.

    namespace Project.Myjob
    {

    public class JsonModel
    {
            public JsonModel()
            {
                some_var = new Type_X(); //desperate attempt to get my variables filled with some default values. Not happening.
            }

        public Type_X typeX { get; set; }             
    }  

    public class Type_X
            {  
                public Type_X()
                {
                    var1 = "null";
                    var2 = "null";
                    var3 = "null";
                    var4 = "null";
                    var5 = "null";
                }
     
                public string var1 { get; set; } = "null";
                public string var2 { get; set; } = "null";
                public string var3 { get; set; } = "null";
                public string var4 { get; set; } = "null";
                public string var5 { get; set; } = "null";

    //Above 2 more attempts to generate some default values, but these are never called.

            }  

    None of this is taken into consideration at object creation and my Type_X.var1 etc. are always null and my program crashes with the exception: "System.NullReferenceException: Object reference not set to an instance of an object".

    Maybe I misunderstand how the deserialisation works, but I thought that it would create an object and then match the values from the json on the fields found in the model. But it seems that objects that do not exist in the JSON are not even being considered at all.

    My goal at this point is basically to just be able to fill the strings in my json string object with default values, which should be overwritten in case non-null values exist.

    Any ideas? Many thanks in advance!

    Thursday, December 21, 2017 6:50 AM

Answers

  • User690865889 posted

    I found my problem. I was mis-using the JSON deserialiser. When using NullValueHandling = NullValueHandling.Ignore and then initialising my values through the constructors it worked.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 22, 2017 6:34 AM

All replies

  • User690865889 posted

    I found my problem. I was mis-using the JSON deserialiser. When using NullValueHandling = NullValueHandling.Ignore and then initialising my values through the constructors it worked.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 22, 2017 6:34 AM
  • User283571144 posted

    Hi baoshenglishanghai,

    Maybe I misunderstand how the deserialisation works, but I thought that it would create an object and then match the values from the json on the fields found in the model. But it seems that objects that do not exist in the JSON are not even being considered at all.

    My goal at this point is basically to just be able to fill the strings in my json string object with default values, which should be overwritten in case non-null values exist.

    Do you mean you want to call the JsonModel's constructors  when using  JsonConvert.DeserializeObject method?

    I have also created a test demo on my side, it works well. It will call the constructors when using DeserializeObject. My Newtonsoft.Json version 10.0.3.

    Besides, if your codes still not work, you could consider using  [JsonConstructor] as below:

       public class JsonModel
        {
           [JsonConstructor]
            public JsonModel()
            {
                typeX = new Type_X(); //desperate attempt to get my variables filled with some default values. Not happening.
            }
    
            public Type_X typeX { get; set; }
        }
    
        public class Type_X
        {
            [JsonConstructor]
            public Type_X()
            {
                var1 = "null";
                var2 = "null";
                var3 = "null";
                var4 = "null";
                var5 = "null";
            }
    
            public string var1 { get; set; } = "null";
            public string var2 { get; set; } = "null";
            public string var3 { get; set; } = "null";
            public string var4 { get; set; } = "null";
            public string var5 { get; set; } = "null";
    
            //Above 2 more attempts to generate some default values, but these are never called.
    
        }

    Mt test codes:

                string json = "{\"typeX\":{\"var1\":\"aa\",\"var2\":\"bb\",\"var3\":\"aa\",\"var4\":\"bb\"}}";
                JsonModel k2 = JsonConvert.DeserializeObject<JsonModel>(json);
                int i = 0;
    

    Result:

    Best Regards,

    Brando

    Friday, December 22, 2017 6:39 AM