Skip to content

Commit

Permalink
json
Browse files Browse the repository at this point in the history
  • Loading branch information
tschm committed Jul 25, 2023
1 parent 3159051 commit ca1cc40
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 1,170 deletions.
File renamed without changes.
23 changes: 23 additions & 0 deletions cvx/io/json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
import json
from collections.abc import Iterable

import numpy as np
from numpyencoder import NumpyEncoder


def read_json(json_file):
"""Read a json file and return a generator of key-value pairs"""
with open(json_file, "r") as f:
json_data = json.load(f)
for name, data in json_data.items():
if isinstance(data, Iterable):
yield name, np.asarray(data)
else:
yield name, data


def write_json(json_file, data):
"""Write a json file with the data"""
with open(json_file, "w") as f:
json.dump(data, f, cls=NumpyEncoder)
Loading

0 comments on commit ca1cc40

Please sign in to comment.