Skip to content

Conversation

@agatti
Copy link
Contributor

@agatti agatti commented Apr 17, 2025

Summary

This PR makes the JSON parser raise an exception when handling objects or arrays whose declaration is incomplete, as in missing the closing marker (brace or bracket) and if the missing marker would have been the last non-whitespace character in the incoming string.

Since CPython's JSON parser would raise an exception in such a case, unlike MicroPython's, this PR aligns MicroPython's behaviour with CPython.

This commit fixes issue #17141.

Testing

This was tested with the Unix port, passing all JSON-related tests (existing and new).

Trade-offs and Alternatives

There may be some code out there that relies on this peculiar behaviour, and thus this PR may introduce an incompatibility across MicroPython versions.

On the other hand, the code wouldn't have worked in CPython in the first place as an incomplete JSON object is not meant to be parseable by json.load or json.loads anyway since they expect a fully formed JSON object to be available (either from a string or from a file descriptor).

@github-actions
Copy link

github-actions bot commented Apr 17, 2025

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:    +0 +0.000% standard
      stm32:    +8 +0.002% PYBV10
     mimxrt:    +8 +0.002% TEENSY40
        rp2:    +8 +0.001% RPI_PICO_W
       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:    +2 +0.000% VIRT_RV32

@agatti agatti force-pushed the detect-incomplete-json-objects branch from 98e5c39 to 65d74ce Compare April 17, 2025 15:35
@dpgeorge dpgeorge added the extmod Relates to extmod/ directory in source label May 7, 2025
@codecov
Copy link

codecov bot commented May 7, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.54%. Comparing base (7a55cb6) to head (9ef16b4).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #17148      +/-   ##
==========================================
- Coverage   98.54%   98.54%   -0.01%     
==========================================
  Files         169      169              
  Lines       21897    21898       +1     
==========================================
  Hits        21579    21579              
- Misses        318      319       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

This commit makes the JSON parser raise an exception when handling
objects or arrays whose declaration is incomplete, as in missing the
closing marker (brace or bracket) and if the missing marker would have
been the last non-whitespace character in the incoming string.

Since CPython's JSON parser would raise an exception in such a case,
unlike MicroPython's, this commit aligns MicroPython's behaviour with
CPython.

This commit fixes issue micropython#17141.

Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
@agatti agatti force-pushed the detect-incomplete-json-objects branch from 65d74ce to 9ef16b4 Compare May 19, 2025 00:09
@dpgeorge dpgeorge merged commit 9ef16b4 into micropython:master May 20, 2025
64 checks passed
@dpgeorge
Copy link
Member

Thanks for updating, now merged.

@agatti agatti deleted the detect-incomplete-json-objects branch May 20, 2025 08:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

extmod Relates to extmod/ directory in source

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants