private static function toArray($object) {     
        $type = gettype($object);
        
        if($type == 'array') {            
            foreach($object as $element) {
                // call this method on each object in the array
                $result[]= self::toArray($element);
            }
            
            // return the processed array
            return $result;
        } else if ($type == 'object') {  
            // if we can call toArray() on this object call it, otherwise return
            // the object as-is and trigger a notice
            if(is_subclass_of($object, 'AbstractJSONWrapper')) { 
                return $object->toArray();
            } else {
                trigger_error("JSONSerializer: Trying to serialize " . get_class($object)); 
                return $object;
            }
        } else {
            // if this is not an object we have nothing to do
            return $object;
        }
    }