Did I find the right examples for you? yes no      Crawl my project      Python Jobs

All Samples(6)  |  Call(4)  |  Derive(0)  |  Import(2)
Converts the given value to a JSON representation. This function is an
enhanced version of `simplejson`, because it supports more datatypes
(datetime, struct_time) and provides more flexibilty in how values can be
serialized.

Specifically, the given 'value' contains a 'asJS' or 'asJSON' method,
this method will be invoked with this function as first argument and
the options as keyword-arguments ('**options')

        def asJSON( value, **options ):
	"""Converts the given value to a JSON representation. This function is an
	enhanced version of `simplejson`, because it supports more datatypes
	(datetime, struct_time) and provides more flexibilty in how values can be
	serialized.
	
	Specifically, the given 'value' contains a 'asJS' or 'asJSON' method,
	this method will be invoked with this function as first argument and
	the options as keyword-arguments ('**options')
	"""
	# FIXME: It might be better to use json(asPrimitive(value,options)) if it
	# does not have a performance penalty
	if options.has_key("currentDepth"):
		options["currentDepth"] = options["currentDepth"] + 1
	else:
		options["currentDepth"] = 0
	if value in (True, False, None) or type(value) in (float, int, long, str, unicode):
		res = json(value)
	elif type(value) in (list, tuple, set):
		res = "[%s]" % (",".join(map(lambda x:asJSON(x,**options), value)))
	elif type(value) == dict:
		r = []
		for k in value.keys():
			r.append('%s:%s' % (json(unicode(k)), asJSON(value[k], **options)))
		res = "{%s}" % (",".join(r))
	elif hasattr(value, "__class__") and value.__class__.__name__ == "datetime":
		res = asJSON(tuple(value.timetuple()), **options)
	elif hasattr(value, "__class__") and value.__class__.__name__ == "date":
		res = asJSON(tuple(value.timetuple()), **options)
	elif hasattr(value, "__class__") and value.__class__.__name__ == "struct_time":
		res = asJSON(tuple(value), **options)
	elif hasattr(value, "asJSON")  and callable(value.asJSON):
		res = value.asJSON(asJSON, **options)
	elif hasattr(value, "export") and callable(value.export):
		try:
			value = value.export(**options)
		except:
			value = value.export() 
		res = asJSON(value)
	# The asJS is not JSON, but rather only JavaScript objects, so this implies
	# that there is a library implemented on the client side
	elif hasattr(value, "asJS") and callable(value.asJS):
		res = value.asJS(asJSON, **options)
	# There may be a "serializer" function that knows better about the different
	# types of object. We use it if it is provided.
	elif options.get("serializer"):
		serializer = options.get("serializer")
		res = serializer(asJSON, value, **options)
		if res is None: res = asJSON(value.__dict__, **options)
	else:
		res = asJSON(value.__dict__, **options)
	return res
        


src/r/e/retro-HEAD/Sources/retro/web.py   retro(Download)
 
import os, re, sys, time, functools, traceback, StringIO
from core import Request, Response, Event, RendezVous, asJSON, json, unjson
 
LOG_ENABLED       = True
					if context.items():
						yield "{'status':%s,'reason':%s,'headers':%s,'body':%s}\n\n" % (
							asJSON(context['status']),
							asJSON(context['reason']),
							asJSON(context['headers']),
							asJSON(res)

src/r/e/retro-HEAD/Sources/retro/__init__.py   retro(Download)
import wsgi
from wsgi import REACTOR, onShutdown, onError
from core import asJSON, asPrimitive, cut, escapeHTML
from web  import on, expose, predicate, when, restrict, cache, \
Component, Application, \