Skip to content

Commit 3c932c0

Browse files
authored
Merge pull request #1333 from razmser/no-std-preserve-order
Support preserve_order in no_std builds
2 parents 4b2d726 + 35ca8ef commit 3c932c0

3 files changed

Lines changed: 23 additions & 9 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,16 @@ jobs:
6969
- run: cargo check --manifest-path tests/crate/Cargo.toml --no-default-features --features alloc,raw_value
7070
- run: cargo check --manifest-path tests/crate/Cargo.toml --features serde_json/preserve_order
7171
if: matrix.rust != '1.71.0'
72-
- run: cargo check --manifest-path tests/crate/Cargo.toml --no-default-features --features alloc,serde_json/preserve_order
72+
- run: cargo check --manifest-path tests/crate/Cargo.toml --no-default-features --features serde_json/preserve_order
7373
if: matrix.rust != '1.71.0'
7474
- name: Build without std
7575
run: cargo check --manifest-path tests/crate/Cargo.toml --target
7676
${{matrix.target}} --no-default-features --features alloc
7777
if: matrix.target
78+
- name: Build without std with preserve_order
79+
run: cargo check --manifest-path tests/crate/Cargo.toml --target
80+
${{matrix.target}} --no-default-features --features alloc,serde_json/preserve_order
81+
if: matrix.target
7882

7983
minimal:
8084
name: Minimal versions

‎Cargo.toml‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ repository = "https://github.com/serde-rs/json"
1111
rust-version = "1.71"
1212

1313
[dependencies]
14-
indexmap = { version = "2.2.3", optional = true }
14+
foldhash = { version = "0.2", optional = true, default-features = false }
15+
indexmap = { version = "2.2.3", optional = true, default-features = false }
1516
itoa = "1.0"
1617
memchr = { version = "2", default-features = false }
1718
serde_core = { version = "1.0.220", default-features = false }
@@ -61,7 +62,7 @@ alloc = ["serde_core/alloc"]
6162
# Make serde_json::Map use a representation which maintains insertion order.
6263
# This allows data to be read into a Value and written back to a JSON string
6364
# while preserving the order of map keys in the input.
64-
preserve_order = ["indexmap", "std"]
65+
preserve_order = ["indexmap", "alloc", "dep:foldhash"]
6566

6667
# Use sufficient precision when parsing fixed precision floats from JSON to
6768
# ensure that they maintain accuracy when round-tripped through JSON. This comes

‎src/map.rs‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@
33
//! By default the map is backed by a [`BTreeMap`]. Enable the `preserve_order`
44
//! feature of serde_json to use [`IndexMap`] instead.
55
//!
6+
//! In builds without the `std` feature, `preserve_order` hashes keys with
7+
//! foldhash's [`RandomState`]. Foldhash provides minimal HashDoS resistance,
8+
//! but does not protect against interactive attackers. Builds with `std` use
9+
//! the standard library's [`RandomState`].
10+
//!
611
//! [`BTreeMap`]: std::collections::BTreeMap
712
//! [`IndexMap`]: indexmap::IndexMap
13+
//! [`RandomState`]: std::collections::hash_map::RandomState
814
915
use crate::error::Error;
1016
use crate::value::Value;
@@ -33,14 +39,19 @@ pub struct Map<K, V> {
3339
#[cfg(not(feature = "preserve_order"))]
3440
type MapImpl<K, V> = BTreeMap<K, V>;
3541
#[cfg(feature = "preserve_order")]
36-
type MapImpl<K, V> = IndexMap<K, V>;
42+
type MapImpl<K, V> = IndexMap<K, V, BuildHasherImpl>;
43+
44+
#[cfg(all(feature = "preserve_order", feature = "std"))]
45+
type BuildHasherImpl = std::collections::hash_map::RandomState;
46+
#[cfg(all(feature = "preserve_order", not(feature = "std")))]
47+
type BuildHasherImpl = foldhash::fast::RandomState;
3748

3849
impl Map<String, Value> {
3950
/// Makes a new empty Map.
4051
#[inline]
4152
pub fn new() -> Self {
4253
Map {
43-
map: MapImpl::new(),
54+
map: MapImpl::default(),
4455
}
4556
}
4657

@@ -55,7 +66,7 @@ impl Map<String, Value> {
5566
BTreeMap::new()
5667
},
5768
#[cfg(feature = "preserve_order")]
58-
map: IndexMap::with_capacity(capacity),
69+
map: IndexMap::with_capacity_and_hasher(capacity, BuildHasherImpl::default()),
5970
}
6071
}
6172

@@ -386,9 +397,7 @@ impl Map<String, Value> {
386397
impl Default for Map<String, Value> {
387398
#[inline]
388399
fn default() -> Self {
389-
Map {
390-
map: MapImpl::new(),
391-
}
400+
Map::new()
392401
}
393402
}
394403

0 commit comments

Comments
 (0)