Skip to content

Commit

Permalink
added files
Browse files Browse the repository at this point in the history
  • Loading branch information
auralshin committed May 11, 2023
1 parent 779555d commit d7463af
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "rust_json_parser"
version = "0.1.0"
edition = "2021"


[lib]
name = "mylib"
crate-type = ["cdylib"]


[dependencies]
serde_json = "1.0"

[profile.release]
lto = true
codegen-units = 1
26 changes: 26 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use serde_json::Value;
use std::ffi::{CStr, CString};
use std::os::raw::c_char;

#[no_mangle]
pub extern "C" fn parse_json(input: *const c_char) -> *mut c_char {
let c_str = unsafe {
assert!(!input.is_null());

CStr::from_ptr(input)
};

let input_str = match c_str.to_str() {
Ok(str) => str,
Err(_) => return CString::new("").unwrap().into_raw(),
};

let result = serde_json::from_str::<Value>(input_str);

match result {
Ok(data) => CString::new(format!("{:?}", data)).unwrap().into_raw(),
Err(err) => CString::new(format!("Failed to parse JSON: {}", err))
.unwrap()
.into_raw(),
}
}

0 comments on commit d7463af

Please sign in to comment.