Skip to main content

json_embedded/
json_embedded.rs

1//! Embedded Compatibility Test Example
2//!
3//! This example validates that babbel_json can be built and used for embedded systems.
4
5use babbel_json::embedded::{ArrayBuilder, ObjectBuilder, sensor};
6use babbel_json::{BufferDestination, BufferSource, ParserConfig, parse_with_config, stringify};
7
8pub fn test_sensor_reading() {
9    let _reading = sensor::simple_reading("temp_01", 23.5, 1234567890);
10}
11
12pub fn test_object_builder() {
13    let _config = ObjectBuilder::with_capacity(3)
14        .add_str("device", "sensor_01")
15        .add_i32("rate", 1000)
16        .add_bool("enabled", true)
17        .build();
18}
19
20pub fn test_array_builder() {
21    let _arr = ArrayBuilder::with_capacity(5)
22        .add_i32(1)
23        .add_i32(2)
24        .add_i32(3)
25        .add_i32(4)
26        .add_i32(5)
27        .build();
28}
29
30pub fn test_parsing() {
31    let json = br#"{"id":"sensor_01","value":23.5}"#;
32    let config = ParserConfig::strict();
33    let mut source = BufferSource::new(json);
34    let _ = parse_with_config(&mut source, &config);
35}
36
37pub fn test_stringify() {
38    let node = ObjectBuilder::new().add_str("test", "value").build();
39    let mut dest = BufferDestination::new();
40    let _ = stringify(&node, &mut dest);
41}
42
43pub fn test_batch_readings() {
44    let mut readings = Vec::new();
45    for i in 0..10 {
46        readings.push(sensor::simple_reading("sensor", 23.5, 1234567890 + i));
47    }
48    let _batch = sensor::batch_readings("device_01", readings);
49}
50
51fn main() {
52    println!("Testing embedded utilities...");
53    test_sensor_reading();
54    test_object_builder();
55    test_array_builder();
56    test_parsing();
57    test_stringify();
58    test_batch_readings();
59    println!("Embedded compatibility tests passed!");
60}