]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
rust/json: expose more of jansson to rust
authorJason Ish <ish@unx.ca>
Fri, 2 Feb 2018 18:45:35 +0000 (12:45 -0600)
committerVictor Julien <victor@inliniac.net>
Wed, 14 Mar 2018 21:29:39 +0000 (22:29 +0100)
rust/src/json.rs
src/output-json.c
src/output-json.h

index 1119add5c360fc9978f34c38aa2cc3f852c707a6..38ebda3507907125a8a242963ef3dc97865886b5 100644 (file)
@@ -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;
     }
index 2c6291a8e5059952f12fb40f333d90ab23080c73..563f67c7f17110fa803e3700a26e7973f3ed225f 100644 (file)
@@ -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 */
 
index dffb24004d0b4dd890ce933fda800b226754317d..c4b55c61c7ad24743f4a6d0a7a93c7b19e95cdfc 100644 (file)
@@ -67,6 +67,7 @@ typedef struct OutputJsonCtx_ {
 } OutputJsonCtx;
 
 json_t *SCJsonBool(int val);
+void SCJsonDecref(json_t *js);
 
 #endif /* HAVE_LIBJANSSON */