Adding IndexMap
under feature flag preserve_order
(#36)
This commit is contained in:
parent
a02947462d
commit
a618a90eb4
3 changed files with 27 additions and 6 deletions
|
@ -19,7 +19,7 @@ serde_json = "1.0"
|
||||||
dyn-clone = "1.0"
|
dyn-clone = "1.0"
|
||||||
|
|
||||||
chrono = { version = "0.4", default-features = false, optional = true }
|
chrono = { version = "0.4", default-features = false, optional = true }
|
||||||
indexmap = { version = "1.2", optional = true }
|
indexmap = { version = "1.2", features=["serde-1"], optional = true }
|
||||||
either = { version = "1.3", default-features = false, optional = true }
|
either = { version = "1.3", default-features = false, optional = true }
|
||||||
uuid = { version = "0.8", default-features = false, optional = true }
|
uuid = { version = "0.8", default-features = false, optional = true }
|
||||||
smallvec = { version = "1.0", optional = true }
|
smallvec = { version = "1.0", optional = true }
|
||||||
|
@ -34,6 +34,11 @@ default = ["derive"]
|
||||||
|
|
||||||
derive = ["schemars_derive"]
|
derive = ["schemars_derive"]
|
||||||
|
|
||||||
|
# Use a different representation for the map type of Schemars.
|
||||||
|
# This allows data to be read into a Value and written back to a JSON string
|
||||||
|
# while preserving the order of map keys in the input.
|
||||||
|
preserve_order = ["indexmap"]
|
||||||
|
|
||||||
impl_json_schema = ["derive"]
|
impl_json_schema = ["derive"]
|
||||||
# derive_json_schema will be removed in a later version
|
# derive_json_schema will be removed in a later version
|
||||||
derive_json_schema = ["impl_json_schema"]
|
derive_json_schema = ["impl_json_schema"]
|
||||||
|
|
|
@ -101,7 +101,10 @@ impl<T> Merge for Vec<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<K: Ord, V> Merge for Map<K, V> {
|
impl<K, V> Merge for Map<K, V>
|
||||||
|
where
|
||||||
|
K: std::hash::Hash + Eq + Ord,
|
||||||
|
{
|
||||||
fn merge(mut self, other: Self) -> Self {
|
fn merge(mut self, other: Self) -> Self {
|
||||||
self.extend(other);
|
self.extend(other);
|
||||||
self
|
self
|
||||||
|
|
|
@ -197,8 +197,9 @@ fn main() {
|
||||||
`#[serde(...)]` attributes can be overriden using `#[schemars(...)]` attributes, which behave identically (e.g. `#[schemars(rename_all = "camelCase")]`). You may find this useful if you want to change the generated schema without affecting Serde's behaviour, or if you're just not using Serde.
|
`#[serde(...)]` attributes can be overriden using `#[schemars(...)]` attributes, which behave identically (e.g. `#[schemars(rename_all = "camelCase")]`). You may find this useful if you want to change the generated schema without affecting Serde's behaviour, or if you're just not using Serde.
|
||||||
|
|
||||||
## Feature Flags
|
## Feature Flags
|
||||||
- `derive` (enabled by default) - provides `#[derive(JsonSchema)]` macro
|
- `derive` (enabled by default) - provides `#[derive(JsonSchema)]` macro.
|
||||||
- `impl_json_schema` - implements `JsonSchema` for Schemars types themselves
|
- `impl_json_schema` - implements `JsonSchema` for Schemars types themselves.
|
||||||
|
- `preserve_order` - keep the order of structure fields in `Schema` and `SchemaObject`.
|
||||||
|
|
||||||
## Optional Dependencies
|
## Optional Dependencies
|
||||||
Schemars can implement `JsonSchema` on types from several popular crates, enabled via optional dependencies (dependency versions are shown in brackets):
|
Schemars can implement `JsonSchema` on types from several popular crates, enabled via optional dependencies (dependency versions are shown in brackets):
|
||||||
|
@ -212,15 +213,27 @@ Schemars can implement `JsonSchema` on types from several popular crates, enable
|
||||||
|
|
||||||
/// The map type used by schemars types.
|
/// The map type used by schemars types.
|
||||||
///
|
///
|
||||||
/// Currently a `BTreeMap`, but this may change a different implementation
|
/// Currently a `BTreeMap` or `IndexMap` can be used, but this may change to a different implementation
|
||||||
/// with a similar interface in a future version of schemars.
|
/// with a similar interface in a future version of schemars.
|
||||||
|
/// The `IndexMap` will be used when the `preserve_order` feature flag is set.
|
||||||
|
#[cfg(not(feature = "preserve_order"))]
|
||||||
pub type Map<K, V> = std::collections::BTreeMap<K, V>;
|
pub type Map<K, V> = std::collections::BTreeMap<K, V>;
|
||||||
|
#[cfg(feature = "preserve_order")]
|
||||||
|
pub type Map<K, V> = indexmap::IndexMap<K, V>;
|
||||||
/// The set type used by schemars types.
|
/// The set type used by schemars types.
|
||||||
///
|
///
|
||||||
/// Currently a `BTreeSet`, but this may change a different implementation
|
/// Currently a `BTreeSet`, but this may change to a different implementation
|
||||||
/// with a similar interface in a future version of schemars.
|
/// with a similar interface in a future version of schemars.
|
||||||
pub type Set<T> = std::collections::BTreeSet<T>;
|
pub type Set<T> = std::collections::BTreeSet<T>;
|
||||||
|
|
||||||
|
/// A view into a single entry in a map, which may either be vacant or occupied.
|
||||||
|
/// This is constructed from the `entry` method on `BTreeMap` or `IndexMap`
|
||||||
|
/// depending on whether the `preserve_order` feature flag is set.
|
||||||
|
#[cfg(not(feature = "preserve_order"))]
|
||||||
|
pub type MapEntry<'a, K, V> = std::collections::btree_map::Entry<'a, K, V>;
|
||||||
|
#[cfg(feature = "preserve_order")]
|
||||||
|
pub type MapEntry<'a, K, V> = indexmap::map::Entry<'a, K, V>;
|
||||||
|
|
||||||
mod flatten;
|
mod flatten;
|
||||||
mod json_schema_impls;
|
mod json_schema_impls;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue