explodinglabs / jsonrpcserver

Process incoming JSON-RPC requests in Python
MIT License
186 stars 40 forks source link

Add ability to use custom serializer and deserializer #125

Closed deptyped closed 4 years ago

deptyped commented 4 years ago

Now all you have to do to change the serialization or deserialization is pass the function to the dispatcher, like this:

import ujson
from flask import Flask, request, Response
from jsonrpcserver import method, dispatch

app = Flask(__name__)

@method
def ping():
    return "pong"

@app.route("/", methods=["POST"])
def index():
    response = dispatch(request.get_data().decode(), serialize=ujson.dumps, deserialize=ujson.loads)
    return Response(str(response), response.http_status, mimetype="application/json")

if __name__ == "__main__":
    app.run()
bcb commented 4 years ago

Thanks @deptyped 👌