You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There are several types of JSON files in the Meeko world. Many objects can be created from JSON (which is really convenient when put into use), but the errors are not so clear if there's a problem within the JSON file or the incorrect object hook. It might be helpful to catch and elaborate errors (just provide some quick hints) for bad input JSON or incorrect object hook.
There are some situations that the JSON can be loaded into a dict, but cannot be built into a valid Python object like Polymer. I wish there's a way to catch the problem, but I'm not so sure how to do it
The text was updated successfully, but these errors were encountered:
Its really really not necessary unless more people want it. Here's a class to inherit from. Something like the following gives some hints on where the problem occurred (parsing, decoder function, object type) and avoids letting go a partial parse silently.
classBaseJSONParsable:
# Define in Individual Subclasses expected_json_keys=Noneobject_hook=None# and some decoder function# Inherit from_json and from_json_file@classmethoddeffrom_json(cls, json_string):
try:
obj=json.loads(json_string, object_hook=cls.object_hook)
# Log mismatched keys (maybe not a fatal problem)ifset(obj.keys()) !=cls.expected_json_keys:
logging.error(
f"Keys from JSON ({set(obj.keys())}) differ from "f"expected keys ({cls.expected_json_keys})."
)
# Successifisinstance(obj, cls):
returnobjexceptExceptionasdecoder_error:
try:
obj=json.loads(json_string)
# Error occurred while parsing JSONexceptExceptionasparser_error:
raiseRuntimeError(
f"Unable to parse the source JSON for {cls.__name__}: {parser_error}"
)
# Error occurred within the decoder functionraiseRuntimeError(
f"An error occurred when creating {cls.__name__}: {decoder_error}"
)
# Failed to create objraiseValueError(
f"Unexpected object type created from JSON: {type(obj)}. "f"Expected object type is: {cls.__name__}."
)
@classmethoddeffrom_json_file(cls, json_file):
withopen(json_file, "r") asf:
json_string=f.read()
returncls.from_json(json_string)
There are several types of JSON files in the Meeko world. Many objects can be created from JSON (which is really convenient when put into use), but the errors are not so clear if there's a problem within the JSON file or the incorrect object hook. It might be helpful to catch and elaborate errors (just provide some quick hints) for bad input JSON or incorrect object hook.
There are some situations that the JSON can be loaded into a dict, but cannot be built into a valid Python object like Polymer. I wish there's a way to catch the problem, but I'm not so sure how to do it
The text was updated successfully, but these errors were encountered: