Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ser/de/schema of serde_json::Value #314

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ cargo test --features de_strict_order 'roundtrip::test_hash_map'
cargo test --features de_strict_order 'roundtrip::test_btree_map'
########## features = ["bson"] group
cargo test --features bson,derive 'roundtrip::requires_derive_category::test_bson_object_ids'
########## features = ["serde_json_value"] group
cargo test --features serde_json_value 'roundtrip::test_serde_json_value'
cargo test --features serde_json_value,unstable__schema 'schema::test_serde_json_value'
########## features = ["bytes"] group
cargo test --features bytes,derive 'roundtrip::requires_derive_category::test_ultimate_many_features_combined'

Expand All @@ -44,6 +47,9 @@ cargo test --no-default-features --features ascii,unstable__schema 'schema::test
########## features = ["rc"] group
cargo test --no-default-features --features rc 'roundtrip::test_rc'
cargo test --no-default-features --features rc,unstable__schema 'schema::test_rc'
########## features = ["serde_json_value"] group
cargo test --features serde_json_value 'roundtrip::test_serde_json_value'
cargo test --features serde_json_value,unstable__schema 'schema::test_serde_json_value'
########## features = ["hashbrown"] group
cargo test --no-default-features --features hashbrown
cargo test --no-default-features --features hashbrown,derive
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: run cargo doc
run: RUSTDOCFLAGS="-D warnings" cargo doc --features derive,unstable__schema
run: RUSTDOCFLAGS="-D warnings" cargo doc --features derive,unstable__schema,rc,bytes,bson,ascii,serde_json_value

release-plz:
runs-on: ubuntu-latest
Expand Down
4 changes: 3 additions & 1 deletion borsh-derive/src/internals/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,9 @@ impl FindTyParams {
)]
match bound {
TypeParamBound::Trait(bound) => self.visit_path(&bound.path),
TypeParamBound::Lifetime(_) | TypeParamBound::Verbatim(_) => {}
TypeParamBound::Lifetime(_)
| TypeParamBound::Verbatim(_)
| TypeParamBound::PreciseCapture(_) => {}
_ => {}
}
}
Expand Down
5 changes: 4 additions & 1 deletion borsh/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ borsh-derive = { path = "../borsh-derive", version = "~1.5.1", optional = true }
hashbrown = { version = ">=0.11,<0.15.0", optional = true }
bytes = { version = "1", optional = true }
bson = { version = "2", optional = true }
serde_json = { version = "1", optional = true }

[dev-dependencies]
insta = "1.29.0"

[package.metadata.docs.rs]
features = ["derive", "unstable__schema", "rc"]
features = ["derive", "unstable__schema", "rc", "bytes", "bson", "ascii", "serde_json_value"]
targets = ["x86_64-unknown-linux-gnu"]

[features]
Expand All @@ -54,3 +55,5 @@ std = []
# Be sure that this is what you want before enabling this feature.
rc = []
de_strict_order = []
# Implements BorshSerialize,BorshDeserialize and BorshSchema for serde_json::Value.
serde_json_value = ["dep:serde_json"]
85 changes: 85 additions & 0 deletions borsh/src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,91 @@ impl BorshDeserialize for bson::oid::ObjectId {
}
}

#[cfg(feature = "serde_json_value")]
impl BorshDeserialize for serde_json::Value {
#[inline]
fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
let flag: u8 = BorshDeserialize::deserialize_reader(reader)?;
match flag {
0 => Ok(Self::Null),
1 => {
let b: bool = BorshDeserialize::deserialize_reader(reader)?;
Ok(Self::Bool(b))
}
2 => {
let n: serde_json::Number = BorshDeserialize::deserialize_reader(reader)?;
Ok(Self::Number(n))
}
3 => {
let s: String = BorshDeserialize::deserialize_reader(reader)?;
Ok(Self::String(s))
}
4 => {
let a: Vec<Self> = BorshDeserialize::deserialize_reader(reader)?;
Ok(Self::Array(a))
}
5 => {
let o: serde_json::Map<_, _> = BorshDeserialize::deserialize_reader(reader)?;
Ok(Self::Object(o))
}
_ => {
let msg = format!(
"Invalid JSON value representation: {}. The first byte must be 0-5",
flag
);

Err(Error::new(ErrorKind::InvalidData, msg))
}
}
}
}

