Skip to content
Snippets Groups Projects
Commit 78469bbe authored by Mohamed, Fawzi Roberto (fawzi)'s avatar Mohamed, Fawzi Roberto (fawzi)
Browse files

fix numpy json support

parent 90bc7a85
No related branches found
No related tags found
No related merge requests found
from builtins import object from builtins import object
import json import json
from nomadcore.compact_sha import sha512 from nomadcore.compact_sha import sha512
import numpy
"""Various functions to simplify and standardize dumping objects to json""" """Various functions to simplify and standardize dumping objects to json"""
def numpyEncoder(o): def numpyEncoder(o):
"""new default function for json class so that numpy arrays and sets can be encoded""" """new default function for json class so that numpy arrays and sets can be encoded"""
# check if object is a numpy array # check if object is a numpy array
if isinstance(o, np.ndarray): if isinstance(o, numpy.ndarray):
# ensure that we have an array with row-major memory order (C like) # ensure that we have an array with row-major memory order (C like)
if not o.flags['C_CONTIGUOUS']: if not o.flags['C_CONTIGUOUS']:
o = np.ascontiguousarray(o) o = numpy.ascontiguousarray(o)
return o.tolist() return o.tolist()
# see default method in python/json/encoder.py # see default method in python/json/encoder.py
elif isinstance(o, set): elif isinstance(o, set):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment