[SalesForce] JSON deserialize from another managed package

In Salesforce if you try and do this:

JSON.serialize(data);

where data is an instance of a global Apex class in another managed package it will fail.

This seems like a completely arbitrary and pointless limitation, since all the global fields can be accessed anyway. So why did Salesforce do this, and is there any alternative?

Best Answer

There is a @JsonAccess annotation. Check this:

https://releasenotes.docs.salesforce.com/en-us/summer20/release-notes/rn_apex_JsonAccessAnnotation.htm

This annotation has 2 parameters: serializable & deserializable with this 4 options:

  • never: never allowed
  • sameNamespace: allowed only for Apex code in the same namespace
  • samePackage: allowed only for Apex code in the same package (impacts only second-generation packages)
  • always: always allowed for any Apex code

Example:

@JsonAccess(serializable='never' deserializable='sameNamespace')

public class Foo {}

Related Topic