3 releases

Uses old Rust 2015

0.3.2 Apr 16, 2020
0.3.1 Apr 1, 2018
0.3.0 Mar 19, 2018

#1091 in Encoding

MIT license

400KB
7.5K SLoC

Serde support for Sciter engine.

While technically you could just use the serde_json crate and perform serialization via an intermediate string (something like sciter::Value::from_str(&serde_json::to_string(<your data>)?)?), you can also use direct serialization between your data and sciter::Value.

Supported types of Sciter value

  • Bool (bool)
  • Integer (i8-i32)
  • Float (f32-f64)
  • String (&str, String)
  • Bytes (&[u8])
  • Array (&[T], Vec<T>)
  • Object (key-value mapping like struct or HashMap, BTreeMap, etc.)

Unsupported:

  • Date
  • Currency
  • Length
  • Range
  • Duration
  • Angle
  • Color

Supported types of the Serde data model

  • bool
  • integer types except the following:
  • i64/u64 - 64-bit integers stored as f64 in Sciter
  • strings
  • byte arrays
  • option
  • unit (stored as null)
  • unit struct (stored as null)
  • unit variant (aka enum, stored just as enum index of i32 type)
  • newtype struct (aka struct Io(u32), stored as underlaying value)
  • newtype variant
  • seq, like vector (stored as array)
  • tuple (stored as array)
  • tuple struct (stored as array)
  • tuple variant
  • map (stored as map)
  • struct (stored as map)
  • struct variant

See the Serde data model for reference.

Examples

extern crate sciter;
extern crate sciter_serde;

use sciter::Value;
use sciter_serde::{from_value, to_value};

fn back_and_forth() {
let v: Value = to_value(&true).unwrap();
let b: bool = from_value(&v).unwrap();
assert_eq!(b, true);
}

fn main() {

// bool
let v: Value = to_value(&true).unwrap();
assert!(v.is_bool());
assert_eq!(v, Value::from(true));

// numbers
let v = to_value(&12u32).unwrap();
assert_eq!(v, 12.into());

let v = to_value(& 42.0f64).unwrap();
assert_eq!(v, 42.0f64.into());

// strings
let v = to_value("hello").unwrap();
assert_eq!(v, "hello".into());

// arrays
let a = [1,2,3];
let v = to_value(&a).unwrap();
assert_eq!(v, a.iter().cloned().collect());

// maps
let m = {
use std::collections::BTreeMap;
let mut m = BTreeMap::new();
m.insert("17", 17);
m.insert("42", 42);
m
};
let v = to_value(&m).unwrap();
assert_eq!(v, Value::parse(r#"{ "17": 17, "42": 42 }"#).unwrap());
}

With derived serialization:

#[macro_use]
extern crate serde_derive;
extern crate serde;

extern crate sciter;
extern crate sciter_serde;

use sciter::Value;
use sciter_serde::to_value;

fn main() {

// structs
#[derive(Serialize)]
struct Test {
x: i32,
y: i32,
}

let v = to_value(&Test {x: 1, y: 2}).unwrap();
assert_eq!(v, Value::parse(r#"{ "x": 1, "y": 2 }"#).unwrap());
}

Dependencies

~110–410KB