praw-dev / praw

PRAW, an acronym for "Python Reddit API Wrapper", is a python package that allows for simple access to Reddit's API.
http://praw.readthedocs.io/
BSD 2-Clause "Simplified" License
3.49k stars 457 forks source link

Where did json_dict go? #701

Closed kittsville closed 7 years ago

kittsville commented 7 years ago

I was wondering where the property json_dict of Reddit objects went? I can't seem to find any other way of retrieving the original JSON object returned by Reddit.

I've found many references to the property in past on this repo and StackOverflow but not what it was renamed to or why it was removed.

System Information

PRAW Version: 4.1.0 Python Version: 2.7 Operating System: Windows 10 (unfortunately)

bboe commented 7 years ago

Hi, great question. You're the first to ask about it. It's not currently available. Can you help me understand what your use case is for having it? It's probably something we could add back, I just want to understand the reasoning for it.

kittsville commented 7 years ago

Hi,

Sure! I'm currently making Pyroon, a script which converts switch-a-roos into an interactive graph. The back-end can parse switch-a-roo chains and either save them or export them as Cytoscape.js formatted JSON. The front-end loads the formatted JSON and displays the graph.

json_dict would be used to save the switch-a-roo comments in case they were later deleted or mischievously edited to hyperlink elsewhere (common for Roos). I currently load deleted comments in PRAW with the PushShift API (a cache of Reddit) using praw.models.Comment(reddit, _data=pushshift_json). With access to json_dict I could save comments then later load them via the same means. Without relying on the PushShift API when I don't have to would reduce the risk of malicious edits (cached by PushShift) from permanently breaking the switch-a-roo chain.

tl;dr backing up switch-a-roos

bboe commented 7 years ago

Would using the pickle support PRAW has be a suitable alternative?

Here's an example: https://github.com/praw-dev/praw/blob/3002fe9730c1194c8183d492d318d079094ca618/tests/unit/models/reddit/test_comment.py#L48

kittsville commented 7 years ago

I probably should have mentioned* I was also hoping to access the whole Reddit response from the front-end, to display information about roo comments when they're clicked on. While serialising the comment via pickle would probably be the best solution for the back-end it wouldn't be readable by the front.

I could always just write a helper for Pyroon to copy the 10 or so properties into a dict, something like:

def serialize(comment):
    return {
        'stickied': comment.stickied,
        'author': str(comment.author),
        'subreddit': str(comment.subreddit),
        # etc...
    }

*Sorry, my brain doesn't seem to be working much today

bboe commented 7 years ago

If you can manually json serialize just the properties you want I'd prefer that to exposing the underlying response for simplicity on PRAW's end. Please let me know if that's suitable.

kittsville commented 7 years ago

That'd probably be best. I mostly didn't want to reinvent the wheel if the json_dict property still existed, just under a different name.

Plus it'll make my code more explicit, which is always a plus.

bboe commented 7 years ago

Great. I'm going to close this issue for now. Thanks for bringing it up.

duhaime commented 4 years ago

@bboe is it really not possible to access the full response from the Reddit API in JSON form? That seems like it should definitely be possible with this library...

PythonCoderAS commented 4 years ago

It's completely possible. You just need to make a call to Reddit.request instead of Reddit.get, Reddit.post, and the other methods that objectify returned JSON data.

duhaime commented 4 years ago

Ah, perfect! Thanks very much @PythonCoderAS !

johnwheeler commented 4 months ago
import humps

def praw_object_to_dict(obj):
    data = {}
    for attr in dir(obj):
        if not attr.startswith("_") and not callable(getattr(obj, attr)):
            data[humps.camelize(attr)] = getattr(obj, attr)
    return data