Crate neon_serde2

Source
Expand description

§Neon-serde

This crate is a utility to easily convert values between

A Handle<JsValue> from the neon crate and any value implementing serde::{Serialize, Deserialize}

§Usage

§neon_serde::from_value

Convert a Handle<js::JsValue> to a type implementing serde::Deserialize

§neon_serde::to_value

Convert a value implementing serde::Serialize to a Handle<JsValue>

§Example

extern crate neon_serde;
extern crate neon;
#[macro_use]
extern crate serde_derive;

use neon::prelude::*;

#[derive(Serialize, Debug, Deserialize)]
struct AnObject {
    a: u32,
    b: Vec<f64>,
    c: String,
}

fn deserialize_something(mut cx: FunctionContext) -> JsResult<JsValue> {
    let arg0 = cx.argument::<JsValue>(0)?;

    let arg0_value :AnObject = neon_serde::from_value(&mut cx, arg0)
        .or_else(|e| cx.throw_error(e.to_string()))
        .unwrap();
    println!("{:?}", arg0_value);

    Ok(JsUndefined::new(&mut cx).upcast())
}

fn serialize_something(mut cx: FunctionContext) -> JsResult<JsValue> {
    let value = AnObject {
        a: 1,
        b: vec![2f64, 3f64, 4f64],
        c: "a string".into()
    };

    let js_value = neon_serde::to_value(&mut cx, &value)
        .or_else(|e| cx.throw_error(e.to_string()))
        .unwrap();
    Ok(js_value)
}

Re-exports§

Modules§

  • Deserialize a JsValue into a Rust data structure
  • Defines error handling types used by the create uses the error-chain create for generation
  • Serialize a Rust data structure into a JsValue

Macros§