'Serialize the mobile form configuration for the target resource
@param msince: include look-up records only if modified
after this datetime ("modified since")
@return: a JSON-serialiable dict containing the mobile form
configuration for export to the mobile client'

def serialize(self, msince=None):

    s3db = current.s3db
    resource = self.resource
    ms = S3MobileSchema(resource)
    schema = ms.serialize()
    main = {'tablename': resource.tablename, 'schema': schema, 'form': ms.form}
    strings = self.strings()
    if strings:
        main['strings'] = strings
    subheadings = ms.subheadings
    if subheadings:
        main['subheadings'] = subheadings
    required = set(ms.references.keys())
    provided = set([resource.tablename])
    components = self.components()
    for alias in components:
        cresource = resource.components.get(alias)
        if (not cresource):
            continue
        cschema = S3MobileSchema(cresource)
        hook = components[alias]
        hook['schema'] = cschema.serialize()
        provided.add(cresource.tablename)
        for tname in cschema.references:
            required.add(tname)
    references = {}
    required = list(required)
    while required:
        tablename = required.pop()
        if (tablename in provided):
            continue
        record_ids = ms.references[tablename]
        if record_ids:
            rresource = s3db.resource(tablename, id=list(record_ids))
        else:
            rresource = s3db.resource(tablename)
        rs = S3MobileSchema(rresource)
        schema = rs.serialize()
        spec = {'schema': schema}
        if record_ids:
            fields = schema.keys()
            tree = rresource.export_tree(fields=fields, references=fields, msince=msince)
            if len(tree.getroot()):
                data = current.xml.tree2json(tree, as_dict=True)
                spec['data'] = data
        references[tablename] = spec
        for reference in rs.references:
            if (reference not in provided):
                required.append(reference)
        provided.add(tablename)
    form = {'main': main}
    if references:
        form['references'] = references
    if components:
        form['components'] = components
    return form
