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

JSON serialization error Object of type int32 is not JSON serializable #15

Open
zhubonan opened this issue Jun 13, 2022 · 0 comments
Open

Comments

@zhubonan
Copy link

When I run the code below (code version 6cc8c38):

data= generate_slabs(
    structure='./POSCAR.mp-149_Si',
                    hkl=2,
                    parallelise=True,
                    #save_metadata=False,
                    thicknesses=[20,30], 
                    vacuums=[20,30], 
                    potcar_functional='ps', 
                    #save_slabs=False,
                    #save_metadata=False,
               make_input_files=False)

The following error is thrown:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-86e11d266020> in <module>
----> 1 data= generate_slabs(
      2     structure='./POSCAR.mp-149_Si',
      3                     hkl=2,
      4                     parallelise=True,
      5                     #save_metadata=False,

~/tmp/surfaxe/surfaxe/generation.py in generate_slabs(structure, hkl, thicknesses, vacuums, save_slabs, save_metadata, json_fname, make_fols, make_input_files, max_size, center_slab, ox_states, is_symmetric, layers_to_relax, fmt, name, config_dict, user_incar_settings, user_kpoints_settings, user_potcar_settings, parallelise, **kwargs)
    298             i['slab'] = i['slab'].as_dict()
    299         with open(json_fname, 'w') as f:
--> 300             json.dump(unique_list_of_dicts_copy, f)
    301 
    302     if save_slabs:

~/miniconda3/envs/py3w/lib/python3.8/json/__init__.py in dump(obj, fp, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, default, sort_keys, **kw)
    177     # could accelerate with writelines in some versions of Python, at
    178     # a debuggability cost
--> 179     for chunk in iterable:
    180         fp.write(chunk)
    181 

~/miniconda3/envs/py3w/lib/python3.8/json/encoder.py in _iterencode(o, _current_indent_level)
    427             yield _floatstr(o)
    428         elif isinstance(o, (list, tuple)):
--> 429             yield from _iterencode_list(o, _current_indent_level)
    430         elif isinstance(o, dict):
    431             yield from _iterencode_dict(o, _current_indent_level)

~/miniconda3/envs/py3w/lib/python3.8/json/encoder.py in _iterencode_list(lst, _current_indent_level)
    323                 else:
    324                     chunks = _iterencode(value, _current_indent_level)
--> 325                 yield from chunks
    326         if newline_indent is not None:
    327             _current_indent_level -= 1

~/miniconda3/envs/py3w/lib/python3.8/json/encoder.py in _iterencode_dict(dct, _current_indent_level)
    403                 else:
    404                     chunks = _iterencode(value, _current_indent_level)
--> 405                 yield from chunks
    406         if newline_indent is not None:
    407             _current_indent_level -= 1

~/miniconda3/envs/py3w/lib/python3.8/json/encoder.py in _iterencode_dict(dct, _current_indent_level)
    403                 else:
    404                     chunks = _iterencode(value, _current_indent_level)
--> 405                 yield from chunks
    406         if newline_indent is not None:
    407             _current_indent_level -= 1

~/miniconda3/envs/py3w/lib/python3.8/json/encoder.py in _iterencode_list(lst, _current_indent_level)
    323                 else:
    324                     chunks = _iterencode(value, _current_indent_level)
--> 325                 yield from chunks
    326         if newline_indent is not None:
    327             _current_indent_level -= 1

~/miniconda3/envs/py3w/lib/python3.8/json/encoder.py in _iterencode_dict(dct, _current_indent_level)
    403                 else:
    404                     chunks = _iterencode(value, _current_indent_level)
--> 405                 yield from chunks
    406         if newline_indent is not None:
    407             _current_indent_level -= 1

~/miniconda3/envs/py3w/lib/python3.8/json/encoder.py in _iterencode_dict(dct, _current_indent_level)
    403                 else:
    404                     chunks = _iterencode(value, _current_indent_level)
--> 405                 yield from chunks
    406         if newline_indent is not None:
    407             _current_indent_level -= 1

~/miniconda3/envs/py3w/lib/python3.8/json/encoder.py in _iterencode(o, _current_indent_level)
    436                     raise ValueError("Circular reference detected")
    437                 markers[markerid] = o
--> 438             o = _default(o)
    439             yield from _iterencode(o, _current_indent_level)
    440             if markers is not None:

~/miniconda3/envs/py3w/lib/python3.8/json/encoder.py in default(self, o)
    177 
    178         """
--> 179         raise TypeError(f'Object of type {o.__class__.__name__} '
    180                         f'is not JSON serializable')
    181 

TypeError: Object of type int32 is not JSON serializable

It is caused because the dictionary contains a int as keys. For python this is OK, but not the case for JSON where the keys must be strings. I am not able to pin point where the offending dictionary key is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant