Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions docs/source/usage/customization.md
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From reading this page, I have no clue where to place the XML or the Python code. Would someone please add that information?

Also are there any missing steps? Assume that the reader doesn't have decades of experience with Plone.

Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,89 @@ It is recommended to pass all values through `json_compatible` in order to valid

For customizing a specific field instance, a named `IFieldSerializer` adapter can be registered.
The name may either be the full dotted name of the field, such as `plone.app.dexterity.behaviors.exclfromnav.IExcludeFromNavigation.exclude_from_nav`, or the shortname of the field, such as `exclude_from_nav`.


### Dexterity content types

The API automatically provides a default serialization for all Dexterity content types.

To customize the serialization of a given content type, define a custom adapter as shown.

```{code-block} xml
:caption: `configure.zcml`
<adapter
factory=".serializers.MySerializer"
provides="plone.restapi.interfaces.ISerializeToJson"
for="my.package.interfaces.IMyContentType
zope.interface.Interface"
/>
```

```{code-block} python
:caption: `serializers.py`
from plone import api
from plone.restapi.serializer.dxcontent import SerializeToJson


class MySerializer(SerializeToJson):

def __call__(self, version=None, include_items=True, include_expansion=True):
# default provided by Plone
result = super().__call__(
version=version,
include_items=include_items,
include_expansion=include_expansion,
)
#
# here goes your custom logic
#
return result
```


```{warning}
If you modify the serialization of a content type, you might need to customize its deserialization as described in the next section.
```

#### Deserialization

The reverse of serialization is deserialization.

The API provides a default deserializer.
You can customize it with an adapter as shown.

```{code-block} xml
:caption: `configure.zcml`
<adapter
factory=".deserialiers.MyDeserializer"
provides="plone.restapi.interfaces.IDeserializeFromJson"
for="my.package.interfaces.IMyContentType
zope.interface.Interface"
/>
```

```{code-block} python
:caption: `deserializers.py`
from plone import api
from plone.restapi.deserializer.dxcontent import DeserializeFromJson


class MyDeserializer(DeserializeFromJson):

def __call__(
self, validate_all=False, data=None, create=False, mask_validation_errors=True
):
#
# your custom logic goes here, you might want to manipulate the `data` value
#

# after your custom logic, you might rely on the default provided by Plone
result = super().__call__(
validate_all=validate_all,
data=data,
create=create,
mask_validation_errors=mask_validation_errors,
)
return result
```

1 change: 1 addition & 0 deletions news/1975.documentation
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Explain how to customize serializers for content types @gforcada