serialize method

  1. @override
String serialize(
  1. Object document, {
  2. bool withIncluded = false,
})
override

Implementation

@override
String serialize(Object document, {bool withIncluded = false}) {
  try {
    JsonApiDocument jsonApiDoc = (document as JsonApiDocument);
    Map<String, dynamic> jsonMap = {
      'data': {
        'id': jsonApiDoc.id,
        'type': jsonApiDoc.type,
        'attributes': jsonApiDoc.attributes,
        'relationships': jsonApiDoc.relationships,
      },
    };
    if (withIncluded) {
      jsonMap['included'] = jsonApiDoc.included;
    }
    return json.encode(jsonMap);
  } on TypeError {
    throw ArgumentError('document must be a JsonApiDocument');
  } on JsonUnsupportedObjectError {
    throw SerializationException();
  }
}