本文整理汇总了Python中pyramid.compat.json.dumps函数的典型用法代码示例。如果您正苦于以下问题:Python dumps函数的具体用法?Python dumps怎么用?Python dumps使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了dumps函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_json_body_alternate_charset
def test_json_body_alternate_charset(self):
from pyramid.compat import json
request = self._makeOne({'REQUEST_METHOD':'POST'})
inp = text_(
b'/\xe6\xb5\x81\xe8\xa1\x8c\xe8\xb6\x8b\xe5\x8a\xbf',
'utf-8'
)
if PY3: # pragma: no cover
body = bytes(json.dumps({'a':inp}), 'utf-16')
else:
body = json.dumps({'a':inp}).decode('utf-8').encode('utf-16')
request.body = body
request.content_type = 'application/json; charset=utf-16'
self.assertEqual(request.json_body, {'a':inp})
示例2: notify_observers
def notify_observers(self, msg):
out = json.dumps(msg)
for obs in self.observers:
obs.put(out)
obs.put(StopIteration)
# for now just kill observers once a message is sent instead
# of playing with chunked encoding
self.observers = []
示例3: _render
def _render(value, system):
request = system.get('request')
if request is not None:
response = request.response
ct = response.content_type
if ct == response.default_content_type:
response.content_type = 'application/json'
return json.dumps(value, default=default_encoder)
示例4: test_json_body_alternate_charset
def test_json_body_alternate_charset(self):
from pyramid.compat import json
request = self._makeOne({'REQUEST_METHOD':'POST'})
request.charset = 'latin-1'
la = unicode('La Pe\xc3\xb1a', 'utf-8')
body = json.dumps({'a':la}, encoding='latin-1')
request.body = body
self.assertEqual(request.json_body, {'a':la})
示例5: _render
def _render(value, system):
request = system.get("request")
if request is not None:
response = request.response
ct = response.content_type
if ct == response.default_content_type:
response.content_type = "application/json"
return json.dumps(value)
示例6: _makeDummyRequest
def _makeDummyRequest(self, request_data=None):
from pyramid.testing import DummyRequest
request = DummyRequest()
request.matched_route = DummyRoute('JSON-RPC')
if request_data is not None:
request.body = json.dumps(request_data)
request.content_length = len(request.body)
return request
示例7: add_observer
def add_observer(self, cursor=None):
obs = self.Observer(game=self)
if cursor == self.cursor or cursor is None:
self.observers.append(obs)
else:
msg = json.dumps(self.updates[cursor+1])
obs.put(msg)
obs.put(StopIteration)
return obs
示例8: serialize
def serialize(self):
tracking = {
'events': self.events,
'alias': self.user_id if self.alias else None,
'identify': self.user_id if self.user_id else None
}
return {
'api_token': self.api_token,
'tracking': tracking,
'tracking_json': json.dumps(tracking)
}
示例9: jsonrpc_error_response
def jsonrpc_error_response(error, id=None):
""" Marshal a Python Exception into a webob ``Response``
object with a body that is a JSON string suitable for use as
a JSON-RPC response with a content-type of ``application/json``
and return the response."""
body = json.dumps({"jsonrpc": "2.0", "id": id, "error": error.as_dict()})
response = Response(body)
response.content_type = "application/json"
response.content_length = len(body)
return response
示例10: test_view_callable_cls_with_dict
def test_view_callable_cls_with_dict(self):
target = self._makeOne()
view_callable = target(DummyView)
request = testing.DummyRequest()
params = {'jsonrpc':'2.0', 'method':'dummy_rpc',
'params':dict(a=3, b=4), 'id':'test'}
body = json.dumps(params)
request.json_body = params
request.rpc_args = dict(a=3, b=4)
context = object()
result = view_callable(context, request)
self.assertEqual(result, 7)
示例11: test_view_callable_cls_with_invalid_args
def test_view_callable_cls_with_invalid_args(self):
from pyramid_rpc.jsonrpc import JsonRpcParamsInvalid
target = self._makeOne()
view_callable = target(DummyView)
request = testing.DummyRequest()
params = {'jsonrpc':'2.0', 'method':'dummy_rpc',
'params':[], 'id':'test'}
body = json.dumps(params)
request.json_body = params
request.rpc_args = []
context = object()
self.assertRaises(JsonRpcParamsInvalid, view_callable, context, request)
示例12: view_calendar
def view_calendar(context, request):
kotti_calendar_resources.need()
locale_name = get_locale_name(request)
if locale_name in fullcalendar_locales:
fullcalendar_locales[locale_name].need()
else: # pragma: no cover (safety belt only, should never happen)
fullcalendar_locales["en"].need()
session = DBSession()
now = datetime.datetime.now()
query = session.query(Event).filter(Event.parent_id == context.id)
future = or_(Event.start > now, Event.end > now)
upcoming = query.filter(future).order_by(Event.start).all()
past = query.filter(Event.start < now).order_by(desc(Event.start)).all()
upcoming = [event for event in upcoming if
has_permission('view', event, request)]
past = [event for event in past if
has_permission('view', event, request)]
fmt = '%Y-%m-%d %H:%M:%S'
fullcalendar_events = []
for event in (upcoming + past):
json_event = {
'title': event.title,
'url': resource_url(event, request),
'start': event.start.strftime(fmt),
'allDay': event.all_day,
}
if event.end:
json_event['end'] = event.end.strftime(fmt)
fullcalendar_events.append(json_event)
fullcalendar_options = {
'header': {
'left': 'prev,next today',
'center': 'title',
'right': 'month,agendaWeek,agendaDay'
},
'eventSources': context.feeds,
'weekends': context.weekends,
'events': fullcalendar_events,
}
return {
'api': template_api(context, request),
'upcoming_events': upcoming,
'past_events': past,
'fullcalendar_options': json.dumps(fullcalendar_options),
}
示例13: test_it_with_no_id
def test_it_with_no_id(self):
def view(request):
return request.rpc_args[0]
config = self.config
config.include('pyramid_rpc.jsonrpc')
config.add_jsonrpc_endpoint('rpc', '/api/jsonrpc')
config.add_jsonrpc_method(view, endpoint='rpc', method='dummy')
app = config.make_wsgi_app()
app = TestApp(app)
params = {'jsonrpc': '2.0', 'method': 'dummy', 'params': [2, 3]}
resp = app.post('/api/jsonrpc', content_type='application/json',
params=json.dumps(params))
self.assertEqual(resp.status_int, 204)
self.assertEqual(resp.body, '')
示例14: test_it_with_invalid_method
def test_it_with_invalid_method(self):
config = self.config
config.include('pyramid_rpc.jsonrpc')
config.add_jsonrpc_endpoint('rpc', '/api/jsonrpc')
app = config.make_wsgi_app()
app = TestApp(app)
params = {'jsonrpc': '2.0', 'id': 5, 'method': 'foo', 'params': [2, 3]}
resp = app.post('/api/jsonrpc', content_type='application/json',
params=json.dumps(params))
self.assertEqual(resp.status_int, 200)
self.assertEqual(resp.content_type, 'application/json')
result = json.loads(resp.body)
self.assertEqual(result['id'], 5)
self.assertEqual(result['jsonrpc'], '2.0')
self.assertEqual(result['error']['code'], -32601)
示例15: search
def search(context, request):
sr = get_search_results(context, request)
artists, albums, tracks, criteria = sr["artists"], sr["albums"], sr["tracks"], sr["criteria"]
return dict(results=json.dumps({
"artists": render_artists(artists,
request,
context),
"albums": render_albums(context,
request,
albums or [],
show_artist=True)["items"],
"tracks": render_tracks(tracks,
request,
show_artist=True),
"criteria": criteria}), criteria=criteria)