diff --git a/crates/bevy_core/Cargo.toml b/crates/bevy_core/Cargo.toml index 5c26103cb7a82..90745ff093a3c 100644 --- a/crates/bevy_core/Cargo.toml +++ b/crates/bevy_core/Cargo.toml @@ -20,6 +20,10 @@ bevy_utils = { path = "../bevy_utils", version = "0.9.0-dev" } # other bytemuck = "1.5" +serde = { version = "1.0", optional = true } + +[features] +serialize = ["dep:serde"] [dev-dependencies] crossbeam-channel = "0.5.0" diff --git a/crates/bevy_core/src/lib.rs b/crates/bevy_core/src/lib.rs index 6badaabed691b..fb22cbc2ab732 100644 --- a/crates/bevy_core/src/lib.rs +++ b/crates/bevy_core/src/lib.rs @@ -2,6 +2,8 @@ //! This crate provides core functionality for Bevy Engine. mod name; +#[cfg(feature = "serialize")] +mod serde; mod task_pool_options; use bevy_ecs::system::Resource; diff --git a/crates/bevy_core/src/serde.rs b/crates/bevy_core/src/serde.rs new file mode 100644 index 0000000000000..f8835e9a5f8d2 --- /dev/null +++ b/crates/bevy_core/src/serde.rs @@ -0,0 +1,41 @@ +use std::{ + any, + fmt::{self, Formatter}, +}; + +use serde::{ + de::{Error, Visitor}, + Deserialize, Deserializer, Serialize, Serializer, +}; + +use super::name::Name; + +impl Serialize for Name { + fn serialize(&self, serializer: S) -> Result { + serializer.serialize_str(self.as_str()) + } +} + +impl<'de> Deserialize<'de> for Name { + fn deserialize>(deserializer: D) -> Result { + deserializer.deserialize_str(EntityVisitor) + } +} + +struct EntityVisitor; + +impl<'de> Visitor<'de> for EntityVisitor { + type Value = Name; + + fn expecting(&self, formatter: &mut Formatter) -> fmt::Result { + formatter.write_str(any::type_name::()) + } + + fn visit_str(self, v: &str) -> Result { + Ok(Name::new(v.to_string())) + } + + fn visit_string(self, v: String) -> Result { + Ok(Name::new(v)) + } +} diff --git a/crates/bevy_internal/Cargo.toml b/crates/bevy_internal/Cargo.toml index e61f55f24e243..dcbeecded07df 100644 --- a/crates/bevy_internal/Cargo.toml +++ b/crates/bevy_internal/Cargo.toml @@ -45,7 +45,7 @@ wav = ["bevy_audio/wav"] # Enable watching file system for asset hot reload filesystem_watcher = ["bevy_asset/filesystem_watcher"] -serialize = ["bevy_input/serialize", "bevy_time/serialize", "bevy_window/serialize"] +serialize = ["bevy_input/serialize", "bevy_time/serialize", "bevy_window/serialize", "bevy_core/serialize"] # Display server protocol support (X11 is enabled by default) wayland = ["bevy_winit/wayland"]