Skip to content

✨ feat(mq-lang): replace mq-lang JSON parser with native serde_json builtin#1462

Merged
harehare merged 1 commit intomainfrom
feat/native-json-parse-builtin
Mar 17, 2026
Merged

✨ feat(mq-lang): replace mq-lang JSON parser with native serde_json builtin#1462
harehare merged 1 commit intomainfrom
feat/native-json-parse-builtin

Conversation

@harehare
Copy link
Copy Markdown
Owner

No description provided.

…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.
Copilot AI review requested due to automatic review settings March 17, 2026 12:33
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_parse builtin that parses JSON via serde_json and converts it into RuntimeValue.
  • Added From<serde_json::Value> for RuntimeValue to 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())
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Mar 17, 2026

Merging this PR will improve performance by ×2

⚡ 1 improved benchmark
✅ 28 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
eval_json_parse 14.5 ms 7.1 ms ×2

Comparing feat/native-json-parse-builtin (2046ce1) with main (5d4b5a1)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (5e9ae3d) during the generation of this report, so 5d4b5a1 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@harehare harehare merged commit bb7ba4e into main Mar 17, 2026
12 checks passed
@harehare harehare deleted the feat/native-json-parse-builtin branch March 17, 2026 13:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants