Add JSON content detection - #5
Open
andrew wants to merge 3 commits into
Open
Conversation
There was a problem hiding this comment.
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/mimeJSONand integrates JSON parsing into the text detection flow. - Adds a dedicated JSON parser for complete vs incomplete vs invalid JSON handling (used by both
DetectandDetectPrefix). - 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 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
FormatJSONdetection for complete RFC 8259 JSON values, including top-level scalars and surrounding whitespace. Prefix detection reports valid and incomplete JSON provisionally withReasonNeedMore, while invalid JSON falls back to the existing classification.Closes #4.