From 57d9574839f089624a6c5ed39a754ca3011c974c Mon Sep 17 00:00:00 2001 From: Jason Ish Date: Fri, 2 Feb 2018 12:45:35 -0600 Subject: [PATCH] rust/json: expose more of jansson to rust --- rust/src/json.rs | 17 +++++++++++++++++ src/output-json.c | 9 +++++++++ src/output-json.h | 1 + 3 files changed, 27 insertions(+) diff --git a/rust/src/json.rs b/rust/src/json.rs index 1119add5c3..38ebda3507 100644 --- a/rust/src/json.rs +++ b/rust/src/json.rs @@ -35,6 +35,7 @@ extern { fn json_string(value: *const c_char) -> *mut JsonT; fn json_integer(val: u64) -> *mut JsonT; + fn SCJsonDecref(value: *mut JsonT); fn SCJsonBool(val: bool) -> *mut JsonT; } @@ -44,6 +45,10 @@ pub struct Json { impl Json { + pub fn decref(val: Json) { + unsafe{SCJsonDecref(val.js)}; + } + pub fn object() -> Json { return Json{ js: unsafe{json_object()}, @@ -56,6 +61,18 @@ impl Json { } } + pub fn string(val: &str) -> Json { + return Json{ + js: unsafe{json_string(to_cstring(val.as_bytes()).as_ptr())} + }; + } + + pub fn string_from_bytes(val: &[u8]) -> Json { + return Json{ + js: unsafe{json_string(to_cstring(val).as_ptr())} + }; + } + pub fn unwrap(&self) -> *mut JsonT { return self.js; } diff --git a/src/output-json.c b/src/output-json.c index 2c6291a8e5..563f67c7f1 100644 --- a/src/output-json.c +++ b/src/output-json.c @@ -108,6 +108,15 @@ json_t *SCJsonBool(int val) return (val ? json_true() : json_false()); } +/** + * Wrap json_decref. This is mainly to expose this function to Rust as its + * defined in the Jansson header file as an inline function. + */ +void SCJsonDecref(json_t *json) +{ + json_decref(json); +} + /* Default Sensor ID value */ static int64_t sensor_id = -1; /* -1 = not defined */ diff --git a/src/output-json.h b/src/output-json.h index dffb24004d..c4b55c61c7 100644 --- a/src/output-json.h +++ b/src/output-json.h @@ -67,6 +67,7 @@ typedef struct OutputJsonCtx_ { } OutputJsonCtx; json_t *SCJsonBool(int val); +void SCJsonDecref(json_t *js); #endif /* HAVE_LIBJANSSON */