 
 
 
-extension Locus {
-    static func instantiate(dictionary: [String:Any]) -> Locus? {
-        guard let type = dictionary["type"] as? String else {
-            fatalError("No type specified")
-        }
-        if type == "Arc" {
-            return Arc(dictionary: dictionary)
-        }
-        if type == "Circle" {
-            return Circle(dictionary: dictionary)
-        }
-        if type == "Ellipse" {
-            return Ellipse(dictionary: dictionary)
-        }
-        if type == "Line" {
-            return Line(dictionary: dictionary)
-        }
-        if type == "Path" {
-            return Path(dictionary: dictionary)
-        }
-        if type == "Polygon" {
-            return Polygon(dictionary: dictionary)
-        }
-        if type == "Polyline" {
-            return Polyline(dictionary: dictionary)
-        }
-        if type == "Rect" {
-            return Rect(dictionary: dictionary)
-        }
-        if type == "RoundRect" {
-            return RoundRect(dictionary: dictionary)
-        }
-        
-        fatalError("Locus from dictionary error. Locus \(type) not supported")
+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)
     }
 }
 

    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"]))
    }
}


    func toDictionary() -> [String:Any] {
        return ["type": "Arc", "ellipse": ellipse.toDictionary(), "shift": shift, "extent": extent]
    }
}

extension Circle: Serializable {
