mrubyedge_serde_json/
lib.rs1pub mod json_value;
2
3use std::rc::Rc;
4
5use mrubyedge::{
6 Error,
7 yamrb::{helpers::mrb_define_class_cmethod, value::RObject, vm::VM},
8};
9
10pub fn init_json(vm: &mut VM) {
11 if vm.get_const_by_name("JSON").is_some() {
12 return;
13 }
14
15 let json_class = vm.define_class("JSON", None, None);
16
17 mrb_define_class_cmethod(
18 vm,
19 json_class.clone(),
20 "dump",
21 Box::new(mrb_json_class_dump),
22 );
23 mrb_define_class_cmethod(
24 vm,
25 json_class.clone(),
26 "generate",
27 Box::new(mrb_json_class_dump),
28 );
29 mrb_define_class_cmethod(
30 vm,
31 json_class.clone(),
32 "load",
33 Box::new(mrb_json_class_load),
34 );
35 mrb_define_class_cmethod(
36 vm,
37 json_class.clone(),
38 "parse",
39 Box::new(mrb_json_class_load),
40 );
41}
42
43pub fn mrb_json_class_dump(vm: &mut VM, args: &[Rc<RObject>]) -> Result<Rc<RObject>, Error> {
44 if args.len() != 1 {
45 return Err(Error::ArgumentError(
46 "wrong number of arguments".to_string(),
47 ));
48 }
49 let result = json_value::mrb_json_dump(vm, args[0].clone())?;
50 Ok(result)
51}
52
53pub fn mrb_json_class_load(vm: &mut VM, args: &[Rc<RObject>]) -> Result<Rc<RObject>, Error> {
54 if args.len() != 1 {
55 return Err(Error::ArgumentError(
56 "wrong number of arguments".to_string(),
57 ));
58 }
59 let json_str: String = args[0].as_ref().try_into()?;
60 let result = json_value::mrb_json_load(vm, json_str)?;
61 Ok(result.get_inner())
62}