     func toDictionary() -> [String:Any] {
         return ["type": "Circle", "cx": cx, "cy": cy, "r": r]
     }
-    
-    convenience init(dictionary: [String:Any]) {
-        self.init(cx: parse(dictionary["cx"]),
-                  cy: parse(dictionary["cy"]),
-                  r: parse(dictionary["r"]))
-    }
 }
 
 extension Ellipse: Serializable {

    func toDictionary() -> [String:Any] {
        return ["type": "Ellipse", "cx": cx, "cy": cy, "rx": rx, "ry": ry]
    }
    
    convenience init(dictionary: [String:Any]) {
        self.init(cx: parse(dictionary["cx"]),
                  cy: parse(dictionary["cy"]),
                  rx: parse(dictionary["rx"]),
                  ry: parse(dictionary["ry"]))
    }    
}

extension Line: Serializable {
    
    func toDictionary() -> [String:Any] {
        return ["type": "Line", "x1": x1, "y1": y1, "x2": x2, "y2": y2]
    }
    
    convenience init(dictionary: [String:Any]) {
        self.init(x1: parse(dictionary["x1"]),
                  y1: parse(dictionary["y1"]),
                  x2: parse(dictionary["x2"]),
                  y2: parse(dictionary["y2"]))
    }
}





class LocusSerializer {
    
    var factories = [String:([String:Any]) -> Locus]()
    
    init() {
        factories["Arc"] = { dictionary in
            Arc(ellipse: self.instance(dictionary: dictionary["Ellipse"] as! [String:Any]) as! Ellipse,
                shift: parse(dictionary["shift"]),
                extent: parse(dictionary["extent"]))
        }
        factories["Circle"] = { dictionary in
            Circle(cx: parse(dictionary["cx"]),
                   cy: parse(dictionary["cy"]),
                   r: parse(dictionary["r"]))
        }
        factories["Ellipse"] = { dictionary in
            Ellipse(cx: parse(dictionary["cx"]),
                    cy: parse(dictionary["cy"]),
                    rx: parse(dictionary["rx"]),
                    ry: parse(dictionary["rx"]))
        }
        factories["Line"] = { dictionary in
            Line(x1: parse(dictionary["x1"]),
                 y1: parse(dictionary["y1"]),
                 x2: parse(dictionary["x2"]),
                 y2: parse(dictionary["y2"]))
        }
        factories["Path"] = { dictionary in
            let array = dictionary["segments"] as! [[String:Any]]
            var pathSegments = [PathSegment]()
            for dict in array {
                pathSegments.append(PathSegment(dictionary: dict))
            }
            return Path(segments: pathSegments)
        }
        factories["Polygon"] = { dictionary in
            Polygon.init(points: dictionary["points"] as! [Double])
        }
        factories["Polyline"] = { dictionary in
            Polyline.init(points: dictionary["points"] as! [Double])
        }
        factories["Rect"] = { dictionary in
            Rect(x: parse(dictionary["x"]),
                 y: parse(dictionary["y"]),
                 w: parse(dictionary["w"]),
                 h: parse(dictionary["h"]))
        }
        factories["RoundRect"] = { dictionary in
            RoundRect(rect: self.instance(dictionary: dictionary["Rect"] as! [String:Any]) as! Rect,
                      rx: parse(dictionary["rx"]),
                      ry: parse(dictionary["ry"]))
        }
    }
    
    func instance(dictionary: [String:Any]) -> Locus {
        let type = dictionary["type"] as! String
        return factories[type]!(dictionary)
    }
}

