export_obj raises TypeError: Object of type 'WindowsPath' is not JSON serializable

Issue #56 closed
Pascal Lesage
created an issue
>>> len(Database('Battery foreground'))
>>> bw2io.BW2Package.export_obj(Database('Battery foreground'))
TypeError                                 Traceback (most recent call last)
<ipython-input-54-1bb4bf0b72ea> in <module>()
----> 1 bw2io.BW2Package.export_obj(Database('Battery foreground'))

C:\mypy\anaconda\envs\cafetests\lib\site-packages\bw2io\package.py in export_obj(cls, obj, filename, folder, backwards_compatible)
    164         if filename is None:
    165             filename = obj.filename
--> 166         return cls.export_objs([obj], filename, folder, backwards_compatible)
    168     @classmethod

C:\mypy\anaconda\envs\cafetests\lib\site-packages\bw2io\package.py in export_objs(cls, objs, filename, folder, backwards_compatible)
    145             safe_filename(filename) + u".bw2package"
    146         )
--> 147         cls._write_file(filepath, [cls._prepare_obj(o, backwards_compatible) for o in objs])
    148         return filepath

C:\mypy\anaconda\envs\cafetests\lib\site-packages\bw2io\package.py in _write_file(cls, filepath, data)
    124     def _write_file(cls, filepath, data):
    125         JsonWrapper.dump_bz2(
--> 126             JsonSanitizer.sanitize(data), filepath
    127         )

C:\mypy\anaconda\envs\cafetests\lib\site-packages\bw2data\serialization.py in dump_bz2(self, data, filepath)
     37             with bz2.BZ2File(f.name, "wb") as b:
     38                 b.write(
---> 39                     (JsonWrapper.dumps(data)).encode('utf-8')
     40                 )

C:\mypy\anaconda\envs\cafetests\lib\site-packages\bw2data\serialization.py in dumps(self, data)
     58             return anyjson.serialize(data)
     59         else:
---> 60             return json.dumps(data)
     62     @classmethod

C:\mypy\anaconda\envs\cafetests\lib\json\__init__.py in dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, default, sort_keys, **kw)
    229         cls is None and indent is None and separators is None and
    230         default is None and not sort_keys and not kw):
--> 231         return _default_encoder.encode(obj)
    232     if cls is None:
    233         cls = JSONEncoder

C:\mypy\anaconda\envs\cafetests\lib\json\encoder.py in encode(self, o)
    197         # exceptions aren't as detailed.  The list call should be roughly
    198         # equivalent to the PySequence_Fast that ''.join() would do.
--> 199         chunks = self.iterencode(o, _one_shot=True)
    200         if not isinstance(chunks, (list, tuple)):
    201             chunks = list(chunks)

C:\mypy\anaconda\envs\cafetests\lib\json\encoder.py in iterencode(self, o, _one_shot)
    255                 self.key_separator, self.item_separator, self.sort_keys,
    256                 self.skipkeys, _one_shot)
--> 257         return _iterencode(o, 0)
    259 def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,

C:\mypy\anaconda\envs\cafetests\lib\json\encoder.py in default(self, o)
    178         """
    179         raise TypeError("Object of type '%s' is not JSON serializable" %
--> 180                         o.__class__.__name__)
    182     def encode(self, o):

TypeError: Object of type 'WindowsPath' is not JSON serializable

Comments (7)

  1. Chris Mutel repo owner

    Can you email me a copy of one dataset and the database metadata? Was this extracted from Excel? I am not sure how this WindowsPath instance would be introduced.

  2. Pascal Lesage reporter

    Perhaps more useful, and succinct: here is an activity as_dict:

    {'simapro metadata': {'Category type': 'energy',
      'Process identifier': 'psigabe1000217405400971',
      'Time period': 'Unspecified',
      'Geography': 'Unspecified',
      'Technology': 'Unspecified',
      'Representativeness': 'Unspecified',
      'Multiple output allocation': 'Unspecified',
      'Substitution allocation': 'Unspecified',
      'Cut off rules': 'Unspecified',
      'Capital goods': 'Unspecified',
      'Boundary with nature': 'Unspecified',
      'Infrastructure': 'No',
      'Date': '02.10.2015',
      'System description': ''},
     'code': 'a2fa2a88e057c84e351eaae1c6fdaa45',
     'database': 'Battery foreground',
     'filename': WindowsPath('C:/mypy/code/CAFE3/data/clean_or_delete/grid_integration/Grid_integration_22_novembre/Grid_integration_22_novembre/Code pour relinker v3.4 (pour toutes batteries sauf LMP)/PB VRFB import_simapro_updated.CSV'),
     'type': 'process',
     'reference product': 'Cell container, Stack, CellCube, PGU, restructured',
     'production amount': 1.0,
     'name': 'Cell container, Stack, CellCube, PGU, restructured',
     'unit': 'unit'}

    The culprit seems to filename

  3. Chris Mutel repo owner

    Opened #57.

    To solve the immediate problem, either edit the database metadata in each activitiy to convert Windowspath instances to a string, or re-import the whole database but pass a string or result from os.path instead of a Pathlib instance for the filepath of the Simapro database.

  4. Log in to comment