examples/reflection/reflection_ser_std_string.rs
1use crate::z_ignore_test_common::*;
2
3use flecs_ecs::prelude::*;
4
5// This example shows how to serialize a component with an std::string
6
7// std::String is already registered in the Rust Meta framework (unlike in CPP)
8// in case you wish to see how, find it in `src/addons/meta/builtin.rs`
9// thus serializing components with String is already supported out of the box
10
11#[derive(Component, Debug)]
12#[flecs(meta)]
13struct StringComponent {
14 a: String,
15 b: String,
16}
17
18fn main() {
19 let world = World::new();
20
21 // Create value & serialize it
22 let mut v = StringComponent {
23 a: "foo".to_string(),
24 b: "bar".to_string(),
25 };
26
27 println!("{:?}", world.to_json::<StringComponent>(&v));
28
29 // Deserialize new strings into value
30 world.from_json::<StringComponent>(&mut v, "{\"a\": \"hello\", \"b\": \"world\"}", None);
31 println!("{v:?}");
32
33 // Output:
34 // {"a": "foo", "b": "bar"}
35 // {a: "hello", b: "world"}
36}
37
38#[cfg(feature = "flecs_nightly_tests")]
39#[test]
40fn test() {
41 let output_capture = OutputCapture::capture().unwrap();
42 main();
43 output_capture.test("reflection_ser_std_string".to_string());
44}