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
45
46
47
48
49
50
51
52
53
54
55
56
57
extern crate rustc_serialize;
mod parsing;
#[cfg(test)]
mod standart_tests;
#[cfg(test)]
mod weak_tests;
use parsing::{Builder};
use rustc_serialize::Decodable;
use rustc_serialize::json::Json;
use rustc_serialize::json::Decoder;
use rustc_serialize::json::DecoderError;
use rustc_serialize::json::ParserError;
use rustc_serialize::json::BuilderError;
use rustc_serialize::json::ErrorCode;
use std::{str, io};
pub fn decode<T: Decodable>(s: &str) -> Result<T, DecoderError> {
let json = match from_str(s) {
Ok(x) => x,
Err(e) => return Err(DecoderError::ParseError(e))
};
let mut decoder = Decoder::new(json);
Decodable::decode(&mut decoder)
}
pub fn from_reader(rdr: &mut io::Read) -> Result<Json, BuilderError> {
let contents = {
let mut c = Vec::new();
match rdr.read_to_end(&mut c) {
Ok(_) => (),
Err(e) => return Err(ParserError::IoError(e))
}
c
};
let s = match str::from_utf8(&contents).ok() {
Some(s) => s,
_ => return Err(ParserError::SyntaxError(ErrorCode::NotUtf8, 0, 0))
};
let mut builder = Builder::new(s.chars());
builder.build()
}
pub fn from_str(s: &str) -> Result<Json, BuilderError> {
let mut builder = Builder::new(s.chars());
builder.build()
}