Skip to content

Commit

Permalink
Add a example using the tape API and fix some more doc stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Kriskras99 authored and Licenser committed Sep 11, 2024
1 parent ab9818c commit d09fa74
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 26 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,12 @@ An highly experimental implementation of the algorithm using `std::simd` and up

## Usage

simd-json offers two main entry points for usage:
simd-json offers three main entry points for usage:

### Values API

The values API is a set of optimized DOM objects that allow parsed
json to JSON data that has no known variable structure. simd-lite
JSON to JSON data that has no known variable structure. `simd-json`
has two versions of this:

**Borrowed Values**
Expand Down Expand Up @@ -175,6 +175,22 @@ let mut d = br#"{"some": ["key", "value", 2]}"#.to_vec();
let v: Value = simd_json::serde::from_slice(&mut d).unwrap();
```

### Tape API

```rust
use simd_json;

let mut d = br#"{"the_answer": 42}"#.to_vec();
let tape = simd_json::to_tape(&mut d).unwrap();
let value = tape.as_value();
// try_get treats value like an object, returns Ok(Some(_)) because the key is found
assert!(value.try_get("the_answer").unwrap().unwrap() == 42);
// returns Ok(None) because the key is not found but value is an object
assert!(value.try_get("does_not_exist").unwrap() == None);
// try_get_idx treats value like an array, returns Err(_) because value is not an array
assert!(value.try_get_idx(0).is_err());
```

## Other interesting things

There are also bindings for upstream `simdjson` available [here](https://github.com/SunDoge/simdjson-rust)
Expand Down
2 changes: 1 addition & 1 deletion src/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/// Prelude to include needed traits
// Prelude to include needed traits
pub use value_trait::prelude::*;
26 changes: 14 additions & 12 deletions src/value.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/// This module holds the two dom implementations we use. We distinguish between
/// owned and borrowed. The difference being is that the borrowed value will
/// use `&str` as its string type, referencing the input, while owned will
/// allocate a new String for each value.
/// This module holds the two dom implementations we use.
///
/// Note that since json strings allow for for escape sequences the borrowed
/// We distinguish between owned and borrowed. The difference being is that
/// the borrowed value will use `&str` as its string type, referencing the input,
/// while owned will allocate a new String for each value.
///
/// Note that since JSON strings allow for escape sequences the borrowed
/// value does not implement zero copy parsing, it does however not allocate
/// new memory for strings.
///
/// This differs notably from serde's zero copy implementation as, unlike serde,
/// we do not require prior knowledge about string content to to take advantage
/// This differs notably from Serde's zero copy implementation as, unlike Serde,
/// we do not require prior knowledge about string content to take advantage
/// of it.
///
/// ## Usage
Expand Down Expand Up @@ -57,7 +58,6 @@ pub mod owned;
/// Tape implementation
pub mod tape;

/// A value that starts out as a tape and upgraes to a borrowed value when mutation is needed
pub mod lazy;

pub use self::borrowed::{
Expand All @@ -82,8 +82,9 @@ pub type ObjectHasher = crate::known_key::NotSoRandomState;
#[cfg(not(feature = "known-key"))]
pub type ObjectHasher = halfbrown::DefaultHashBuilder;

/// Parses a slice of bytes into a Value dom. This function will
/// rewrite the slice to de-escape strings.
/// Parses a slice of bytes into a Value dom.
///
/// This function will rewrite the slice to de-escape strings.
/// As we reference parts of the input slice the resulting dom
/// has the same lifetime as the slice it was created from.
///
Expand All @@ -101,8 +102,9 @@ where
}
}

/// Parses a slice of bytes into a Value dom. This function will
/// rewrite the slice to de-escape strings.
/// Parses a slice of bytes into a Value dom.
///
/// This function will rewrite the slice to de-escape strings.
/// As we reference parts of the input slice the resulting dom
/// has the same lifetime as the slice it was created from.
///
Expand Down
10 changes: 6 additions & 4 deletions src/value/borrowed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ pub type Object<'value> = HashMap<Cow<'value, str>, Value<'value>, ObjectHasher>
/// Representation of a JSON array
pub type Array<'value> = Vec<Value<'value>>;

/// Parses a slice of bytes into a Value dom. This function will
/// rewrite the slice to de-escape strings.
/// Parses a slice of bytes into a Value dom.
///
/// This function will rewrite the slice to de-escape strings.
/// As we reference parts of the input slice the resulting dom
/// has the same lifetime as the slice it was created from.
///
Expand All @@ -52,8 +53,9 @@ pub fn to_value(s: &mut [u8]) -> Result<Value> {
}
}

/// Parses a slice of bytes into a Value dom. This function will
/// rewrite the slice to de-escape strings.
/// Parses a slice of bytes into a Value dom.
///
/// This function will rewrite the slice to de-escape strings.
/// As we reference parts of the input slice the resulting dom
/// has the same lifetime as the slice it was created from.
///
Expand Down
7 changes: 4 additions & 3 deletions src/value/lazy.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Lazy value, this gets initialized with a tape and as long as only non mutating operations are performed
//! it will stay a tape. If it is mutated it is upgtaded to a borrowed value.
//! This allows for a very cheap parsin and data access while still maintaining mutability.
//! Lazy value, uses a tape until mutated.
//!
//! If it is mutated it is upgraded to a borrowed value.
//! This allows for cheap parsing and data access while still maintaining mutability.
//!
//! # Example
//!
Expand Down
10 changes: 6 additions & 4 deletions src/value/owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ use std::ops::{Index, IndexMut};
/// Representation of a JSON object
pub type Object = HashMap<String, Value, ObjectHasher>;

/// Parses a slice of bytes into a Value dom. This function will
/// rewrite the slice to de-escape strings.
/// Parses a slice of bytes into a Value dom.
///
/// This function will rewrite the slice to de-escape strings.
/// We do not keep any references to the raw data but re-allocate
/// owned memory wherever required thus returning a value without
/// a lifetime.
Expand All @@ -49,8 +50,9 @@ pub fn to_value(s: &mut [u8]) -> Result<Value> {
}
}

/// Parses a slice of bytes into a Value dom. This function will
/// rewrite the slice to de-escape strings.
/// Parses a slice of bytes into a Value dom.
///
/// This function will rewrite the slice to de-escape strings.
/// We do not keep any references to the raw data but re-allocate
/// owned memory wherever required thus returning a value without
/// a lifetime.
Expand Down

0 comments on commit d09fa74

Please sign in to comment.