Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Serializer

Serializer converts resource objects and collections into array representation.

Serializer is mainly used by REST controllers to convert different objects into array representation so that they can be further turned into different formats, such as JSON, XML, by response formatters.

The default implementation handles resources as Model objects and collections as objects implementing [[DataProviderInterface]]. You may override [[serialize()]] to handle more types.

author

Mahesh S Warrier https://github.com/codespede

Hierarchy

Index

Constructors

Properties

collectionEnvelope: any
var

string the name of the envelope (e.g. items) for returning the resource objects in a collection. This is used when serving a resource collection. When this is set and pagination is enabled, the serializer will return a collection in the following format:

{
'items': [...], // assuming collectionEnvelope is "items"
'_links': { // pagination links as returned by Pagination.getLinks()
'self': '...',
'next': '...',
'last': '...',
},
'_meta': { // meta information as returned by Pagination.toArray()
'totalCount': 100,
'pageCount': 5,
'currentPage': 1,
'perPage': 20,
},
}

If this property is not set, the resource arrays will be directly returned without using envelope. The pagination information as shown in _links and _meta can be accessed from the response HTTP headers.

currentPageHeader: string = 'X-Pagination-Current-Page'
var

string the name of the HTTP header containing the information about the current page number (1-based). This is used when serving a resource collection with pagination.

expandParam: string = 'expand'
var

string the name of the query parameter containing the information about which fields should be returned in addition to those listed in fieldsParam for a resource object.

fieldsParam: string = 'fields'
var

string the name of the query parameter containing the information about which fields should be returned for a Model object. If the parameter is not provided or empty, the default set of fields as defined by [[Model.fields()]] will be returned.

initialization: Promise<void>

A Promise which resolves when this Component has been successfully initialized.

linksEnvelope: string = '_links'
var

string the name of the envelope (e.g. _links) for returning the links objects. It takes effect only, if collectionEnvelope is set.

since

2.0.4

metaEnvelope: string = '_meta'
var

string the name of the envelope (e.g. _meta) for returning the pagination object. It takes effect only, if collectionEnvelope is set.

since

2.0.4

pageCountHeader: string = 'X-Pagination-Page-Count'
var

string the name of the HTTP header containing the information about total number of pages of data. This is used when serving a resource collection with pagination.

perPageHeader: string = 'X-Pagination-Per-Page'
var

string the name of the HTTP header containing the information about the number of data items in each page. This is used when serving a resource collection with pagination.

preserveKeys: boolean = false

Whether to preserve array keys when serializing collection data. Set this to true to allow serialization of a collection as a JSON object where array keys are used to index the model objects. The default is to serialize all collections as array, regardless of how the array is indexed.

see

serializeDataProvider

request: {}

The current request. If not set, Application.request will be used.

Type declaration

  • [key: string]: any
response: {}

The current response. If not set, Application.response will be used.

Type declaration

  • [key: string]: any
totalCountHeader: string = 'X-Pagination-Total-Count'
var

string the name of the HTTP header containing the information about total number of data items. This is used when serving a resource collection with pagination.

Methods

  • addPaginationHeaders(pagination: Pagination): void
  • getRequestedFields(): { expand: string[]; fields: string[] }
  • see

    [[Model.fields]]

    see

    [[Model.extraFields]]

    Returns { expand: string[]; fields: string[] }

    array the names of the requested fields. The first element is an array representing the list of default fields requested, while the second element is an array of the extra fields requested in addition to the default fields.

    • expand: string[]
    • fields: string[]
  • init(): Promise<void>
  • Serializes the given data into a format that can be easily turned into other formats. This method mainly converts the objects of recognized types into array representation. It will not do conversion for unknown object types or non-object data. The default implementation will handle Model, DataProvider and Arrays. You may override this method to support more object types.

    Parameters

    Returns Promise<{}>

    the converted data.

  • serializeDataProvider(dataProvider: DataProvider): Promise<{}>
  • Serializes a data provider.

    Parameters

    • dataProvider: DataProvider

      the DataProvider to be serialized.

    Returns Promise<{}>

    the array representation of the DataProvider.

  • serializeModelErrors(model: Model): {}
  • Serializes the validation errors in a model.

    Parameters

    Returns {}

    the array representation of the errors

    • [key: string]: any

Generated using TypeDoc