Update JsValue::to_json to support undefined#4212
Merged
jedel1043 merged 3 commits intoboa-dev:mainfrom Mar 20, 2025
Merged
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4212 +/- ##
==========================================
+ Coverage 47.24% 52.59% +5.34%
==========================================
Files 476 487 +11
Lines 46892 51965 +5073
==========================================
+ Hits 22154 27330 +5176
+ Misses 24738 24635 -103 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Member
I think this is fine. The specification already does this, so it is nice to have parity with it. |
jedel1043
reviewed
Mar 20, 2025
Comment on lines
116
to
118
| /// # Panics | ||
| /// | ||
| /// Panics if the `JsValue` is `Undefined`. |
Member
There was a problem hiding this comment.
This should probably be removed.
Contributor
Author
There was a problem hiding this comment.
Done, good catch.
jedel1043
approved these changes
Mar 20, 2025
Member
jedel1043
left a comment
There was a problem hiding this comment.
Looks good! Thank you for the contribution!
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.
This Pull Request fixes/closes #3842.
Personally I've found
JsValue::to_jsonto be fairly useless in its current implementation, as it will panic if the data structure being converted contains undefined, and I often don't have control over this.The PR changes the following:
The
JsValue::to_jsonmethod no longer panics if the structure containsundefined.If
undefinedis encountered it will:nullifundefinedis an element of an array. I don't love this, but couldn't think of a better solution.to_jsonis being called onJsValue::undefined().Because of the last point there is a breaking change:
JsValue::to_jsonnow returnsJsResult<Option<Value>>instead ofJsResult<Value>.To avoid a breaking change we could perhaps introduce a new method
try_to_jsoninstead, and have the originalto_jsoncalltry_to_jsonand panic ifNoneis returned.On the other hand, the new behaviour forces people to think about handling
undefined, which will probably prevent unexpected panics in the future.