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

bevy_reflect: Improve serialization format #4561

Closed
wants to merge 24 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
52 changes: 16 additions & 36 deletions assets/scenes/load_scene_example.scn.ron
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,29 @@
entity: 0,
components: [
{
"type": "bevy_transform::components::transform::Transform",
"struct": {
"bevy_transform::components::transform::Transform": {
"translation": {
"type": "glam::f32::vec3::Vec3",
"value": (0.0, 0.0, 0.0),
},
"rotation": {
"type": "glam::f32::sse2::quat::Quat",
"value": (0.0, 0.0, 0.0, 1.0),
"x": 0.0,
"y": 0.0,
"z": 0.0
},
"rotation": (0.0, 0.0, 0.0, 1.0),
"scale": {
"type": "glam::f32::vec3::Vec3",
"value": (1.0, 1.0, 1.0),
"x": 1.0,
"y": 1.0,
"z": 1.0
},
},
},
{
"type": "scene::ComponentB",
"struct": {
"value": {
"type": "alloc::string::String",
"value": "hello",
},
"scene::ComponentB": {
"value": "hello",
},
},
{
"type": "scene::ComponentA",
"struct": {
"x": {
"type": "f32",
"value": 1.0,
},
"y": {
"type": "f32",
"value": 2.0,
},
"scene::ComponentA": {
"x": 1.0,
"y": 2.0,
},
},
],
Expand All @@ -47,16 +34,9 @@
entity: 1,
components: [
{
"type": "scene::ComponentA",
"struct": {
"x": {
"type": "f32",
"value": 3.0,
},
"y": {
"type": "f32",
"value": 4.0,
},
"scene::ComponentA": {
"x": 3.0,
"y": 4.0,
},
},
],
Expand Down
26 changes: 13 additions & 13 deletions crates/bevy_reflect/src/impls/glam.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ use bevy_reflect_derive::{impl_from_reflect_value, impl_reflect_struct, impl_ref
use glam::*;

impl_reflect_struct!(
#[reflect(Debug, PartialEq, Serialize, Deserialize, Default)]
#[reflect(Debug, PartialEq, Default)]
struct IVec2 {
x: i32,
y: i32,
}
);
impl_reflect_struct!(
#[reflect(Debug, PartialEq, Serialize, Deserialize, Default)]
#[reflect(Debug, PartialEq, Default)]
struct IVec3 {
x: i32,
y: i32,
z: i32,
}
);
impl_reflect_struct!(
#[reflect(Debug, PartialEq, Serialize, Deserialize, Default)]
#[reflect(Debug, PartialEq, Default)]
struct IVec4 {
x: i32,
y: i32,
Expand All @@ -31,22 +31,22 @@ impl_reflect_struct!(
);

impl_reflect_struct!(
#[reflect(Debug, PartialEq, Serialize, Deserialize, Default)]
#[reflect(Debug, PartialEq, Default)]
struct UVec2 {
x: u32,
y: u32,
}
);
impl_reflect_struct!(
#[reflect(Debug, PartialEq, Serialize, Deserialize, Default)]
#[reflect(Debug, PartialEq, Default)]
struct UVec3 {
x: u32,
y: u32,
z: u32,
}
);
impl_reflect_struct!(
#[reflect(Debug, PartialEq, Serialize, Deserialize, Default)]
#[reflect(Debug, PartialEq, Default)]
struct UVec4 {
x: u32,
y: u32,
Expand All @@ -56,30 +56,30 @@ impl_reflect_struct!(
);

impl_reflect_struct!(
#[reflect(Debug, PartialEq, Serialize, Deserialize, Default)]
#[reflect(Debug, PartialEq, Default)]
struct Vec2 {
x: f32,
y: f32,
}
);
impl_reflect_struct!(
#[reflect(Debug, PartialEq, Serialize, Deserialize, Default)]
#[reflect(Debug, PartialEq, Default)]
struct Vec3 {
x: f32,
y: f32,
z: f32,
}
);
impl_reflect_struct!(
#[reflect(Debug, PartialEq, Serialize, Deserialize, Default)]
#[reflect(Debug, PartialEq, Default)]
struct Vec3A {
x: f32,
y: f32,
z: f32,
}
);
impl_reflect_struct!(
#[reflect(Debug, PartialEq, Serialize, Deserialize, Default)]
#[reflect(Debug, PartialEq, Default)]
struct Vec4 {
x: f32,
y: f32,
Expand Down Expand Up @@ -114,22 +114,22 @@ impl_reflect_struct!(
);

impl_reflect_struct!(
#[reflect(Debug, PartialEq, Serialize, Deserialize, Default)]
#[reflect(Debug, PartialEq, Default)]
struct DVec2 {
x: f64,
y: f64,
}
);
impl_reflect_struct!(
#[reflect(Debug, PartialEq, Serialize, Deserialize, Default)]
#[reflect(Debug, PartialEq, Default)]
struct DVec3 {
x: f64,
y: f64,
z: f64,
}
);
impl_reflect_struct!(
#[reflect(Debug, PartialEq, Serialize, Deserialize, Default)]
#[reflect(Debug, PartialEq, Default)]
struct DVec4 {
x: f64,
y: f64,
Expand Down
50 changes: 36 additions & 14 deletions crates/bevy_reflect/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ pub mod __macro_exports {
mod tests {
#[cfg(feature = "glam")]
use ::glam::{vec3, Vec3};
use ::serde::de::DeserializeSeed;
use ::serde::{de::DeserializeSeed, Deserialize, Serialize};
use bevy_utils::HashMap;
use ron::{
ser::{to_string_pretty, PrettyConfig},
Expand All @@ -104,7 +104,7 @@ mod tests {
use super::prelude::*;
use super::*;
use crate as bevy_reflect;
use crate::serde::{ReflectDeserializer, ReflectSerializer};
use crate::serde::{ReflectSerializer, UntypedReflectDeserializer};

#[test]
fn reflect_struct() {
Expand Down Expand Up @@ -451,7 +451,8 @@ mod tests {
h: [u32; 2],
}

#[derive(Reflect)]
#[derive(Reflect, Serialize, Deserialize)]
#[reflect(Serialize, Deserialize)]
struct Bar {
x: u32,
}
Expand All @@ -472,18 +473,23 @@ mod tests {

let mut registry = TypeRegistry::default();
registry.register::<u32>();
registry.register::<isize>();
registry.register::<i8>();
registry.register::<i32>();
registry.register::<usize>();
registry.register::<isize>();
registry.register::<Foo>();
registry.register::<Bar>();
registry.register::<String>();
registry.register::<i8>();
registry.register::<i32>();
registry.register::<Vec<isize>>();
registry.register::<HashMap<usize, i8>>();
registry.register::<(i32, Vec<isize>, Bar)>();
registry.register::<[u32; 2]>();

let serializer = ReflectSerializer::new(&foo, &registry);
let serialized = to_string_pretty(&serializer, PrettyConfig::default()).unwrap();

let mut deserializer = Deserializer::from_str(&serialized).unwrap();
let reflect_deserializer = ReflectDeserializer::new(&registry);
let reflect_deserializer = UntypedReflectDeserializer::new(&registry);
let value = reflect_deserializer.deserialize(&mut deserializer).unwrap();
let dynamic_struct = value.take::<DynamicStruct>().unwrap();

Expand Down Expand Up @@ -952,23 +958,39 @@ bevy_reflect::tests::should_reflect_debug::Test {

let ser = ReflectSerializer::new(&v, &registry);

let result = ron::to_string(&ser).expect("Failed to serialize to string");
let config = PrettyConfig::default()
.new_line(String::from("\n"))
.decimal_floats(true)
.indentor(String::from(" "));
let output = to_string_pretty(&ser, config).unwrap();
let expected = r#"
{
"glam::f32::vec3::Vec3": {
"x": 12.0,
"y": 3.0,
"z": -6.9,
},
}"#;

assert_eq!(
result,
r#"{"type":"glam::f32::vec3::Vec3","struct":{"x":{"type":"f32","value":12.0},"y":{"type":"f32","value":3.0},"z":{"type":"f32","value":-6.9}}}"#
);
assert_eq!(expected, format!("\n{}", output));
}

#[test]
fn vec3_deserialization() {
let data = r#"{"type":"glam::vec3::Vec3","struct":{"x":{"type":"f32","value":12},"y":{"type":"f32","value":3},"z":{"type":"f32","value":-6.9}}}"#;
let data = r#"
{
"glam::f32::vec3::Vec3": {
"x": 12.0,
"y": 3.0,
"z": -6.9,
},
}"#;

let mut registry = TypeRegistry::default();
registry.add_registration(Vec3::get_type_registration());
registry.add_registration(f32::get_type_registration());

let de = ReflectDeserializer::new(&registry);
let de = UntypedReflectDeserializer::new(&registry);

let mut deserializer =
ron::de::Deserializer::from_str(data).expect("Failed to acquire deserializer");
Expand Down
Loading