Ask Your Question
0

JSON and basic sage types

asked 2013-07-02 20:17:27 +0200

Pedro gravatar image

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...

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2013-07-03 14:33:11 +0200

Volker Braun gravatar image

Do you want the internal representation of your object, a sage command that recreates the object, or a human-readable representation? Depending on your needs you'd want very different JSON objects, so there is no one-size-fits-all solution to your question. Essentially you'll have to write your own depending on your needs.

edit flag offensive delete link more

Comments

The habit of the search for standards made ask the question. Thank you.

Pedro gravatar imagePedro ( 2013-07-04 06:25:01 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2013-07-02 20:17:27 +0200

Seen: 599 times

Last updated: Jul 03 '13