Skip to content

Commit 08d2bb6

Browse files
committed
improve docs, bump version
1 parent a27d99a commit 08d2bb6

6 files changed

Lines changed: 60 additions & 27 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
0.8.0 (2025-06-15)
2+
==================
3+
perf: add CowStr type to be able to use Cow::Borrowed on keys https://github.com/PSeitz/serde_json_borrow/pull/32 (Thanks @jszwec)
4+
15
0.7.1 (2024-11-02)
26
==================
37
strip extra iteration when initialising ObjectAsVec https://github.com/PSeitz/serde_json_borrow/pull/29 (Thanks @meskill)

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "serde_json_borrow"
33
categories = ["parsing", "parser-implementations", "encoding"]
44
authors = ["Pascal Seitz <pascal.seitz@gmail.com>"]
55
description = "Provides JSON deserialization into a borrowed DOM"
6-
version = "0.7.1"
6+
version = "0.8.0"
77
edition = "2021"
88
license = "MIT"
99
keywords = ["JSON", "serde", "deserialization", "ref", "borrowed"]

src/lib.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,23 @@
2525
//! due to less allocations. By borrowing a DOM, the library ensures that no additional memory is
2626
//! allocated for `Strings`, that contain no JSON escape codes.
2727
//!
28+
//! # Usage
29+
//! ```rust
30+
//! use std::io;
31+
//! fn main() -> io::Result<()> {
32+
//! let data: &str = r#"{"bool": true, "key": "123"}"#;
33+
//! // Note that serde_json_borrow::Value<'ctx> is tied to the lifetime of data.
34+
//! let value: serde_json_borrow::Value = serde_json::from_str(&data)?;
35+
//! assert_eq!(value.get("bool"), &serde_json_borrow::Value::Bool(true));
36+
//! assert_eq!(value.get("key"), &serde_json_borrow::Value::Str("123".into()));
37+
//! // Using OwnedValue will take ownership of the String.
38+
//! let value: serde_json_borrow::OwnedValue = serde_json_borrow::OwnedValue::from_str(&data)?;
39+
//! assert_eq!(value.get("bool"), &serde_json_borrow::Value::Bool(true));
40+
//! assert_eq!(value.get("key"), &serde_json_borrow::Value::Str("123".into()));
41+
//! Ok(())
42+
//! }
43+
//! ```
44+
//!
2845
//! ## OwnedValue
2946
//! You can take advantage of [`OwnedValue`] to parse a `String` containing
3047
//! unparsed `JSON` into a `Value` without having to worry about lifetimes,
@@ -36,7 +53,7 @@
3653
//! support for escaped data in keys. Without the `cowkeys` feature flag `&str` is used, which does
3754
//! not allow any JSON escaping characters in keys.
3855
//!
39-
//! List of _unsupported_ characters (https://www.json.org/json-en.html) in object keys without `cowkeys` feature flag.
56+
//! List of _unsupported_ characters (<https://www.json.org/json-en.html>) in object keys without `cowkeys` feature flag.
4057
//!
4158
//! ```text
4259
//! \" represents the quotation mark character (U+0022).
@@ -48,18 +65,6 @@
4865
//! \r represents the carriage return character (U+000D).
4966
//! \t represents the character tabulation character (U+0009).
5067
//! ```
51-
//! # Usage
52-
//! ```rust
53-
//! use std::io;
54-
//! use serde_json_borrow::Value;
55-
//! fn main() -> io::Result<()> {
56-
//! let data = r#"{"bool": true, "key": "123"}"#;
57-
//! let value: Value = serde_json::from_str(&data)?;
58-
//! assert_eq!(value.get("bool"), &Value::Bool(true));
59-
//! assert_eq!(value.get("key"), &Value::Str("123".into()));
60-
//! Ok(())
61-
//! }
62-
//! ```
6368
//! # Performance
6469
//! Performance gain depends on how many allocations can be avoided, and how many objects there are,
6570
//! as deserializing into a vec is significantly faster.
@@ -76,13 +81,13 @@ mod deserializer;
7681
mod index;
7782
mod num;
7883
mod object_vec;
79-
mod owned;
84+
mod ownedvalue;
8085
mod ser;
8186
mod value;
8287

8388
#[cfg(feature = "cowkeys")]
8489
mod cowstr;
8590

8691
pub use object_vec::{KeyStrType, ObjectAsVec, ObjectAsVec as Map};
87-
pub use owned::OwnedValue;
92+
pub use ownedvalue::OwnedValue;
8893
pub use value::Value;

