toJson method

Map<String, dynamic> toJson({
  1. dynamic includeAttributes = true,
  2. dynamic includeRelationships = false,
})

Some api requests only want the type and id of an object. In those cases, set includeAttributes to false or call toIdResource instead.

Implementation

Map<String, dynamic> toJson({
  includeAttributes = true,
  includeRelationships = false,
}) {
  // // DateTime objects are not json serializable, so we need to convert them first
  // var attribs = <String, dynamic>{};
  // for (var a in _attributes.entries) {
  //   if (a.value is DateTime) {
  //     attribs[a.key] = (a.value as DateTime).toIso8601String();
  //   } else {
  //     attribs[a.key] = a.value;
  //   }
  // }
  return {
    'type': resourceType,
    if (id != null) 'id': id,
    if (includeAttributes) 'attributes': _attributes,
    if (includeRelationships)
      'relationships': {
        for (var r in _relationships.entries)
          r.key: {
            'data': r.value.length == 1
                ? r.value.first.toIdResource()
                : [for (var v in r.value) v.toIdResource()]
          }
      },
  };
}