✨ feat(mq-lang): replace mq-lang JSON parser with native serde_json builtin#1462
Merged
✨ feat(mq-lang): replace mq-lang JSON parser with native serde_json builtin#1462
Conversation
…uiltin Add `_json_parse` builtin backed by serde_json, removing the hand-rolled mq-lang parser (~170 lines). Make serde/serde_json unconditional dependencies and add `From<serde_json::Value>` for `RuntimeValue`. Update module tests to reflect BTreeMap key ordering.
Contributor
There was a problem hiding this comment.
Pull request overview
Replaces the mq-language-level JSON parser implemented in modules/json.mq with a Rust builtin backed by serde_json, so JSON parsing is handled natively in mq-lang during evaluation.
Changes:
- Added an internal
_json_parsebuiltin that parses JSON viaserde_jsonand converts it intoRuntimeValue. - Added
From<serde_json::Value> for RuntimeValueto support converting parsed JSON into mq runtime values. - Updated module tests’ expected JSON stringify and markdown-table outputs to match the new ordering produced by the native parser/runtime dict iteration.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| crates/mq-lang/src/eval/runtime_value.rs | Adds conversion from serde_json::Value into RuntimeValue for use by the new builtin. |
| crates/mq-lang/src/eval/builtin.rs | Introduces _json_parse builtin, registers it, documents it, and adds unit tests. |
| crates/mq-lang/modules/json.mq | Switches json_parse to delegate to the _json_parse builtin instead of the mq-level parser. |
| crates/mq-lang/modules/module_tests.mq | Updates expected outputs for JSON stringify and JSON-to-table tests. |
| crates/mq-lang/Cargo.toml | Makes serde/serde_json non-optional dependencies and adjusts the ast-json feature accordingly. |
| def test_json_stringify(): | ||
| let result = json::json_stringify(json::json_parse(json_input)) | ||
| | assert_eq(result, "{\"users\": [{\"name\": \"Alice\", \"id\": 1, \"email\": \"[email protected]\", \"roles\": [\"admin\", \"user\"]}, {\"name\": \"Bob\", \"id\": 2, \"email\": \"[email protected]\", \"roles\": [\"user\"]}, {\"name\": \"Charlie\", \"id\": 3, \"email\": \"[email protected]\", \"roles\": [\"editor\", \"user\"]}], \"meta\": {\"count\": 3, \"generated_at\": \"2024-06-01T12:00:00Z\"}}") | ||
| | assert_eq(result, "{\"meta\": {\"count\": 3, \"generated_at\": \"2024-06-01T12:00:00Z\"}, \"users\": [{\"name\": \"Alice\", \"email\": \"[email protected]\", \"id\": 1, \"roles\": [\"admin\", \"user\"]}, {\"name\": \"Bob\", \"email\": \"[email protected]\", \"id\": 2, \"roles\": [\"user\"]}, {\"name\": \"Charlie\", \"email\": \"[email protected]\", \"id\": 3, \"roles\": [\"editor\", \"user\"]}]}") |
Comment on lines
73
to
+75
| let result = json::json_parse(json_input) | ||
| | let result = json::json_to_markdown_table(result["users"]) | ||
| | assert_eq(result, "| name | id | email | roles |\n| --- | --- | --- | --- |\n| Alice | 1 | [email protected] | [\"admin\", \"user\"] |\n| Bob | 2 | [email protected] | [\"user\"] |\n| Charlie | 3 | [email protected] | [\"editor\", \"user\"] |") | ||
| | assert_eq(result, "| name | email | id | roles |\n| --- | --- | --- | --- |\n| Alice | [email protected] | 1 | [\"admin\", \"user\"] |\n| Bob | [email protected] | 2 | [\"user\"] |\n| Charlie | [email protected] | 3 | [\"editor\", \"user\"] |") |
Comment on lines
+28
to
+37
| serde = {workspace = true, features = ["derive", "rc"]} | ||
| serde_json = {workspace = true} | ||
| smallvec = {workspace = true} | ||
| smol_str = {workspace = true} | ||
| string-interner = {workspace = true} | ||
| thiserror = {workspace = true} | ||
| url = {workspace = true} | ||
|
|
||
| [features] | ||
| ast-json = ["dep:serde", "dep:serde_json", "smallvec/serde", "smol_str/serde"] | ||
| ast-json = ["smallvec/serde", "smol_str/serde"] |
Comment on lines
+221
to
+224
| if let Some(f) = n.as_f64() { | ||
| RuntimeValue::Number(f.into()) | ||
| } else { | ||
| RuntimeValue::Number(0.into()) |
Merging this PR will improve performance by ×2
Performance Changes
Comparing Footnotes |
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.
No description provided.