2

I have the following JSON data

{
    "objectName": "CustomObject__c",
    "fields": [
        {
            "name": "Field1__c",
            "extName": "ABC",
            "master": "System1"
        },
        {
            "name": "Field2__c",
            "extName": "DEF",
            "master": "System2"
        }
    ]
}

which I wanted to deserialize and added some more information in the target Apex object via its constructor:

public class JsonTest {

    public static void test() {
        String jsonData = '....';
        Schema s = (Schema)JSON.deserialize(jsonData, Schema.class);
        System.debug(s); // custom = null
    }

    public class Schema {
        String objectName;
        List<FieldItem> fields;
        String custom;
            
        public Schema() {
            custom = 'test';
        }
    }

    public class FieldItem {
        String name;
        String extName;
        String master;
    }
}

When I run my code JsonTest.test() via anonymous apex, the custom property is still null (although it has been set via the class constructor). Why is it so? Thanks.

3
  • This is by design, when deserializing a JSON into a class, the constructor is not executed. You'll probably have to move the logic to a separate method and call that method after deserializing.
    – rael_kid
    Sep 27 at 9:28
  • 1
    It will remain null as you are attempting to deserialise JSON into the inner object that is either an empty object or an object without a value for the custom key. If you call new Schema() (suggest a name change away from platform Schema) you will see custom=test. If you construct valid JSON to support the inner Schema object properties you will see the correct value also i.e String jsonData = '{"custom":"testing"}'; See JSON.deserialize won't call default constructor for more.
    – TSmith
    Sep 27 at 9:30
  • Oh, thanks for the link @TSmith, not sure why I couldn't find it before posted this question. I think your comment should be marked as the answer. Thanks for your help. Sep 27 at 9:37
4

It will remain null as you are attempting to deserialise JSON into the inner object that is either an empty object or an object without a value for the custom key.

If you call new Schema() (suggest a name change away from platform Schema) you will see custom=test. If you construct valid JSON to support the inner Schema object properties you will see the correct value also i.e String jsonData = '{"custom":"testing"}';

See JSON.deserialize won't call default constructor for more.

1
  • Not sure about Apex but in C#, deserialization is essentially the object materializing into existence. Constructors are a set of instructions on how to build a new instance of an object, but when you deserialize, you don't need those instructions because you already know exactly what values it's going to have for everything. This means the memory can simply be allocated directly. So even though the language is different, I suspect this is what's happening under the hood.
    – aleppke
    Sep 27 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.