Skip to content

Add JSON content detection - #5

Open
andrew wants to merge 3 commits into
mainfrom
issue-4-json-detection
Open

Add JSON content detection#5
andrew wants to merge 3 commits into
mainfrom
issue-4-json-detection

Conversation

@andrew

@andrew andrew commented Aug 15, 2026

Copy link
Copy Markdown
Contributor

Adds FormatJSON detection for complete RFC 8259 JSON values, including top-level scalars and surrounding whitespace. Prefix detection reports valid and incomplete JSON provisionally with ReasonNeedMore, while invalid JSON falls back to the existing classification.

Closes #4.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds first-class JSON detection to the magic package, classifying complete RFC 8259 JSON values (including scalar top-level values and surrounding whitespace) as FormatJSON with application/json, and treating JSON-looking prefixes as provisional (ReasonNeedMore).

Changes:

  • Introduces FormatJSON / mimeJSON and integrates JSON parsing into the text detection flow.
  • Adds a dedicated JSON parser for complete vs incomplete vs invalid JSON handling (used by both Detect and DetectPrefix).
  • Expands test, fuzz, and benchmark coverage to include JSON inputs and invariants.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
README.md Documents JSON detection behavior and updates perf notes.
magic.go Adds FormatJSON / mimeJSON and hooks JSON detection into detect.
magic_test.go Extends allocation test inputs to include JSON.
json.go Implements JSON validation / prefix parsing logic.
json_test.go Adds unit tests for valid/invalid JSON, prefix behavior, and nesting depth.
fuzz_test.go Seeds JSON inputs and adds fuzz invariants against encoding/json validity.
benchmark_test.go Adds a JSON fixture into the benchmark corpus and helper generator.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread json.go Outdated
Comment on lines +50 to +66
switch parser.data[parser.offset] {
case '{':
if parser.depth == jsonMaximumDepth {
return jsonInvalid
}
parser.depth++
result := parser.parseObject()
parser.depth--
return result
case '[':
if parser.depth == jsonMaximumDepth {
return jsonInvalid
}
parser.depth++
result := parser.parseArray()
parser.depth--
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add FormatJSON detection

2 participants