contentToValue method

Future<DataSchemaValue<Object?>?> contentToValue(
  1. Content content,
  2. DataSchema? dataSchema
)

Converts a Content object to a typed Object.

A dataSchema can be passed for validating the result. If the media type specified in the content is not supported, the method will try to convert its body to an UTF-8 string.

Implementation

Future<DataSchemaValue<Object?>?> contentToValue(
  Content content,
  DataSchema? dataSchema,
) async {
  final parsedMediaType = MediaType.parse(content.type);
  final mimeType = parsedMediaType.mimeType;
  final parameters = parsedMediaType.parameters;

  final bytes = await content.toByteList();
  // TODO(JKRhb): Reevaluate usage of TextCodec here
  final codec = _getCodecFromMediaType(mimeType) ?? TextCodec();

  final value = codec.bytesToValue(bytes, dataSchema, parameters);

  if (value != null) {
    _validateValue(value, dataSchema);
  }
  return value;
}