JsonAccess Annotation
The @JsonAccess annotation defined at Apex class level controls whether instances of the class can be serialized or deserialized. If the annotation restricts the JSON or XML serialization and deserialization, a runtime JSONException exception is thrown.
The serializable and deserializable parameters of the @JsonAccess annotation enforce the contexts in which Apex allows serialization and deserialization. You can specify one or both parameters, but you can’t specify the annotation with no parameters. The valid values for the parameters to indicate whether serialization and deserialization are allowed:
never: never allowedsameNamespace: allowed only for Apex code in the same namespacesamePackage: allowed only for Apex code in the same package (impacts only second-generation packages)always: always allowed for any Apex code
This example code shows an Apex class marked with the @JsonAccess annotation.
- If an Apex class annotated with
JsonAccessis extended, the extended class doesn’t inherit this property. - If the
toStringmethod is applied on objects that mustn’t be serialized, private data can be exposed. You must override thetoStringmethod on objects whose data must be protected. For example, serializing an object stored as a key in a Map invokes thetoStringmethod. The generated map includes key (string) and value entries, thus exposing all the fields of the object.
In versions 48.0 and earlier, the default access for deserialization is always and the default access for serialization is sameNamespace to preserve the existing behavior. From version 49.0 onwards, the default access for both serialization and deserialization is sameNamespace.