Source code for invenio_records_rest.serializers.json

# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2016-2018 CERN.
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.

"""Marshmallow based JSON serializer for records."""

from flask import json, request

from .base import PreprocessorMixin, SerializerMixinInterface
from .marshmallow import MarshmallowMixin


[docs]class JSONSerializerMixin(SerializerMixinInterface): """Mixin serializing records as JSON.""" @staticmethod def _format_args(): """Get JSON dump indentation and separates.""" if request and request.args.get("prettyprint"): return dict( indent=2, separators=(", ", ": "), ) else: return dict( indent=None, separators=(",", ":"), )
[docs] def serialize(self, pid, record, links_factory=None, **kwargs): """Serialize a single record and persistent identifier. :param pid: Persistent identifier instance. :param record: Record instance. :param links_factory: Factory function for record links. """ return json.dumps( self.transform_record(pid, record, links_factory, **kwargs), **self._format_args() )
[docs]class JSONSerializer(JSONSerializerMixin, MarshmallowMixin, PreprocessorMixin): """Marshmallow based JSON serializer for records."""