forked from extern/nushell
f41c93b2d3
<!-- if this PR closes one or more issues, you can automatically link the PR with them by using one of the [*linking keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword), e.g. - this PR should close #xxxx - fixes #xxxx you can also mention related issues, PRs or discussions! --> # Description <!-- Thank you for improving Nushell. Please, check our [contributing guide](../CONTRIBUTING.md) and talk to the core team before making major changes. Description of your pull request goes here. **Provide examples and/or screenshots** if your changes affect the user experience. --> Clippy fixes for rust 1.76.0-nightly # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> N/A # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass (on Windows make sure to [enable developer mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging)) - `cargo run -- -c "use std testing; testing run-tests --path crates/nu-std"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. -->
79 lines
2.0 KiB
Markdown
79 lines
2.0 KiB
Markdown
# nu-json
|
|
|
|
[![crate](https://img.shields.io/crates/v/nu-json.svg?style=flat-square)](https://crates.io/crates/nu-json)
|
|
|
|
> a fork of [serde_hjson](https://crates.io/crates/serde-hjson).
|
|
|
|
> The changes made to this crate are kept in [CHANGELOG](/crates/nu-json/CHANGELOG.md).
|
|
|
|
|
|
The Rust implementation of Hjson is based on the [Serde JSON Serialization Library](https://github.com/serde-rs/json).
|
|
|
|
This crate is a Rust library for parsing and generating Human JSON [Hjson](https://hjson.github.io). It is built upon [Serde](https://github.com/serde-rs/serde), a high performance generic serialization framework.
|
|
|
|
# Install
|
|
|
|
This crate works with Cargo and can be found on [crates.io](https://crates.io/crates/nu-json) with a `Cargo.toml` like:
|
|
|
|
```toml
|
|
[dependencies]
|
|
serde = "1"
|
|
nu-json = "0.76"
|
|
```
|
|
|
|
## From the Commandline
|
|
|
|
Add with:
|
|
```
|
|
cargo add serde
|
|
cargo add nu-json
|
|
```
|
|
|
|
# Usage
|
|
|
|
```rust
|
|
extern crate serde;
|
|
extern crate nu_json;
|
|
|
|
use nu_json::{Map,Value};
|
|
|
|
fn main() {
|
|
|
|
// Now let's look at decoding Hjson data
|
|
|
|
let sample_text=r#"
|
|
{
|
|
# specify rate in requests/second
|
|
rate: 1000
|
|
array:
|
|
[
|
|
foo
|
|
bar
|
|
]
|
|
}"#;
|
|
|
|
// Decode and unwrap.
|
|
let mut sample: Map<String, Value> = nu_json::from_str(&sample_text).unwrap();
|
|
|
|
// scope to control lifetime of borrow
|
|
{
|
|
// Extract the rate
|
|
let rate = sample.get("rate").unwrap().as_f64().unwrap();
|
|
println!("rate: {}", rate);
|
|
|
|
// Extract the array
|
|
let array : &mut Vec<Value> = sample.get_mut("array").unwrap().as_array_mut().unwrap();
|
|
println!("first: {}", array.first().unwrap());
|
|
|
|
// Add a value
|
|
array.push(Value::String("tak".to_string()));
|
|
}
|
|
|
|
// Encode to Hjson
|
|
let sample2 = nu_json::to_string(&sample).unwrap();
|
|
println!("Hjson:\n{}", sample2);
|
|
}
|
|
```
|
|
# DOCS
|
|
|
|
At the moment, the documentation on [serde_hjson](https://docs.rs/serde-hjson/0.9.1/serde_hjson/) / [serde_json](https://docs.rs/serde_json/1.0.93/serde_json/) is also relevant for nu-json. |