neon-serde 0.0.3

Easily serialize object for use with neon
docs.rs failed to build neon-serde-0.0.3
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.

Neon-serde

Build Status

This crate is a utility to easily convert values between

A Handle<JsValue> from the https://github.com/neon-bindings/neon crate and any value implementing serde::{Serialize, Deserialize}

Versions support

neon-serde targets node >= 6.0 Uint8ClampedArray only works on node >= 8

Usage

neon_serde::from_handle

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>

Export Macro example

The export! macro allows you to quickly define functions automatically convert thier arguments


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

#[derive(Deserialize)]
struct User {
    name: String,
    age: u16,
}

export! {
    fn say_hello(name: String) -> String {
        format!("Hello, {}!", name)
    }

    fn greet(user: User) -> String {
        format!("{} is {} years old", user.name, user.age)
    }

    fn fibonacci(n: i32) -> i32 {
        match n {
            1 | 2 => 1,
            n => fibonacci(n - 1) + fibonacci(n - 2)
        }
    }
}

Direct Usage Example

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

use neon::js::{JsValue, JsUndefined};
use neon::vm::{Call, JsResult};

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

fn deserialize_something(call: Call) -> JsResult<JsValue> {
    let scope = call.scope;
    let arg0 = call.arguments
         .require(scope, 0)?
         .check::<JsValue>()?;

    let arg0_value :AnObject = neon_serde::from_handle(arg0, scope)?;
    println!("{:?}", arg0_value);

    Ok(JsUndefined::new().upcast())
}

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

    let js_value = neon_serde::to_value(&value, scope)?;
    Ok(js_value)
}

Limitations

Data ownership

All Deserialize Values must own all thier data (they must have the trait serde::DererializeOwned)