src/object_vec.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ use std::borrow::Cow;
66
use crate::Value;
77

88
#[cfg(feature = "cowkeys")]
9-
/// The string type used. Can be toggled between &str and Cow<str> via `cowstr` feature flag
9+
/// The string type used. Can be toggled between `&str` and `Cow<str>` via `cowstr` feature flag
1010
pub type KeyStrType<'a> = crate::cowstr::CowStr<'a>;
1111

1212
#[cfg(not(feature = "cowkeys"))]
13-
/// The string type used. Can be toggled between &str and Cow<str> via `cowstr` feature flag
13+
/// The string type used. Can be toggled between `&str` and `Cow<str>` via `cowstr` feature flag
1414
/// Cow strings
1515
pub type KeyStrType<'a> = &'a str;
1616

@@ -51,7 +51,7 @@ impl<'ctx> ObjectAsVec<'ctx> {
5151
/// # Note
5252
/// Since KeyStrType can be changed via a feature flag avoid using `as_vec` and use other
5353
/// methods instead. This could be a problem with feature unification, when one crate uses it
54-
/// as &str and another uses it as Cow<str>, both will get Cow<str?
54+
/// as `&str` and another uses it as `Cow<str>`, both will get `Cow<str>`
5555
#[inline]
5656
pub fn as_vec(&self) -> &Vec<(KeyStrType, Value<'ctx>)> {
5757
&self.0

src/owned.rs renamed to src/ownedvalue.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,26 @@ use std::ops::Deref;
33

44
use crate::Value;
55

6-
/// Parses a `String` into `Value`, by taking ownership of `String` and reference slices from it in
7-
/// contrast to copying the contents.
6+
/// Parses a `String` into `Value`, by taking ownership of `String` and reference slices from it.
7+
///
8+
/// With [`crate::Value`], your lifetime is tied to the lifetime of the
9+
/// passed `str`. This means that the `Value` can only be used as long as the original `str` is
10+
/// valid. With [`OwnedValue`], you get a owned Value instead.
11+
///
12+
/// Note: `OwnedValue` does not implement `Deserialize`, as it is not intended to be used for
13+
/// deserialization. It is designed to be used when you already have a `String` containing JSON
14+
/// data, and you want to parse it into a `Value` without worrying about lifetimes.
15+
///
16+
/// ## Example
17+
/// ```
18+
/// use serde_json::json;
19+
/// use crate::OwnedValue;
20+
/// let raw_json = r#"{"name": "John", "age": 30}"#;
21+
/// let owned_value = OwnedValue::from_string(raw_json.to_string()).unwrap();
22+
/// assert_eq!(owned_value.get("name"), &Value::Str("John".into()));
23+
/// assert_eq!(owned_value.get("age"), &Value::Number(30_u64.into()));
24+
/// ```
825
///
9-
/// This is done to mitigate lifetime issues.
1026
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
1127
pub struct OwnedValue {
1228
/// Keep owned data, to be able to safely reference it from Value<'static>

src/ser.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
use serde::ser::{Serialize, Serializer};
22

33
use crate::num::{Number, N};
4-
use crate::owned::OwnedValue;
4+
use crate::ownedvalue::OwnedValue;
55
use crate::value::Value;
66
use crate::Map;
77

88
impl Serialize for Value<'_> {
99
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
10-
where S: Serializer {
10+
where
11+
S: Serializer,
12+
{
1113
match self {
1214
Value::Null => serializer.serialize_unit(),
1315
Value::Bool(b) => serializer.serialize_bool(*b),
@@ -20,21 +22,27 @@ impl Serialize for Value<'_> {
2022
}
2123
impl Serialize for Map<'_> {
2224
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
23-
where S: Serializer {
25+
where
26+
S: Serializer,
27+
{
2428
serializer.collect_map(self.iter())
2529
}
2630
}
2731

2832
impl Serialize for OwnedValue {
2933
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
30-
where S: Serializer {
34+
where
35+
S: Serializer,
36+
{
3137
Value::serialize(self.get_value(), serializer)
3238
}
3339
}
3440

3541
impl Serialize for Number {
3642
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
37-
where S: Serializer {
43+
where
44+
S: Serializer,
45+
{
3846
match self.n {
3947
N::PosInt(n) => serializer.serialize_u64(n),
4048
N::NegInt(n) => serializer.serialize_i64(n),

0 commit comments

Comments
 (0)