import class Range from range;
import fn { range, iterator, next, is_consumed } from range;

interface Serializable<Serializer> {
    fn serialize(serializer: @'Serializer, obj: Self) -> String;
}

// This is a stub
class JSONSerializer {}

fn serialize(serializer: @JSONSerializer, obj: Int) -> String {
    return obj.deref().to_string();
}

fn serialize(serializer: @JSONSerializer, obj: Float) -> String {
    return obj.deref().to_string();
}

fn serialize(serializer: @JSONSerializer, obj: String) -> String {
    return "\"" + obj + "\"";
}

fn<K, V> serialize_map_field(serializer: @JSONSerializer, key: 'K [Serializable<JSONSerializer>], value: 'V [Serializable<JSONSerializer>]) -> String {
    return serializer.serialize(*key) + ": " + serializer.serialize(*value);
}

fn<T> serialize(serializer: @JSONSerializer, obj: Array<'T [Serializable<JSONSerializer>]>) -> String {
    let res = "[";

    for i in range(0, obj.len()) {
        res = res + serializer.serialize(*obj[*i]);

        if i < obj.len() - 1 {
            res = res + ", ";
        }
    }

    return res + "]";
}

// Implementations

implement Serializable<JSONSerializer> for Int;
implement Serializable<JSONSerializer> for Float;
implement Serializable<JSONSerializer> for String;
implement<T> Serializable<JSONSerializer> for Array<'T [Serializable<JSONSerializer>]>;