RustでEnumのプロパティがあるstructをシリアライズしたりデシリアライズする
RustでEnumのプロパティがあるstructをシリアライズしたりデシリアライズする方法です。 Enumにすることでパターンマッチと相性が良いの良いですよね。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "swiswiswift" | |
version = "0.1.0" | |
edition = "2018" | |
[dependencies] | |
serde = { version = "1.0", features = ["derive"] } | |
serde_json = "1.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use serde::{Serialize, Deserialize}; | |
#[derive(Serialize, Deserialize, Debug)] | |
enum PokemonType { | |
Normal, | |
Fire, | |
Water, | |
} | |
#[derive(Serialize, Deserialize, Debug)] | |
struct Pokemon { | |
id: i32, | |
name: String, | |
pokemon_type: PokemonType, | |
} | |
fn main() { | |
let pokemon = Pokemon { id: 143, name: String::from("Snorlax"), pokemon_type: PokemonType::Normal }; | |
let serialized = serde_json::to_string(&pokemon).unwrap(); | |
println!("serialized = {}", serialized); | |
let deserialized: Pokemon = serde_json::from_str(&serialized).unwrap(); | |
println!("deserialized = {:?}", deserialized); | |
} |