Skip to content
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

Fixed JSON encoding of importance used as keys #397

Draft
wants to merge 2 commits into
base: dev
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions src/nwbinspector/nwbinspector.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import yaml
from tqdm import tqdm
from natsort import natsorted
from packaging.version import Version

from . import available_checks
from .inspector_tools import (
Expand All @@ -38,13 +39,29 @@
class InspectorOutputJSONEncoder(json.JSONEncoder):
"""Custom JSONEncoder for the NWBInspector."""

def default(self, o): # noqa D102
if isinstance(o, InspectorMessage):
return o.__dict__
if isinstance(o, Enum):
return o.name
else:
return super().default(o)
def _transform_enum(self, object_: object):
if isinstance(object_, Enum):
return object_.name
return self._recursively_encode(object_)

def _recursively_encode(self, object_: object):
if isinstance(object_, dict):
return {
self._transform_enum(object_=key): self._transform_enum(object_=value) for key, value in object_.items()
}
return object_

def encode(self, object_: object):
return super().encode(self._recursively_encode(object_=object_))

def default(self, object_): # noqa D102
if isinstance(object_, InspectorMessage):
return object_.__dict__
if isinstance(object_, Enum):
return object_.name
if isinstance(object_, Version):
return str(object_)
return super().default(object_)


def validate_config(config: dict):
Expand Down