Skip to main content

sciter_serde/
lib.rs

1// This component uses Sciter Engine,
2// copyright Terra Informatica Software, Inc.
3// (http://terrainformatica.com/).
4
5/*!
6
7[Serde](https://docs.rs/serde) support for [Sciter](https://docs.rs/sciter-rs) engine.
8
9While technically you could just use the `serde_json` crate and perform serialization via
10an intermediate string (something like `sciter::Value::from_str(&serde_json::to_string(<your data>)?)?`),
11you can also use direct serialization between your data and `sciter::Value`.
12
13## Supported types of Sciter value
14
15+ Bool (`bool`)
16+ Integer (`i8`-`i32`)
17+	Float (`f32`-`f64`)
18+ String (`&str`, `String`)
19+ Bytes (`&[u8]`)
20+ Array (`&[T]`, `Vec<T>`)
21+ Object (key-value mapping like `struct` or `HashMap`, `BTreeMap`, etc.)
22
23Unsupported:
24
25- Date
26- Currency
27- Length
28- Range
29- Duration
30- Angle
31- Color
32
33## Supported types of the Serde data model
34
35* [x] `bool`
36* [x] integer types except the following:
37* [-] `i64`/`u64` - 64-bit integers stored as `f64` in Sciter
38* [x] strings
39* [x] byte arrays
40* [x] option
41* [x] unit (stored as `null`)
42* [x] unit struct (stored as `null`)
43* [x] unit variant (aka `enum`, stored just as enum index of `i32` type)
44* [x] newtype struct (aka `struct Io(u32)`, stored as underlaying value)
45* [-] newtype variant
46* [x] seq, like vector (stored as array)
47* [x] tuple (stored as array)
48* [x] tuple struct (stored as array)
49* [-] tuple variant
50* [x] map (stored as map)
51* [x] struct (stored as map)
52* [-] struct variant
53
54See the [Serde data model](https://serde.rs/data-model.html) for reference.
55
56# Examples
57
58```rust
59extern crate sciter;
60extern crate sciter_serde;
61
62use sciter::Value;
63use sciter_serde::{from_value, to_value};
64
65fn back_and_forth() {
66	let v: Value = to_value(&true).unwrap();
67	let b: bool = from_value(&v).unwrap();
68	assert_eq!(b, true);
69}
70
71fn main() {
72
73	// bool
74	let v: Value = to_value(&true).unwrap();
75	assert!(v.is_bool());
76	assert_eq!(v, Value::from(true));
77
78	// numbers
79	let v = to_value(&12u32).unwrap();
80	assert_eq!(v, 12.into());
81
82	let v = to_value(& 42.0f64).unwrap();
83	assert_eq!(v, 42.0f64.into());
84
85	// strings
86	let v = to_value("hello").unwrap();
87	assert_eq!(v, "hello".into());
88
89	// arrays
90	let a = [1,2,3];
91	let v = to_value(&a).unwrap();
92	assert_eq!(v, a.iter().cloned().collect());
93
94	// maps
95	let m = {
96		use std::collections::BTreeMap;
97		let mut m = BTreeMap::new();
98		m.insert("17", 17);
99		m.insert("42", 42);
100		m
101	};
102	let v = to_value(&m).unwrap();
103	assert_eq!(v, Value::parse(r#"{ "17": 17, "42": 42 }"#).unwrap());
104}
105```
106
107With derived serialization:
108
109```rust
110# #![doc(test(no_crate_inject))]
111#[macro_use]
112extern crate serde_derive;
113extern crate serde;
114
115extern crate sciter;
116extern crate sciter_serde;
117
118use sciter::Value;
119use sciter_serde::to_value;
120
121fn main() {
122
123	// structs
124	#[derive(Serialize)]
125	struct Test {
126		x: i32,
127		y: i32,
128	}
129
130	let v = to_value(&Test {x: 1, y: 2}).unwrap();
131	assert_eq!(v, Value::parse(r#"{ "x": 1, "y": 2 }"#).unwrap());
132}
133
134```
135
136*/
137#![allow(clippy::redundant_field_names)]
138#![allow(clippy::tabs_in_doc_comments)]
139
140#[macro_use]
141extern crate serde;
142extern crate sciter;
143
144
145mod error;
146mod ser;
147mod de;
148
149#[doc(inline)]
150pub use ser::to_value;
151
152#[doc(inline)]
153pub use de::from_value;
154
155pub use error::{Result, Error};