Make serde_json an optional optimization for the json module · RustPython/RustPython@a950aa6 · GitHub
Skip to content

Commit a950aa6

Browse files
committed
Make serde_json an optional optimization for the json module
1 parent 31780be commit a950aa6

6 files changed

Lines changed: 69 additions & 0 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/json/__init__.py

Lines changed: 12 additions & 0 deletions

Lib/json/decoder.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@ class JSONDecodeError(ValueError):
2727
colno: The column corresponding to pos
2828
2929
"""
30+
# RUSTPYTHON SPECIFIC
31+
@classmethod
32+
def _from_serde(cls, msg, doc, line, col):
33+
pos = 0
34+
# 0-indexed
35+
line -= 1
36+
col -= 1
37+
while line > 0:
38+
i = doc.index('\n', pos)
39+
line -= 1
40+
pos = i
41+
pos += col
42+
return cls(msg, doc, pos)
43+
44+
3045
# Note that this exception is used from _json
3146
def __init__(self, msg, doc, pos):
3247
lineno = doc.count('\n', 0, pos) + 1

vm/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ rustpython-bytecode = { path = "../bytecode", version = "0.1.2" }
4747
rustpython-jit = { path = "../jit", optional = true, version = "0.1.2" }
4848
rustpython-pylib = { path = "pylib-crate", optional = true, version = "0.1.0" }
4949
serde = { version = "1.0.66", features = ["derive"] }
50+
serde_json = "1.0"
5051
byteorder = "1.2.6"
5152
regex = "1"
5253
rustc_version_runtime = "0.1.*"

vm/src/stdlib/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ mod platform;
2626
mod pystruct;
2727
mod random;
2828
mod re;
29+
mod serde_json;
2930
#[cfg(not(target_arch = "wasm32"))]
3031
pub mod socket;
3132
mod string;
@@ -91,6 +92,7 @@ pub fn get_module_inits() -> HashMap<String, StdlibInitFunc> {
9192
"_platform".to_owned() => Box::new(platform::make_module),
9293
"regex_crate".to_owned() => Box::new(re::make_module),
9394
"_random".to_owned() => Box::new(random::make_module),
95+
"_serde_json".to_owned() => Box::new(serde_json::make_module),
9496
"_string".to_owned() => Box::new(string::make_module),
9597
"_struct".to_owned() => Box::new(pystruct::make_module),
9698
"time".to_owned() => Box::new(time_module::make_module),

vm/src/stdlib/serde_json.rs

Lines changed: 38 additions & 0 deletions

0 commit comments

Comments
 (0)