Skip to content

Instantly share code, notes, and snippets.

@polachok
Created April 16, 2016 15:07
Show Gist options
  • Save polachok/1fd5fb24e051e2217732a4fe63936832 to your computer and use it in GitHub Desktop.
Save polachok/1fd5fb24e051e2217732a4fe63936832 to your computer and use it in GitHub Desktop.
t
extern crate rustc_serialize;
use rustc_serialize::json;
#[derive(RustcDecodable, RustcEncodable, Debug)]
enum Message {
Type1(String),
Type2(u64),
}
fn main() {
let msg1 = Message::Type1("Hello world".to_string());
let msg2 = Message::Type2(5);
// Serialize using `json::encode`
let encoded = json::encode(&msg1).unwrap();
// Deserialize using `json::decode`
let decoded: Message = json::decode(&encoded).unwrap();
println!("encoded: {}, decoded: {:?}", encoded, decoded);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment