4

We're having an ecosystem of managed packages where one is dependend on another and sometimes there is a need that one package (e.g. extension package) need to serialize some objects from another namespace (e.g. base package).

The most common struggle is error as follows:

cannot serialize type from package namespace

Basically you can not call JSON.serialize on instance of a managed object outside of the managed pacakge (also package B can not serialize object from package A).

A specific problem that I have is that BASE package defines virtual class that is then extended by EXTENSION package. Objects that are instances of extension class will still contain some sub-properties that would be instances of subclasses from BASE namespace - and this result in failed attempt to JSON.serialize() such object.

So here is a structure that I'm working with:

BASE Package has some kind of virtual class - for example like this:

global virtual class CoreClass {

    global String name {get;set;}
    global InnerClassFromCore someProp {get;set;}

    global virtual void construct(String name, String subName, String subValue) {
        this.name = name;
        this.someProp = new InnerClassFromCore(subName, subValue);
    }

    global virtual class InnerClassFromCore {
        global String name {get;set;}
        global String value {get;set;}

        global InnerClassFromCore() {}     // dummy but required constructor

        global InnerClassFromCore(String name, String value) {
            this.name = name;
            this.value = value;
        }
    }    
}

Extension Package will have base package implementation:

global class CoreClassImpl extends masimtestcore.CoreClass {

    global String hereAreSomeExtensionProps {get;set;}
    ...

    global String serialize() {
        return JSON.serialize(this);
    }
}

In this case trying to execute following code:

CoreClassImpl cls = new CoreClassImpl(); 
cls.construct('name', 'subname', 'subvalue');
cls.serialize();

will result in:

Line: 3, Column: 1 System.JSONException: Cannot serialize type from package: masimtestcore.CoreClass.InnerClassFromCore

Question is how to overcome this?

  • Thanks for sharing your knowledge! I'm sure that this will help someone in the future. – sfdcfox Aug 1 '18 at 20:26
  • Very nice. Please remember to come back tomorrow and accept your own answer! – Adrian Larson Aug 2 '18 at 17:48
5

Extension Package should extend all sub-properties of a base class and before calling JSON.serialize we should convert instances of base subproperties into instances of extension sub-properties.

Example is presented in serializeWithConversion - this will be allowed replacement of pure JSON.serialize:

global class CoreClassImpl extends masimtestcore.CoreClass {

    global String someOtherProp {get;set;}

    global class InnerClassFromCoreImpl extends masimtestcore.CoreClass.InnerClassFromCore {
        global InnerClassFromCoreImpl(String name, String value) {
            this.name = name;
            this.value = value;
        }
    }

    global String serializeWithConversion() {
        masimtestcore.CoreClass.InnerClassFromCore toBeConverted = this.someProp;
        this.someProp = null;
        this.someProp = new InnerClassFromCoreImpl(toBeConverted.name, toBeConverted.value);
        return JSON.serialize(this);
    }
}

I hope this would save some time for the other people. We literally wasted gazillion of hours to understand how it works.

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.