Git Product home page Git Product logo

Comments (3)

mmerickel avatar mmerickel commented on June 10, 2024

There is code I wrote in Pyramid's master to do this. I can probably strip it out and provide the JsonRpcRenderer API with an add_adapter method and a user-specified serializer (defaulting to json.dumps). I'll have to think about how to allow the user to do it though.

Probably via

renderer = JsonRpcRenderer()
renderer.add_adapter(MyType, lambda v: str(v))
config.add_renderer('myjsonrpc', renderer)
config.add_jsonrpc_endpoint('foo', '/api/jsonrpc', default_renderer='myjsonrpc')

Normally renderers in Pyramid are specified on the view (method), but a default could be provided on the route (endpoint).

from pyramid_rpc.

kisielk avatar kisielk commented on June 10, 2024

Yeah, I think configuring it on a per-endpoint basis makes sense. I was trying to figure out how to do that myself, but I don't think I understand the Pyramid architecture well enough to make a patch :/

from pyramid_rpc.

mmerickel avatar mmerickel commented on June 10, 2024

Added this feature in 634223c. You can just add a new renderer to Pyramid and specify it on your endpoint like so

def jsonrpc_renderer(info):
    def _render(value, system):
        return json.dumps(value)
    return _render

config.add_renderer('myjsonrpc', jsonrpc_renderer)
config.add_jsonrpc_endpoint('rpc', '/api/jsonrpc', default_renderer='myjsonrpc')

If you want to use Pyramid 1.4's cool new JSON renderer which supports adapters for custom data types, just do

from pyramid.renderers import JSON

jsonrpc_renderer = JSON()
jsonrpc_renderer.add_adapter(lambda v: v.isoformat(), datetime.datetime)

config.add_renderer('myjsonrpc', jsonrpc_renderer)
config.add_jsonrpc_endpoint('rpc', '/api/jsonrpc', default_renderer='myjsonrpc')

from pyramid_rpc.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.