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
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#![allow(unused_must_use)]

use formatter::Formatter;
use std::io;
use std::io::Write;
use std::str;
use serialize::json;
use types::EncodingType;
use super::write_str;

pub struct JSON {
    out: Box<Write+'static>,
    is_first_db: bool,
    has_databases: bool,
    is_first_key_in_db: bool,
    elements_in_key: u32,
    element_index: u32
}

impl JSON {
    pub fn new() -> JSON {
        let out = Box::new(io::stdout());
        JSON {
            out: out,
            is_first_db: true,
            has_databases: false,
            is_first_key_in_db: true,
            elements_in_key: 0,
            element_index: 0
        }
    }
}

fn encode_to_ascii(value: &[u8]) -> String {
    let s = unsafe{str::from_utf8_unchecked(value)};
    json::encode(&s).unwrap()
}

impl JSON {
    fn start_key(&mut self, length: u32) {
        if !self.is_first_key_in_db {
            write_str(&mut self.out, ",");
        }

        self.is_first_key_in_db = false;
        self.elements_in_key = length;
        self.element_index = 0;
    }

    fn end_key(&mut self) { }

    fn write_comma(&mut self) {
        if self.element_index > 0 {
            write_str(&mut self.out, ",");
        }
        self.element_index += 1;
    }

    fn write_key(&mut self, key: &[u8]) {
        self.out.write_all(encode_to_ascii(key).as_bytes());
    }
    fn write_value(&mut self, value: &[u8]) {
        self.out.write_all(encode_to_ascii(value).as_bytes());
    }
}

impl Formatter for JSON {
    fn start_rdb(&mut self) {
        write_str(&mut self.out, "[");
    }

    fn end_rdb(&mut self) {
        if self.has_databases {
            write_str(&mut self.out, "}");
        }
        write_str(&mut self.out, "]\n");
    }

    fn start_database(&mut self, _db_number: u32) {
        if !self.is_first_db {
            write_str(&mut self.out, "},");
        }

        write_str(&mut self.out, "{");
        self.is_first_db = false;
        self.has_databases = true;
        self.is_first_key_in_db = true;
    }

    fn set(&mut self, key: &[u8], value: &[u8], _expiry: Option<u64>) {
        self.start_key(0);
        self.write_key(key);
        write_str(&mut self.out, ":");
        self.write_value(value);
    }

    fn start_hash(&mut self, key: &[u8], length: u32,
                  _expiry: Option<u64>, _info: EncodingType) {
        self.start_key(length);
        self.write_key(key);
        write_str(&mut self.out, ":{");
        self.out.flush();
    }

    fn end_hash(&mut self, _key: &[u8]) {
        self.end_key();
        write_str(&mut self.out, "}");
        self.out.flush();
    }

    fn hash_element(&mut self, _key: &[u8], field: &[u8], value: &[u8]) {
        self.write_comma();
        self.write_key(field);
        write_str(&mut self.out, ":");
        self.write_value(value);
        self.out.flush();
    }

    fn start_set(&mut self, key: &[u8], cardinality: u32,
                 _expiry: Option<u64>, _info: EncodingType) {
        self.start_key(cardinality);
        self.write_key(key);
        write_str(&mut self.out, ":[");
        self.out.flush();
    }

    fn end_set(&mut self, _key: &[u8]) {
        self.end_key();
        write_str(&mut self.out, "]");
    }

    fn set_element(&mut self, _key: &[u8], member: &[u8]) {
        self.write_comma();
        self.write_value(member);
    }

    fn start_list(&mut self, key: &[u8], length: u32,
                  _expiry: Option<u64>, _info: EncodingType) {
        self.start_key(length);
        self.write_key(key);
        write_str(&mut self.out, ":[");
    }

    fn end_list(&mut self, _key: &[u8]) {
        self.end_key();
        write_str(&mut self.out, "]");
    }

    fn list_element(&mut self, _key: &[u8], value: &[u8]) {
        self.write_comma();
        self.write_value(value);
    }

    fn start_sorted_set(&mut self, key: &[u8], length: u32,
                        _expiry: Option<u64>, _info: EncodingType) {
        self.start_key(length);
        self.write_key(key);
        write_str(&mut self.out, ":{");
    }

    fn end_sorted_set(&mut self, _key: &[u8]) {
        self.end_key();
        write_str(&mut self.out, "}");
    }

    fn sorted_set_element(&mut self, _key: &[u8],
                          score: f64, member: &[u8]) {
        self.write_comma();
        self.write_key(member);
        write_str(&mut self.out, ":");
        self.write_value(score.to_string().as_bytes());
    }
}