prebindgen_jni_runtime/
string_helpers.rs1use jni::{
4 objects::{JObject, JString},
5 JNIEnv,
6};
7
8pub fn decode_string(env: &mut JNIEnv, string: &JString) -> Result<String, String> {
12 let binding = env
13 .get_string(string)
14 .map_err(|err| format!("Error while retrieving JString: {}", err))?;
15 let value = binding
16 .to_str()
17 .map_err(|err| format!("Error decoding JString: {}", err))?;
18 Ok(value.to_string())
19}
20
21pub fn encode_string<'local, S: AsRef<str>>(
23 env: &mut JNIEnv<'local>,
24 string: S,
25) -> Result<JString<'local>, String> {
26 env.new_string(string.as_ref())
27 .map_err(|err| format!("Error while encoding JString: {}", err))
28}
29
30pub fn null_string() -> JString<'static> {
32 JString::from(JObject::null())
33}