Allow injection of preserialized JSON using the default API#95
Conversation
When the `default` function returns a bytes object, include it verbatim into the serializaion. This allows users to store preserialized versions of their objects in a database and use rapidjson to generate structures that include them. If the `default` function returns an invalid JSON the resulting string returned by rapidjson will be in turn invalid.
|
Thank you for the proposal. While I can understand your use case and thus how this would be handy, I'm not sure I like the automatic assumption that any I wonder if a better way would be providing a I will try to have a closer look at your commits this weekend. |
|
I understand your concern and agree that a more explicit RawJSON class would be desirable. But in the interest of clarity, let me point out that this PR does not affect serialization of byte strings by rapidjson when not using the It would only affect byte strings directly returned by the default function. >>> import rapidjson as rj
>>> rj.dumps({'foo': b'bar'})
'{"foo":"bar"}'
>>> rj.dumps({'foo': rj}, default=lambda x: {'contains_a_nested': b'byte string'})
'{"foo":{"contains_a_nested":"byte string"}}'
>>> rj.dumps({'foo': rj}, default=lambda x: b'{"Pre": "serialized"}')
'{"foo":{"Pre": "serialized"}}'Anyway, I can refactor the PR to use a custom RawJSON class, when you give me the green light. |
|
@lelit I updated the pull request. I took the liberty to add running doctests in travis. |
|
Great! I'm sorry I could not focus on PyRJ, but I'll do my best to merge this in a short time. |
|
Thank you Silvio, I merged your changes, with some mostly stylistic tweaks. |
|
@lelit Thanks! I realized failing doctests didn't really make the build fail, so I'm opening a different PR for that. |
For speed reason we're storing some preserialized JSON in our database.
It's still very useful to be able to treat preserialized objects with the same toolchain as the not serialized ones, so we amended python-rapidjson to support our use case.
This PR proposes a change to the
defaultfunction API, so that when it returns a bytes string, it's not further serialized, but instead used verbatim.Risks:
defaultand returning a bytes string should no longer expect it to be serialized. We expect all users would return a regular unicode string.