Ask Your Question

Revision history [back]

JSON and basic sage types

Is there any package that produces a JSON string from sage dictionary containing "basic" sage types ?

Something like [1]:

class jsonmegua(json.JSONEncoder):

def default(self, o):
    try:
        if type(o)==sage.rings.integer.Integer:
            json_obj = int(o)
        elif type(o)==sage.rings.real_mpfr.RealLiteral:
            json_obj = float(o)
        elif type(o)==sage.rings.real_mpfr.RealNumber:
            json_obj = float(o)
        elif type(o)==sage.symbolic.expression.Expression:
            json_obj = latex(o)
        else:
            json_obj = o
    except TypeError:
        pass
    else:
        return json_obj

    # Let the base class default method raise the TypeError
    return JSONEncoder.default(self, o)

[1] http://docs.python.org/2/library/json.html