napi-rs/examples/napi/src/serde.rs

39 lines
903 B
Rust
Raw Normal View History

2021-09-28 01:01:19 +09:00
use napi::bindgen_prelude::*;
use serde_json::{Map, Value};
use std::fs;
#[napi(object)]
#[derive(Serialize, Deserialize, Debug)]
/// This is an interface for package.json
2021-09-28 01:01:19 +09:00
struct PackageJson {
pub name: String,
/// The version of the package
2021-09-28 01:01:19 +09:00
pub version: String,
pub dependencies: Option<Map<String, Value>>,
#[serde(rename = "devDependencies")]
pub dev_dependencies: Option<Map<String, Value>>,
}
#[napi]
fn read_package_json() -> Result<PackageJson> {
let raw = fs::read_to_string("package.json")?;
let p: PackageJson = serde_json::from_str(&raw)?;
Ok(p)
}
#[napi]
fn get_package_json_name(package_json: PackageJson) -> String {
package_json.name
}
#[napi]
fn test_serde_roundtrip(data: Value) -> Value {
data
}
#[napi]
fn test_serde_big_number_precision(number: String) -> Value {
let data = format!("{{\"number\":{}}}", number);
serde_json::from_str(&data).unwrap()
}