Skip to content

Add FormatJSON detection #4

Description

@andrew

On v0.2.0 and current main, valid JSON is reported as generic UTF-8 text:

result := magic.Detect([]byte(`{"schemaVersion": 2}`))
fmt.Println(result.Kind, result.Format, result.MIME, result.Encoding)
// text text text/plain utf-8

Add an exported FormatJSON value with the application/json MIME type. For complete input, Detect should require valid JSON according to RFC 8259, including arrays and scalar top-level values, and allow surrounding JSON whitespace. Invalid content and valid JSON followed by non-whitespace data should retain the existing text or unknown classification.

DetectPrefix needs to remain provisional because later bytes can complete or invalidate a JSON value. A valid-looking prefix should keep ReasonNeedMore, while truncated objects, arrays, strings, and numbers should not be treated as invalid complete documents.

Tests should cover objects, arrays, strings, numbers, true, false, null, surrounding whitespace, truncated input, invalid syntax, and trailing non-whitespace content. The existing allocation and binary-size checks should be updated if the detector changes them.

This would identify OCI manifests, indexes, and configuration blobs as JSON while leaving OCI-specific semantics to callers. It would also cover package metadata and other JSON files without filename rules.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions