1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
extern crate rand;
extern crate serde;
extern crate serde_json;

#[macro_use]
extern crate serde_derive;

pub mod context;
pub mod ea;
pub mod ga;
pub mod math;
pub mod ne;
pub mod neproblem;
pub mod neuro;
pub mod problem;
pub mod result;
pub mod settings;

use serde::de::{DeserializeOwned};
use serde::ser::{Serialize};
use std::fs::File;
use std::io::{BufReader, Read, Write};


trait Jsonable: Clone+Serialize {
    type T: Clone+DeserializeOwned+Serialize;

    fn from_json(filename: &str) -> Self::T {
        let file = File::open(filename).expect("Can not open file");
        let mut buf_reader = BufReader::new(file);
        let mut json_str = String::new();
        buf_reader.read_to_string(&mut json_str).expect("Can not read file contents");

        let res: Self::T = serde_json::from_str(&json_str).expect("Can not deserialize from json to EAResult");
        res.clone()
    }

    fn to_json(&self, filename: &str) {
        let mut file = File::create(&filename).expect("Can not open file");
        let json_str = serde_json::to_string(&self).expect("Can not serialize to json from EAResult");
        file.write_all(json_str.as_bytes()).expect("Can not write to file");
    }
}