#neon #serialization #fork #object #convert #deserialize #neon-serde

neon-serde2

简化对象序列化,用于与 neon 配合使用,neon-serde 的分支

1 个不稳定版本

使用旧的 Rust 2015

0.8.0 2021 年 7 月 20 日

#1420 in 编码

Download history 10/week @ 2024-03-10 29/week @ 2024-03-31 1/week @ 2024-04-07

每月 609 次下载

MIT 许可证

75KB
1K SLoC

Rust 1K SLoC // 0.0% comments JavaScript 189 SLoC Shell 27 SLoC // 0.1% comments

Neon-serde

这是官方 neon-serde 项目的分支。该项目变得过时,并停止跟踪 neon 的版本。

此 crate 是一个工具,用于轻松地在以下类型之间转换值

A Handle<JsValue> 来自 neon crate 和任何实现 serde::{Serialize, Deserialize}

版本支持

neon-serde 在 node 8 10 12 上进行了测试

用法

neon_serde::from_value

Handle<js::JsValue> 转换为实现 serde::Deserialize 的类型

neon_serde::to_value˚

将实现 serde::Serialize 的值转换为 Handle<JsValue>

导出宏示例

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;
extern crate serde_bytes;

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

export! {

    /// Say hello based on a persons name
    fn say_hello(name: String) -> String {
        format!("Hello, {}!", name)
    }

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

    /// Say how old someone is, if they exist
    fn maybe_say_hello(user: Option<User>) -> Option<String> {
        user.map(greet)
    }

    /// Sorts the bytes in a string
    /// use `serde_bytes::ByteBuf` to return a `Buffer` in node
    /// a `Vec<u8>` will be an array
    fn sort_utf8_bytes(str: String) -> serde_bytes::ByteBuf {
        let mut bytes = str.into_bytes();
        bytes.sort();
        serde_bytes::ByteBuf::from(bytes)
    }

    /// using `serde_bytes::ByteBuf` will make passing an array
    /// of numbers an error
    ///
    /// note: `-> ()` is NOT optional
    fn expect_buffer_only(_buff: serde_bytes::ByteBuf) -> () {
        // code
    }

    /// using `Vec<u8>` not accept a buffer
    fn expect_array(_buff: Vec<u8>) -> () {
        // code
    }

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

直接使用示例

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 = match neon_serde::from_value(&mut cx, arg0) {
        Ok(value) => value,
        Err(e) => {
            return cx.throw_error(e.to_string());
        }
    };
    println!("{:?}", arg0_value);

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

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

    neon_serde::to_value(&mut cx, &value)
        .or_else(|e| cx.throw_error(e.to_string()))
}

限制

数据所有权

所有 Deserialize Values 必须拥有其所有数据(它们必须具有 serde::DererializeOwned 特性)

依赖

~4–5.5MB
~106K SLoC