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