#[cfg(feature = "serde_json_value")]
impl BorshDeserialize for serde_json::Number {
#[inline]
fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
let flag: u8 = BorshDeserialize::deserialize_reader(reader)?;
match flag {
0 => {
let u: u64 = BorshDeserialize::deserialize_reader(reader)?;
Ok(u.into())
}
1 => {
let i: i64 = BorshDeserialize::deserialize_reader(reader)?;
Ok(i.into())
}
2 => {
let f: f64 = BorshDeserialize::deserialize_reader(reader)?;
// This returns None if the number is a NaN or +/-Infinity,
// which are not valid JSON numbers.
Self::from_f64(f).ok_or_else(|| {
let msg = format!("Invalid JSON number: {}", f);

Error::new(ErrorKind::InvalidData, msg)
})
}
_ => {
let msg = format!(
"Invalid JSON number representation: {}. The first byte must be 0-2",
flag
);

Err(Error::new(ErrorKind::InvalidData, msg))
}
}
}
}

#[cfg(feature = "serde_json_value")]
impl BorshDeserialize for serde_json::Map<String, serde_json::Value> {
#[inline]
fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
// The implementation here is identical to that of BTreeMap<K, V>.
let vec = <Vec<(String, serde_json::Value)>>::deserialize_reader(reader)?;
Ok(vec.into_iter().collect())
}
}

impl<T> BorshDeserialize for Cow<'_, T>
where
T: ToOwned + ?Sized,
Expand Down
3 changes: 3 additions & 0 deletions borsh/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@
and [Ord] for btree ones. Deserialization emits error otherwise.

If this feature is not enabled, it is possible that two different byte slices could deserialize into the same `HashMap`/`HashSet` object.
* **serde_json_value** -
Gates implementation of [BorshSerialize], [BorshDeserialize], [BorshSchema] for
[serde_json::Value] and [serde_json] types it depends on.

### Config aliases

Expand Down
87 changes: 87 additions & 0 deletions borsh/src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,93 @@ impl BorshSerialize for bson::oid::ObjectId {
}
}

#[cfg(feature = "serde_json_value")]
impl BorshSerialize for serde_json::Value {
#[inline]
fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> {
match self {
Self::Null => 0_u8.serialize(writer),
Self::Bool(b) => {
1_u8.serialize(writer)?;
b.serialize(writer)
}
Self::Number(n) => {
2_u8.serialize(writer)?;
n.serialize(writer)
}
Self::String(s) => {
3_u8.serialize(writer)?;
s.serialize(writer)
}
Self::Array(a) => {
4_u8.serialize(writer)?;
a.serialize(writer)
}
Self::Object(o) => {
5_u8.serialize(writer)?;
o.serialize(writer)
}
}
}
}

#[cfg(feature = "serde_json_value")]
impl BorshSerialize for serde_json::Number {
#[inline]
fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> {
// A JSON number can either be a non-negative integer (represented in
// serde_json by a u64), a negative integer (by an i64), or a non-integer
// (by an f64).
// We identify these cases with the following single-byte discriminants:
// 0 - u64
// 1 - i64
// 2 - f64
if let Some(u) = self.as_u64() {
0_u8.serialize(writer)?;
return u.serialize(writer);
}

if let Some(i) = self.as_i64() {
1_u8.serialize(writer)?;
return i.serialize(writer);
}

if let Some(f) = self.as_f64() {
2_u8.serialize(writer)?;
return f.serialize(writer);
}

unreachable!("number is neither a u64, i64, nor f64");
}
}

#[cfg(feature = "serde_json_value")]
/// Module is available if borsh is built with `features = ["serde_json_value"]`.
///
/// Module defines [BorshSerialize] implementation for [serde_json::Map],
pub mod serde_json_value {
use super::BorshSerialize;
use crate::io::{ErrorKind, Result, Write};
use core::convert::TryFrom;

impl BorshSerialize for serde_json::Map<String, serde_json::Value> {
#[inline]
fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> {
// The implementation here is identical to that of BTreeMap<String, serde_json::Value>.
u32::try_from(self.len())
.map_err(|_| ErrorKind::InvalidData)?
.serialize(writer)?;

for (key, value) in self {
key.serialize(writer)?;
value.serialize(writer)?;
}

Ok(())
}
}
}

impl<T> BorshSerialize for VecDeque<T>
where
T: BorshSerialize,
Expand Down
Loading
Loading