Python pyramid.renderers.JSON Examples
The following are 12
code examples of pyramid.renderers.JSON().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
pyramid.renderers
, or try the search function
.
![](https://www.programcreek.com/common/static/images/search.png)
Example #1
Source File: test_integration.py From pyramid_jwt with BSD 2-Clause "Simplified" License | 6 votes |
def test_pyramid_custom_json_encoder(app_config: Configurator): """Test we can still use user-defined custom adapter""" from pyramid.renderers import json_renderer_factory def serialize_anyclass(obj, request): assert False # This asserts this method will not be called json_renderer_factory.add_adapter(NonSerializable, serialize_anyclass) def other_serializer(obj, request): return "other_serializer" my_renderer = JSON() my_renderer.add_adapter(NonSerializable, other_serializer) app_config.add_renderer("json", my_renderer) app = TestApp(app_config.make_wsgi_app()) response = app.get("/extra_claims") token = str(response.json_body["token"]) response = app.get("/dump_claims", headers={"X-Token": token}) assert response.json_body["extra_claim"] == "other_serializer"
Example #2
Source File: __init__.py From pyramid-jsonapi with GNU Affero General Public License v3.0 | 6 votes |
def __init__(self, api): self.api = api self.metadata = {} # Load mako templating self.api.config.include('pyramid_mako') self.api.config.add_renderer('json_sorted', JSON(sort_keys=True)) self.views = [ VIEWS( attr='openapi_spec', route_name='specification', request_method='', renderer='json_sorted' ), VIEWS( attr='swagger_ui', route_name='', request_method='', renderer='pyramid_jsonapi.metadata.OpenAPI:swagger-ui/index.mako' ) ]
Example #3
Source File: policy.py From pyramid_jwt with BSD 2-Clause "Simplified" License | 5 votes |
def __call__(self, *args, **kwargs): json_renderer = None if self.registry is not None: json_renderer = self.registry.queryUtility( IRendererFactory, "json", default=JSONEncoder ) request = kwargs.get("request") if not kwargs.get("default") and isinstance(json_renderer, JSON): self.components = json_renderer.components kwargs["default"] = self._make_default(request) return JSONEncoder(*args, **kwargs)
Example #4
Source File: test_integration.py From pyramid_jwt with BSD 2-Clause "Simplified" License | 5 votes |
def __json__(self): return "This is JSON Serializable"
Example #5
Source File: test_integration.py From pyramid_jwt with BSD 2-Clause "Simplified" License | 5 votes |
def test_pyramid_json_encoder_fail(app): with pytest.raises(TypeError) as e: app.get("/extra_claims") assert "NonSerializable" in str(e.value) assert "is not JSON serializable" in str(e.value)
Example #6
Source File: __init__.py From consuming_services_python_demos with MIT License | 5 votes |
def register_json_renderer(config): json_renderer = JSON(indent=4) json_renderer.add_adapter(Post, lambda p, _: p.__dict__) config.add_renderer('pretty_json', json_renderer)
Example #7
Source File: wsgi_app.py From channelstream with BSD 3-Clause "New" or "Revised" License | 5 votes |
def make_app(server_config): config = Configurator( settings=server_config, root_factory=APIFactory, default_permission="access" ) config.include("pyramid_jinja2") module_, class_ = server_config["signature_checker"].rsplit(".", maxsplit=1) signature_checker_cls = getattr(importlib.import_module(module_), class_) config.registry.signature_checker = signature_checker_cls(server_config["secret"]) authn_policy = AuthTktAuthenticationPolicy( server_config["cookie_secret"], max_age=2592000 ) authz_policy = ACLAuthorizationPolicy() config.set_authentication_policy(authn_policy) config.set_authorization_policy(authz_policy) json_renderer = JSON(serializer=json.dumps, indent=4) json_renderer.add_adapter(datetime.datetime, datetime_adapter) json_renderer.add_adapter(uuid.UUID, uuid_adapter) config.add_renderer("json", json_renderer) config.add_subscriber( "channelstream.subscribers.handle_new_request", "pyramid.events.NewRequest" ) config.add_request_method("channelstream.utils.handle_cors", "handle_cors") config.include("channelstream.wsgi_views") config.scan("channelstream.wsgi_views.server") config.scan("channelstream.wsgi_views.error_handlers") config.scan("channelstream.events") config.include("pyramid_apispec.views") config.pyramid_apispec_add_explorer( spec_route_name="openapi_spec", script_generator="channelstream.utils:swagger_ui_script_template", permission="admin", route_args={ "factory": "channelstream.wsgi_views.wsgi_security:AdminAuthFactory" }, ) app = config.make_wsgi_app() return app
Example #8
Source File: renderer.py From pyramid_swagger with BSD 3-Clause "New" or "Revised" License | 5 votes |
def __init__(self, renderer_factory=JSON()): self.renderer_factory = renderer_factory
Example #9
Source File: __init__.py From restful-services-in-pyramid with MIT License | 5 votes |
def configure_renderers(config): json_renderer = JSON(indent=4) json_renderer.add_adapter(Car, lambda c, _: c.to_dict()) config.add_renderer('json', json_renderer)
Example #10
Source File: __init__.py From restful-services-in-pyramid with MIT License | 5 votes |
def configure_renderers(config): json_renderer = JSON(indent=4) json_renderer.add_adapter(Car, lambda c, _: c.to_dict()) config.add_renderer('json', json_renderer)
Example #11
Source File: __init__.py From restful-services-in-pyramid with MIT License | 5 votes |
def configure_renderers(config): json_renderer = JSON(indent=4) json_renderer.add_adapter(Car, lambda c, _: c.to_dict()) config.add_renderer('json', json_renderer)
Example #12
Source File: __init__.py From pyramid-jsonapi with GNU Affero General Public License v3.0 | 4 votes |
def main(global_config, **settings): """ This function returns a Pyramid WSGI application. """ # The usual stuff from the pyramid alchemy scaffold. engine = engine_from_config(settings, 'sqlalchemy.') models.DBSession.configure(bind=engine) models.Base.metadata.bind = engine config = Configurator(settings=settings) config.add_static_view('static', 'static', cache_max_age=3600) config.add_route('home', '/') config.add_route('echo', '/echo/{type}') config.scan(views) # Set up the renderer. renderer = JSON() renderer.add_adapter(datetime.date, datetime_adapter) config.add_renderer('json', renderer) # Lines specific to pyramid_jsonapi. # Create an API instance. pj = pyramid_jsonapi.PyramidJSONAPI( config, test_settings['models_iterable'][ settings.get('pyramid_jsonapi_tests.models_iterable', 'module') ], lambda view: models.DBSession ) # Register a bad filter operator for test purposes. pj.filter_registry.register('bad_op') # Create the routes and views automagically. pj.create_jsonapi_using_magic_and_pixie_dust() person_view = pj.view_classes[ models.Person ] person_view.callbacks['after_serialise_object'].appendleft( person_callback_add_information ) person_view.allowed_fields = property(person_allowed_fields) person_view.allowed_object = person_allowed_object pj.append_callback_set_to_all_views( 'access_control_serialised_objects' ) # Back to the usual pyramid stuff. return config.make_wsgi_app()