import reference

# obj -> function || Ref || [obj || JSON-type]

def serialize(bridge, obj):
    pass

def parse_server_cmd(bridge, obj):
    chain = obj['destination']['ref']
    args = deserialize(bridge, obj['args'])
    # XXX: Are we absolutely guaranteed this node has a record of
    # the specified service? If not, wrap the following line in a try
    # block, and create the (clearly remote) service (without ops?).
    service = reference.get_service(bridge, chain)
    return service._ref, args

def deserialize(bridge, obj):
    if type(obj) is dict:
        for key, val in obj:
            if type(val) is list and 'ref' in val:
                chain = val['ref']
                if len(chain) == 3:
                    name = chain[reference.SERVICE]
                    if name in bridge._children:
                        # XXX: Is it ever possible to already know
                        # an incoming remote service reference?
                        obj[key] = bridge._children[name]._ref
                    else:

                if bridge.
                ref = reference.RemoteRef(bridge, chain, None)
                try:
                    ref._service
                except:
                    # XXX: Handle missing services.
                    raise NotImplemented()

                obj[key] = ref._setOps(val.get('operations', []))
            else:
                deserialize(bridge, obj[key])
    else:
        for elt in obj:
            deserialize(bridge, elt)


@@ -218,213 +218,8 @@
 '%5D)%0A
-    # XXX: Are we absolutely guaranteed this node has a record of%0A    # the specified service? If not, wrap the following line in a try%0A    # block, and create the (clearly remote) service (without ops?).%0A
     
@@ -334,57 +334,51 @@
     
-if type(obj) is dict:%0A        for key, val in 
+for container, key, chain in find_refs(
 obj
+)
 :%0A  
@@ -387,121 +387,63 @@
     
-    if type(val) is list and 'ref' in val:%0A                chain = val%5B'ref'%5D%0A                if len
+try:%0A            service = reference.get_service
 (chain)
- == 3:
 %0A   
@@ -455,96 +455,37 @@
     
-        name = chain%5Breference.SERVICE%5D%0A                    if name in bridge._children:
+container%5Bkey%5D = service._ref
 %0A   
@@ -481,36 +481,40 @@
 ce._ref%0A        
-    
+except:%0A
             # XX
@@ -515,665 +515,473 @@
   # 
-XXX: Is it ever possible to already know%0A                        # an incoming remote service reference?%0A                        obj%5Bkey%5D = bridge._children%5Bname%5D._ref%0A                    else:%0A%0A                if bridge.%0A                ref = reference.RemoteRef(bridge, chain, None)%0A                try:%0A                    ref._service%0A                except:%0A                    # XXX: Handle missing services.%0A                    raise NotImplemented()%0A%0A               
+Create and store a remote service with a RemoteRef.%0A            raise NotImplemented()%0A%0Adef find_refs(obj):%0A    # obj -%3E %5Bobj%5D, %7Bstr: obj%7D, atom%0A    iterator = %5B%5D%0A    if type(obj) is list:%0A        iterator = enumerate(obj)%0A    elif type(obj) is dict:%0A        iterator = obj%0A    for key, val in iterator:%0A        if type(val) is dict and 'ref' in val:%0A            yield
  obj
-%5Bkey%5D = ref._setOps(val.get('operations', %5B%5D))%0A            else:%0A                deserialize(bridge, obj%5Bkey%5D)%0A    else:%0A        for elt in obj:%0A            deserialize(bridge, e
+, key, val%0A        else:%0A            for result in find_refs(val):%0A                yield resu
 lt
-)
 %0A%0A

work on deserializer