Skip to content

Commit

Permalink
Add custom JSONEncoder to handle Paths
Browse files Browse the repository at this point in the history
  • Loading branch information
oerc0122 committed Feb 4, 2025
1 parent 72442d0 commit 419f850
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions MDANSE/Src/MDANSE/Framework/Configurators/IConfigurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,19 @@

import abc
import json
from pathlib import Path

from MDANSE.Core.Error import Error

from MDANSE.Core.SubclassFactory import SubclassFactory

class CustomEncoder(json.JSONEncoder):
"""Custom JSON encoder to encode paths as strings."""
def default(self, obj):
if isinstance(obj, Path):
return str(obj)
return super().default(obj)


class ConfiguratorError(Error):
"""
Expand Down Expand Up @@ -82,20 +90,20 @@ class IConfigurator(dict, metaclass=SubclassFactory):

_default = None

_encoder = json.encoder.JSONEncoder()
_encoder = json.encoder.CustomEncoder()
_decoder = json.decoder.JSONDecoder()

_doc_ = "undocumented"

def __init__(self, name, **kwargs):
"""
Initializes a configurator object.
:param name: the name of this configurator.
:type name: str
:param dependencies: the other configurators on which this configurator depends on to be configured. \
This has to be input as a dictionary that maps the name under which the dependency will be used within \
the configurator implementation to the actual name of the configurator on which this configurator is depending on.
the configurator implementation to the actual name of the configurator on which this configurator is depending on.
:type dependencies: (str,str)-dict
:param default: the default value of this configurator.
:type default: any python object
Expand Down

0 comments on commit 419f850

Please sign in to comment.