Skip to content

Commit

Permalink
implement ToPyObject for JsonValue (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin authored Sep 18, 2023
1 parent a07ebbb commit e21984b
Show file tree
Hide file tree
Showing 7 changed files with 277 additions and 6 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ jobs:
steps:
- uses: actions/checkout@v3

- name: set up python
uses: actions/setup-python@v4
with:
python-version: '3.11'

- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.rust-version }}
Expand All @@ -33,7 +38,7 @@ jobs:

- run: rustup component add llvm-tools-preview

- run: cargo test --test main
- run: cargo test -F python
env:
RUST_BACKTRACE: 1
RUSTFLAGS: '-C instrument-coverage'
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/profiling/
/scratch/
/*.bm
/env*/
1 change: 0 additions & 1 deletion .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
imports_granularity = "Module"
max_width = 120
217 changes: 214 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "jiter"
version = "0.0.1"
version = "0.0.2"
edition = "2021"
description = "Iterable JSON parser"
readme = "README.md"
Expand All @@ -18,11 +18,16 @@ num-bigint = "0.4.3"
num-traits = "0.2.16"
ahash = "0.8.0"
smallvec = "1.11.0"
pyo3 = { version = "0.19.2", features = ["num-bigint"], optional = true }

[features]
python = ["dep:pyo3"]

[dev-dependencies]
paste = "1.0.7"
serde_json = {version = "1.0.87", features = ["preserve_order", "arbitrary_precision"]}
serde = "1.0.147"
pyo3 = { version = "0.19.2", features = ["num-bigint", "auto-initialize"] }

[profile.bench]
lto = true
Expand Down
22 changes: 22 additions & 0 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,28 @@ pub enum JsonValue {
Object(Box<LazyIndexMap<String, JsonValue>>),
}

#[cfg(feature = "python")]
impl pyo3::ToPyObject for JsonValue {
fn to_object(&self, py: pyo3::Python<'_>) -> pyo3::PyObject {
match self {
Self::Null => py.None(),
Self::Bool(b) => b.to_object(py),
Self::Int(i) => i.to_object(py),
Self::BigInt(b) => b.to_object(py),
Self::Float(f) => f.to_object(py),
Self::String(s) => s.to_object(py),
Self::Array(v) => pyo3::types::PyList::new(py, v.iter().map(|v| v.to_object(py))).to_object(py),
Self::Object(o) => {
let dict = pyo3::types::PyDict::new(py);
for (k, v) in o.iter() {
dict.set_item(k, v.to_object(py)).unwrap();
}
dict.to_object(py)
}
}
}
}

impl JsonValue {
pub fn parse(data: &[u8]) -> Result<Self, JsonValueError> {
let mut parser = Parser::new(data);
Expand Down
Loading

0 comments on commit e21984b

Please sign in to comment.