Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Ticket #6 (closed defect: fixed)

Opened 14 years ago

Last modified 8 years ago

Make JSON serialization flexible

Reported by: kevin Owned by: anonymous
Priority: normal Milestone: 0.9
Component: TurboGears Version:
Severity: normal Keywords: sprint


Bob Ippolito has provided a usefully subclassable JSON serializer:  https://svn.red-bean.com/bob/simple_json/trunk

This should be used with PyProtocols?' generic function facility to provide pluggable serialization. This can be used to make a generic SQLObject serializer, for example.

Change History

comment:1 Changed 14 years ago by kevin

  • Component changed from CherryPy to TurboGears

comment:2 Changed 14 years ago by kevin

  • Keywords sprint added

comment:3 Changed 14 years ago by kevin

Ian Bicking did a good portion of this (include SQLObject serialization). I wanted to try RuleDispatch? out, so I played around with a very little JSON serialization routine, since that was brought up here before. Doesn't actually create JSON, just JSONable objects.

You install RuleDispatch? with:

easy_install -Zf  http://peak.telecommunity.com/snapshots/ RuleDispatch?

Some description here:


And the module I wrote here:


A pleasantly simple system indeed. In the end it's more like MochiKit?'s adaptation than Zope's, I'd guess. Except cooler of course -- not because I'm implemented something special, but merely because it's in Python and not Javascript ;) And generic functions are cool.

What makes it more interesting still is that you can override serializations, like override the generic SQLObject serialization for a particular class. Though I feel like it would be nice to make that potentially more contextual... but anyway, it's interesting.

Where the contextual conversion of objects seems really interesting is with views, like an HTML representation of an object, or the HTML form for editing an object, etc.

Here's his code:

# Using RuleDispatch:
import pkg_resources
import dispatch
print dispatch.__file__
import sqlobject

def jsonify(obj):
    Return an object that can be serialized with JSON, i.e., it
    is made up of only lists, dictionaries (with string keys),
    and strings, ints, and floats.
    raise NotImplementedError

@jsonify.when('isinstance(obj, (int, float, str, unicode))')
def jsonify_simple(obj):
    return obj

# @@: Should this match all iterables?
@jsonify.when('isinstance(obj, (list, tuple))')
def jsonify_list(obj):
    return [jsonify(v) for v in obj]

@jsonify.when('isinstance(obj, dict)')
def jsonify_dict(obj):
    result = {}
    for name in obj:
        if not isinstance(name, (str, unicode)):
            raise ValueError(
                "Cannot represent dictionaries with non-string keys in JSON (found: %r)" % name)
        result[name] = jsonify(obj[name])
    return result

@jsonify.when('isinstance(obj, sqlobject.SQLObject)')
def jsonify_sqlobject(obj):
    result = {}
    result['id'] = obj.id
    for name in obj.sqlmeta.columns.keys():
        result[name] = getattr(obj, name)
    return result

@jsonify.when('hasattr(obj, "__json__")')
def jsonify_explicit(obj):
    return obj.__json__()

@jsonify.when('isinstance(obj, sqlobject.SQLObject.SelectResultsClass)')
def jsonify_select_results(obj):
    return jsonify_list(obj)

comment:4 Changed 14 years ago by kevin

  • Milestone changed from 1.0 to 0.9

comment:5 Changed 13 years ago by kevin

Bob has released simple_json:  http://cheeseshop.python.org/pypi/simple_json

comment:6 Changed 13 years ago by kevin

  • Status changed from new to closed
  • Resolution set to fixed

done in [370]

Note: See TracTickets for help on using tickets.