1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
//! The native `json` module for the [Rune Language].
//!
//! [Rune Language]: https://rune-rs.github.io
//!
//! ## Usage
//!
//! Add the following to your `Cargo.toml`:
//!
//! ```toml
//! rune-modules = { version = "0.13.2", features = ["json"] }
//! ```
//!
//! Install it into your context:
//!
//! ```rust
//! let mut context = rune::Context::with_default_modules()?;
//! context.install(rune_modules::json::module(true)?)?;
//! # Ok::<_, rune::support::Error>(())
//! ```
//!
//! Use it in Rune:
//!
//! ```rust,ignore
//! use json;
//!
//! fn main() {
//! let data = json::from_string("{\"key\": 42}");
//! dbg(data);
//! }
//! ```
use rune::{ContextError, Module, vm_write, Any};
use rune::runtime::{Bytes, Value, Formatter};
use rune::alloc::{Vec, String};
use rune::alloc::fmt::TryWrite;
#[rune::module(::json)]
/// Module for processing JSON.
///
/// # Examples
///
/// ```rune
/// let object = #{"number": 42, "string": "Hello World"};
/// let object = json::from_string(json::to_string(object)?)?;
/// assert_eq!(object, #{"number": 42, "string": "Hello World"});
/// ```
pub fn module(_stdio: bool) -> Result<Module, ContextError> {
let mut module = Module::from_meta(self::module_meta)?;
module.ty::<Error>()?;
module.function_meta(Error::display)?;
module.function_meta(Error::debug)?;
module.function_meta(from_bytes)?;
module.function_meta(from_string)?;
module.function_meta(to_string)?;
module.function_meta(to_bytes)?;
Ok(module)
}
#[derive(Any)]
#[rune(item = ::json)]
/// Error type raised during JSON serialization.
struct Error {
error: serde_json::Error,
}
impl Error {
#[rune::function(vm_result, protocol = STRING_DISPLAY)]
pub(crate) fn display(&self, f: &mut Formatter) {
vm_write!(f, "{}", self.error);
}
#[rune::function(vm_result, protocol = STRING_DEBUG)]
pub(crate) fn debug(&self, f: &mut Formatter) {
vm_write!(f, "{:?}", self.error);
}
}
impl From<serde_json::Error> for Error {
fn from(error: serde_json::Error) -> Self {
Self { error }
}
}
/// Convert JSON bytes into a rune value.
///
/// # Examples
///
/// ```rune
/// let object = json::from_bytes(b"{\"number\": 42, \"string\": \"Hello World\"}")?;
/// assert_eq!(object, #{"number": 42, "string": "Hello World"});
/// ```
#[rune::function]
fn from_bytes(bytes: &[u8]) -> Result<Value, Error> {
Ok(serde_json::from_slice(bytes)?)
}
/// Convert a JSON string into a rune value.
///
/// # Examples
///
/// ```rune
/// let object = json::from_string("{\"number\": 42, \"string\": \"Hello World\"}")?;
/// assert_eq!(object, #{"number": 42, "string": "Hello World"});
/// ```
#[rune::function]
fn from_string(string: &str) -> Result<Value, Error> {
Ok(serde_json::from_str(string)?)
}
/// Convert any value to a json string.
///
/// # Examples
///
/// ```rune
/// let object = #{"number": 42, "string": "Hello World"};
/// let object = json::from_string(json::to_string(object)?)?;
/// assert_eq!(object, #{"number": 42, "string": "Hello World"});
/// ```
#[rune::function(vm_result)]
fn to_string(value: Value) -> Result<String, Error> {
Ok(String::try_from(serde_json::to_string(&value)?).vm?)
}
/// Convert any value to json bytes.
///
/// # Examples
///
/// ```rune
/// let object = #{"number": 42, "string": "Hello World"};
/// let object = json::from_bytes(json::to_bytes(object)?)?;
/// assert_eq!(object, #{"number": 42, "string": "Hello World"});
/// ```
#[rune::function(vm_result)]
fn to_bytes(value: Value) -> Result<Bytes, Error> {
Ok(Bytes::from_vec(Vec::try_from(serde_json::to_vec(&value)?).vm?))
}