-
-
Notifications
You must be signed in to change notification settings - Fork 63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: allowing to return (pass-through) json literal string to avoid loads()/dumps() or escaping #600
Comments
|
but that still leaves string escaping issue. If you put json-as-string into JSON value, it will be escaped. Not sure about second point. We don't need to parse PostgreSQL's returned JSON, just pass it to the web browser basically. It's not question on to have "faster/slower" JSON parser, it's just not needed at at, as it's nonsesical to invoke maybe thousands of allocations in Python to build dict/list from PostgreSQL JSON result, just for later (after split second) to be looped and rendered into string again. |
@Talkless You can define your own
See here the example of use. Also, does the capability to custom serializer/deserializer help in that use case? It's very interesting to have on the project, as @alexted mentioned it could be valuable for other scenarios as well. Thank you. |
So if I understood correctly, we create custom view
In
Not sure what you mean by that exactly, but if lines with |
Or maybe even better to have |
Yes, that's it. The downside here is to copy-and-paste the entire method post, because of that the proposal of customizer the serializer/deserializer is welcome.
It is the exact feature that you are asking for, the capability to overwrite the serializer/deserializer ( Does it make sense to you? Thank you. |
@nycholas all clear, thanks! Maybe we can just rename this issue? |
No worries, let's use this issue 💪. |
👋 @Talkless good news, as the project uses the
Testing it by doing the request:
Let me know if solves your use case, please. Thank you. |
Wow, that looks much better solution, thanks! |
Wait, Result needed is:
instead of:
|
The custom JSON provider there are two methods, and each one of them will treat the request and response. The method
and the method
So, I just did an example of how to customize the JSON provider, it wasn't the intention to create a JSON provider that will suit your use case, now you can change it and use it for your use case. Thank you! |
@Talkless I'm closing this issue as you didn't pronounce yourself, I understand that the solution fixed your use case, any other questions or doubts, please reopen the issue. Thank you. |
Thanks @nycholas I might get back to this question some time in the future, currency too busy with other stuff. |
Our use case for
flask-jsonrpc
is to implement API server for PostgreSQL database.PostgreSQL returns results as JSON objects, but that "forces"
psycopg2
to parse it (usingjson.lodas
?) to build Python dict/list.But our
flask-jsonrpc
app DOES NOT need to have JSON parsed into Python objects (dict/list), no need to do any work on it, it just has to pass it to the API user (JavaScript webapp). And these JSON objects can be HUGE!So in naive/straightforward scenario:
psycopg2
parses it, returns Python object (dict/list). That's expensive work (JSON result objects can be huge).flask-jsonrpc
builds final JSON-RPC response dict withid
,jsonrcp
version andresult
Python object (dict/list) from our view.json.dumps
probably?) to return to API client (browser). That's expensive work (JSON result objects can be huge).To avoid loading/dumping JSON in
flask-jsonrpc
API server, we decided to return JSON result object from PostgreSQL as string.Since SQL query's result not "officially" JSON,
psycopg2
does not parse it, but nowflask-jsonrcp
's result JSON has it escaped, it looks like this:Finally, JavaScript webapp has to parse
JSON-RPC
response object AND then parse escaped "result" string into JavaScript object again.This kind of escaping/de-escaping of json-as-string is much better that parsing/dumping JSON object inside Python, but still feels "nonsensical", strange for webdevs, sub-optimal.
I wonder could there be a way to allow to "pass through" JSON-as-string result from PostgreSQL/psycopg2 into
flask-jsonrcp
response object?Maybe ability to override rendering of valid (non-error) result could work (that assumes our views always returns JSON string literals) :
Or, maybe some special type annotating that we want to return literal JSON string to be emplaced without escaping into final
JSON-RPC
response object:Or this is would be considered "obscure", "rare", "out-of-scope" optimization"?
The text was updated successfully, but these errors were encountered: