Skip to content

Commit 4c22c87

Browse files
committed
Update serializer documentation
1 parent abd1d60 commit 4c22c87

File tree

1 file changed

+70
-5
lines changed
  • crates/bevy_reflect/src/serde

1 file changed

+70
-5
lines changed

crates/bevy_reflect/src/serde/ser.rs

+70-5
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,39 @@ fn get_serializable<'a, E: Error>(
5252

5353
/// A general purpose serializer for reflected types.
5454
///
55-
/// The serialized data will take the form of a map containing the following entries:
56-
/// 1. `type`: The _full_ [type path]
57-
/// 2. `value`: The serialized value of the reflected type
55+
/// This is the serializer counterpart to [`ReflectDeserializer`].
5856
///
57+
/// See [`TypedReflectSerializer`] for a serializer that serializes a known type.
58+
///
59+
/// # Output
60+
///
61+
/// This serializer will output a map with a single entry,
62+
/// where the key is the _full_ [type path] of the reflected type
63+
/// and the value is the serialized data.
64+
///
65+
/// # Example
66+
///
67+
/// ```
68+
/// # use bevy_reflect::prelude::*;
69+
/// # use bevy_reflect::{TypeRegistry, serde::ReflectSerializer};
70+
/// #[derive(Reflect, PartialEq, Debug)]
71+
/// #[type_path = "my_crate"]
72+
/// struct MyStruct {
73+
/// value: i32
74+
/// }
75+
///
76+
/// let mut registry = TypeRegistry::default();
77+
/// registry.register::<MyStruct>();
78+
///
79+
/// let input = MyStruct { value: 123 };
80+
///
81+
/// let reflect_serializer = ReflectSerializer::new(&input, &registry);
82+
/// let output = ron::to_string(&reflect_serializer).unwrap();
83+
///
84+
/// assert_eq!(output, r#"{"my_crate::MyStruct":(value:123)}"#);
85+
/// ```
86+
///
87+
/// [`ReflectDeserializer`]: crate::serde::ReflectDeserializer
5988
/// [type path]: crate::TypePath::type_path
6089
pub struct ReflectSerializer<'a> {
6190
pub value: &'a dyn Reflect,
@@ -97,8 +126,44 @@ impl<'a> Serialize for ReflectSerializer<'a> {
97126
}
98127
}
99128

100-
/// A serializer for reflected types whose type is known and does not require
101-
/// serialization to include other metadata about it.
129+
/// A serializer for reflected types whose type will be known during deserialization.
130+
///
131+
/// This is the serializer counterpart to [`TypedReflectDeserializer`].
132+
///
133+
/// See [`ReflectSerializer`] for a serializer that serializes an unknown type.
134+
///
135+
/// # Output
136+
///
137+
/// Since the type is expected to be known during deserialization,
138+
/// this serializer will not output any additional type information,
139+
/// such as the [type path].
140+
///
141+
/// Instead, it will output just the serialized data.
142+
///
143+
/// # Example
144+
///
145+
/// ```
146+
/// # use bevy_reflect::prelude::*;
147+
/// # use bevy_reflect::{TypeRegistry, serde::TypedReflectSerializer};
148+
/// #[derive(Reflect, PartialEq, Debug)]
149+
/// #[type_path = "my_crate"]
150+
/// struct MyStruct {
151+
/// value: i32
152+
/// }
153+
///
154+
/// let mut registry = TypeRegistry::default();
155+
/// registry.register::<MyStruct>();
156+
///
157+
/// let input = MyStruct { value: 123 };
158+
///
159+
/// let reflect_serializer = TypedReflectSerializer::new(&input, &registry);
160+
/// let output = ron::to_string(&reflect_serializer).unwrap();
161+
///
162+
/// assert_eq!(output, r#"(value:123)"#);
163+
/// ```
164+
///
165+
/// [`TypedReflectDeserializer`]: crate::serde::TypedReflectDeserializer
166+
/// [type path]: crate::TypePath::type_path
102167
pub struct TypedReflectSerializer<'a> {
103168
pub value: &'a dyn Reflect,
104169
pub registry: &'a TypeRegistry,

0 commit comments

Comments
 (0)