canParse method

  1. @override
bool canParse(
  1. String content
)
override

Tests if the provided content is likely in this codec's format

This method is used for codec auto-detection when no explicit MIME type is available. It should perform quick heuristic checks to determine if the content appears to be in the format supported by this codec.

The method should balance accuracy with performance - it should not perform a full parse, but should do enough checking to make a reasonable determination.

The content parameter is the string content to check.

Returns true if the content appears to be in this codec's format.

Implementation

@override
bool canParse(String content) {
  // Simple heuristics for detecting JSON-LD format
  final trimmed = content.trim();

  // Must be valid JSON (starts with { or [)
  if (!(trimmed.startsWith('{') || trimmed.startsWith('['))) {
    return false;
  }

  // Must contain at least one of these JSON-LD keywords
  return trimmed.contains('"@context"') ||
      trimmed.contains('"@id"') ||
      trimmed.contains('"@type"') ||
      trimmed.contains('"@graph"');
}