← Back to Pull requests

Improve error handling for malformed JSON syntax #13

Open
unknown opened this pull request with 329 lines diff across 4 files
View on GitHub
unknown unknown commented on Aug 04, 2025 03:22 UTC
Author

Fixes #12 by properly distinguishing between malformed JSON (invalid syntax) and partial JSON (incomplete but valid syntax).

Problem

The parser was silently swallowing invalid JSON syntax instead of throwing errors:

1. Invalid numbers starting with dots - .0516156161551515 would be silently ignored, causing the rest of valid JSON to be discarded
2. Invalid tokens in arrays - [abc would return [] instead of throwing an error
3. Empty arrays with spaces - [ ] would cause parsing to terminate early, discarding subsequent valid JSON

Example

Before this fix:

parse(`{
  "vector": [1, 2, 3, .0516156161551515, 7],
  "info": "valid data"
}`)
// Returns: { "vector": [1, 2, 3] }  // Silent truncation!

After this fix:

parse(`{
  "vector": [1, 2, 3, .0516156161551515, 7],  
  "info": "valid data"
}`)
// Throws: MalformedJSON: Unexpected token '.' at position 83

Solution

1. Enhanced token validation - Added proper validation before attempting to parse numbers, rejecting invalid tokens like .123 and abc

2. Improved array parsing - Fixed whitespace handling in arrays by adding skipBlank() calls at appropriate points

3. Better error propagation - Modified parseArr() and parseObj() to let MalformedJSON errors bubble up while still allowing PartialJSON errors to be handled gracefully for incomplete but valid JSON

Backward Compatibility

✅ All existing functionality is preserved:

  • Valid partial JSON parsing continues to work exactly as before

  • All existing tests pass without modification

  • The Allow flags continue to control partial parsing behavior as expected
  • This change only affects clearly invalid JSON syntax that should never have been accepted in the first place.

    <!-- START COPILOT CODING AGENT TIPS -->
    ---

    💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

    <!-- This is an auto-generated comment: release notes by coderabbit.ai -->

    Summary by CodeRabbit

    Bug Fixes
    Improved error handling for malformed JSON, ensuring invalid numbers and tokens are correctly identified and reported.
    Enhanced whitespace handling within arrays to ensure accurate parsing.

    Tests
    * Added comprehensive test cases to verify correct error reporting for invalid JSON inputs and to confirm proper handling of edge cases.

    <!-- end of auto-generated comment: release notes by coderabbit.ai -->

    Diff: 329 lines (+246 / -83) across 4 files Reviewer: rupayon123 Review duration: 634260m
    All Vouch review confidence checks passed
    Residual risk 0.29 = Change Risk (0.49) × (1 − Review Confidence (0.41)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.49 · 329 lines diff across 4 files
    0.49
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    40%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.347
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #13"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 40.9% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 49.1% (MEDIUM) · Higher = larger blast radius
    49.1%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 40.9% · Lower = rushed or shallow review
    40.9%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 29.0% · Safe to merge
    29.0%