Skip to content

Instantly share code, notes, and snippets.

@selfup
Created May 29, 2016 22:44
Show Gist options
  • Save selfup/cd49df8ed480682146448785fb07b1e0 to your computer and use it in GitHub Desktop.
Save selfup/cd49df8ed480682146448785fb07b1e0 to your computer and use it in GitHub Desktop.
extern crate rustc_serialize;
extern crate stringer;
use rustc_serialize::json::{self};
use std::collections::HashMap;
use std::ffi::CString;
use std::os::raw::c_char;
use stringer::*;
#[no_mangle]
pub extern fn read_json(ro: *const c_char) -> *const c_char {
let roo = make_string(ro);
let json_for_rust: HashMap<String, i32> = json::decode(&roo).unwrap();
let encoded = json::encode(&json_for_rust).unwrap();
turn_into_null_string(encoded)
}
#[test]
fn it_reads_the_json() {
let string = CString::new("{\"wow\":\"6\"}").unwrap().into_raw();
let ex_st = "{\"wow\":\"6\"}".to_string();
let exp = turn_into_null_string(ex_st);
let expected1 = make_string(exp);
let expected2 = make_string(read_json(string));
assert_eq!(expected1, "{\"wow\":\"6\"}");
assert_eq!(expected2, "{\"wow\":6}");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment