Python json.py() Examples

The following are 4 code examples for showing how to use json.py(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module json , or try the search function .

Example 1
Project: ricloud   Author: reincubate   File: utils.py    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
def _encode(data):
    """Adapted from Django source:
    https://github.com/django/django/blob/master/django/core/serializers/json.py
    """
    if isinstance(data, datetime.datetime):
        r = data.isoformat()
        if data.microsecond:
            r = r[:23] + r[26:]
        if r.endswith("+00:00"):
            r = r[:-6] + "Z"
        return r
    elif isinstance(data, datetime.date):
        return data.isoformat()
    elif isinstance(data, datetime.time):
        r = data.isoformat()
        if data.microsecond:
            r = r[:12]
        return r
    elif isinstance(data, datetime.timedelta):
        return data.total_seconds()
    elif isinstance(data, (decimal.Decimal, uuid.UUID)):
        return compat.to_str(data) 
Example 2
Project: zoe   Author: DistributedSystemsGroup   File: request_handler.py    License: Apache License 2.0 5 votes vote down vote up
def dumps(obj, **kwargs):
    """Escape characters."""
    # https://github.com/mitsuhiko/flask/blob/master/flask/json.py
    return json.dumps(obj, **kwargs) \
        .replace(u'<', u'\\u003c') \
        .replace(u'>', u'\\u003e') \
        .replace(u'&', u'\\u0026') \
        .replace(u"'", u'\\u0027') 
Example 3
Project: zoe   Author: DistributedSystemsGroup   File: request_handler.py    License: Apache License 2.0 5 votes vote down vote up
def tojson_filter(obj, **kwargs):
    """Filter for JSON output in templates."""
    # https://github.com/mitsuhiko/flask/blob/master/flask/json.py
    return Markup(dumps(obj, **kwargs)) 
Example 4
Project: heaviside   Author: jhuapl-boss   File: parser.py    License: Apache License 2.0 5 votes vote down vote up
def json_text_():
    """Returns the parser for JSON Text"""
    # Taken from https://github.com/vlasovskikh/funcparserlib/blob/master/funcparserlib/tests/json.py
    # and modified slightly
    unwrap = lambda x: x.value

    null = (n('null') | n('Null')) >> const(None) >> unwrap

    value = forward_decl()
    member = (string >> unwrap) + op_(u':') + value >> tuple
    object = (
        op_(u'{') +
        maybe(member + many(op_(u',') + member) + maybe(op_(','))) +
        op_(u'}')
        >> make_object)
    array = (
        op_(u'[') +
        maybe(value + many(op_(u',') + value) + maybe(op_(','))) +
        op_(u']')
        >> make_array)

    value.define(
        null
        | (true >> unwrap)
        | (false >> unwrap)
        | object
        | array
        | (number >> unwrap)
        | (string >> unwrap))

    return value