예제 #1
0
    """
    if not s:
        return "''"

    s = re_escape.sub(r'\\\1', s)
    if re_space.search(s):
        s = "'" + s + "'"

    return s


# Create default json typecasters for PostgreSQL 9.2 oids
from psycopg2._json import register_default_json, register_default_jsonb    # noqa

try:
    JSON, JSONARRAY = register_default_json()
    JSONB, JSONBARRAY = register_default_jsonb()
except ImportError:
    pass

del register_default_json, register_default_jsonb


# Create default Range typecasters
from psycopg2. _range import Range                              # noqa
del Range


# Add the "cleaned" version of the encodings to the key.
# When the encoding is set its name is cleaned up from - and _ and turned
# uppercase, so an encoding not respecting these rules wouldn't be found in the
def register_json(loads=None, dumps=None):
    if loads:
        globals()['_loads'] = loads
        register_default_json(loads=loads)
    if dumps:
        globals()['_dumps'] = dumps
예제 #3
0
    """
    if not s:
        return "''"

    s = re_escape.sub(r"\\\1", s)
    if re_space.search(s):
        s = "'" + s + "'"

    return s


# Create default json typecasters for PostgreSQL 9.2 oids
from psycopg2._json import register_default_json, register_default_jsonb  # noqa

try:
    JSON, JSONARRAY = register_default_json()
    JSONB, JSONBARRAY = register_default_jsonb()
except ImportError:
    pass

del register_default_json, register_default_jsonb


# Create default Range typecasters
from psycopg2._range import Range  # noqa

del Range


# Add the "cleaned" version of the encodings to the key.
# When the encoding is set its name is cleaned up from - and _ and turned
                '__value__': obj.isoformat()}
    raise TypeError(repr(obj) + ' is not JSON serializable')

def from_json(obj):
    if '__class__' in obj:
        type = obj.get('__class__')
        if type == 'Decimal':
            return float(obj.get('__value__'))
        elif type == 'datetime':
            return datetime.strptime(obj.get('__value__'), '%Y-%m-%dT%H:%M:%S.%fZ')
    return obj

_loads = partial(_json.loads, object_hook=to_json)
_dumps = partial(_json.dumps, default=from_json)

register_default_json(loads=partial(_json.loads, object_hook=from_json))

def register_json(loads=None, dumps=None):
    if loads:
        globals()['_loads'] = loads
        register_default_json(loads=loads)
    if dumps:
        globals()['_dumps'] = dumps

class JSON(sqltypes.Concatenable, sqltypes.TypeEngine):
    """Represents the PostgreSQL JSON type.

    The :class:`.JSON` type stores python dictionaries using standard
    python json libraries to parse and serialize the data for storage
    in your database.
    """