-
Notifications
You must be signed in to change notification settings - Fork 37
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
Add as_json
to responses
#861
Open
PerchunPak
wants to merge
1
commit into
master
Choose a base branch
from
add-as-json-to-responses
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+48
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
from __future__ import annotations | ||
|
||
from abc import ABC, abstractmethod | ||
from dataclasses import dataclass | ||
from dataclasses import dataclass, asdict | ||
from typing import Any, TYPE_CHECKING | ||
|
||
from mcstatus.forge_data import ForgeData, RawForgeData | ||
|
@@ -101,6 +101,25 @@ def build(cls, *args, **kwargs) -> Self: | |
""" | ||
raise NotImplementedError("You can't use abstract methods.") | ||
|
||
def as_json(self) -> dict: | ||
"""Return the dataclass as JSON-serializable :class:`dict`. | ||
|
||
Do note that this method doesn't return :class:`string <str>` but | ||
:class:`dict`, so you can do some processing on returned value. | ||
|
||
Difference from | ||
:attr:`~mcstatus.responses.JavaStatusResponse.raw` is in that, | ||
:attr:`~mcstatus.responses.JavaStatusResponse.raw` returns raw response | ||
in the same format as we got it. This method returns the response | ||
in a more user-friendly JSON serializable format (for example, | ||
:attr:`~mcstatus.responses.BaseStatusResponse.motd` is returned as a | ||
:func:`Minecraft string <mcstatus.motd.Motd.to_minecraft>` and not | ||
:class:`dict`). | ||
""" | ||
as_dict = asdict(self) | ||
as_dict["motd"] = self.motd.simplify().to_minecraft() | ||
return as_dict | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Though only response classes ( |
||
|
||
|
||
@dataclass(frozen=True) | ||
class JavaStatusResponse(BaseStatusResponse): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer the name of this to be
as_dict
orto_dict
. Methods that dump json should returnstr
just like Python's builtinjson.dumps
https://docs.python.org/3/library/json.html#json.dumpsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docstring is missing too. I would highlight the difference between this and
raw
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also thought about this name, but the difference between our
as_json
anddataclasses.asdict
is that our method returns JSON-serializable dict. User could also do some more processing on returned object, e.g. use nonstandard JSON library (orjson
for example).Added a docstring in https://github.com/py-mine/mcstatus/compare/d19f58699f37a40cb45dc4c4aa435ce05de8c2a6..2b9731b13aa5b90f99863488603947026f711fb7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To return anything other than a string would be confusing to all that expect it. Python's
json.dumps
? String. JavaScript'sJSON.parse()
? String input. Go's marshal/unmarshal? Byte strings. Rust's popular serde json library? You guessed it, strings. Given the overwhelming amount of these examples that I could keep finding, the name cannot indicate JSON is returned because JSON is a serialized string format.The user could also serialize to something other than JSON. What about
as_serializable_dict
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Serializable in what? Maybe
as_json_dict
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Serializable would be an adjective in that name. The function is returning a dictionary that is able to be serialized. What about
as_json_dict
? No, JSON is a string format as mentioned earlier. Serializable data is not limited to only becoming JSON and could instead be handled with pickle, protobuf, or some other serializer.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I generally agree with kevin here, it definitely shouldn't be
as_json
, although I don't mindas_json_dict
, since the dict that this produces will be a json serializable dict, which can't be said of every dict (not every python object that can be in a general dict is json serializable).That said, I also don't mind it just being
as_dict
, or something likeas_user_friendly_dict
or some other deviation of that general type of name, the ability to serialize it with JSON doesn't need to be apparent from the function name, it can just be mentioned in the docstring. Also, it is true that by making itas_json_dict
, it might imply that it's only meant for json, and no other formats, which isn't necessarily the case, like kevin mentioned.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem is that it is too loose, you cannot make something that works for every single format on the planet.
I mean, this PR tries to create an easier way to serialize in JSON, it doesn't have a goal to be compatible with anything else. Other formats may have their own quirks, so I don't have a goal to make the underlying object be serializable in any possible format (and function name should reflect that). Yes, JSON is a string format, but user may want to use different library for serializing (e.g. orjson) mainly when computing huge amount of answers in parallel (and we do have such users). In such cases, stdin library is often too slow.
If you can hammer in the nail with scissors, it doesn't mean scissors are intended for this. If this method accidentally works for other formats - good, if not - it is not a supported use case. Again, the goal is to provide an easy way to transform a server's answer to JSON. This is because I needed this method multiple times, and our CLI also has to work around that our response classes are no longer JSON serializable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JSON is a string format. Please remove JSON from this function's name that neither consumes a JSON string nor produces a JSON string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd be a shame to keep blocking this PR just because of the name semantics here. I think we should just go with
as_dict
, which seems perfectly sufficient; we can always clarify in the docstring that the returned dictionary is suitable for JSON serialization.Perhaps
as_json_dict
would be a better name, perhaps it wouldn't, regardless though, I don't see a strong reason to hold up merging over that.as_dict
is good enough, let's use that and get this merged.