'Converts a JSON object into an element
@param tag: tag name for the element
@param obj: the JSON object
@param native: use native mode for attributes'

@classmethod
def __obj2element(cls, tag, obj, native=False):

    resource = field = None
    if (not tag):
        tag = cls.TAG.object
    elif native:
        if tag.startswith(cls.PREFIX.reference):
            field = tag[(len(cls.PREFIX.reference) + 1):]
            tag = cls.TAG.reference
        elif tag.startswith(cls.PREFIX.options):
            resource = tag[(len(cls.PREFIX.options) + 1):]
            tag = cls.TAG.options
        elif tag.startswith(cls.PREFIX.resource):
            resource = tag[(len(cls.PREFIX.resource) + 1):]
            tag = cls.TAG.resource
        elif (not (tag == cls.TAG.root)):
            field = tag
            tag = cls.TAG.data
    element = etree.Element(tag)
    if native:
        if resource:
            if (tag == cls.TAG.resource):
                element.set(cls.ATTRIBUTE.name, resource)
            else:
                element.set(cls.ATTRIBUTE.resource, resource)
        if field:
            element.set(cls.ATTRIBUTE.field, field)
    attribute_prefix = cls.PREFIX.attribute
    text_prefix = cls.PREFIX.text
    obj2element = cls.__obj2element
    json2element = cls.__json2element
    for k in obj:
        m = obj[k]
        if isinstance(m, dict):
            child = obj2element(k, m, native=native)
            element.append(child)
        elif isinstance(m, (list, tuple)):
            if (native and k.startswith(attribute_prefix)):
                a = k[len(attribute_prefix):]
                element.set(a, json.dumps(m))
                continue
            for obj_ in m:
                child = json2element(k, obj_, native=native)
                element.append(child)
        elif (k == text_prefix):
            element.text = m
        elif k.startswith(attribute_prefix):
            a = k[len(attribute_prefix):]
            if (not isinstance(m, basestring)):
                m = str(m)
            element.set(a, m)
        else:
            child = json2element(k, m, native=native)
            element.append(child)
    return element
