# Source code for algebraixlib.import_export.json

"""Import data from JSON."""

# Copyright Algebraix Data Corporation 2015 - 2017
#
# This file is part of algebraixlib <http://github.com/AlgebraixData/algebraixlib>.
#
# algebraixlib is free software: you can redistribute it and/or modify it under the terms of version
#
# algebraixlib is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
# even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License along with algebraixlib.
# --------------------------------------------------------------------------------------------------
import algebraixlib.mathobjects as _mo

[docs]def import_json(json_file_or_filepath) -> 'P( A x M )':
"""Import the file json_file_or_filepath as JSON file and return nested relations.

:param json_file_or_filepath: A file path or a file object of the file to be imported.
:return: A nested :term:relation that represents the JSON document.
"""
def _process_nodes(nodes):
if isinstance(nodes, list):
for list_data in nodes:
yield _mo.Set(*list(_process_nodes(list_data)))
else:
for key, value in nodes.items():
if isinstance(value, list):
for list_data in value:
child = _process_nodes(list_data)
elif isinstance(value, dict):
children = _process_nodes(value)
elif isinstance(value, (str, int, float)):
else:
assert False  # Node type not supported.

def _import_json(json_file):
import json