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?