Saving and loading in JSON#
A Laminate saved with save_json() is loaded identical by
load_json(), from a file name or a file object, see
Saving and loading (JSON). The function make_laminate of the tests creates a
laminate with laminated_plate():
def test_save_load(tmp_path):
lam = make_laminate()
fname = tmp_path / 'laminate.json'
save_json(lam, fname)
assert_same(lam, load_json(fname))
assert_same(lam, load_json(str(fname)))
with open(fname) as f:
assert json.load(f)['type'] == 'Laminate'
buf = io.StringIO()
save_json(lam, buf, indent=None)
assert '\n' not in buf.getvalue()
buf.seek(0)
assert_same(lam, load_json(buf))
to_dict() and from_dict() embed a laminate in another JSON
document:
def test_example_embed_in_json_document(tmp_path):
lam = laminated_plate([0, 45, -45, 90], plyt=0.125e-3,
laminaprop=laminaprop, rho=1600.)
# to_dict returns JSON types, to be embedded in another JSON document
doc = {'name': 'skin', 'mesh_size': 0.01, 'laminate': to_dict(lam)}
fname = tmp_path / 'model.json'
with open(fname, 'w') as f:
json.dump(doc, f)
with open(fname) as f:
doc2 = json.load(f)
lam2 = from_dict(doc2['laminate'])
np.testing.assert_array_equal(lam2.ABD, lam.ABD)
np.testing.assert_array_equal(lam2.Ats, lam.Ats)
assert lam2.intrhoz6 == lam.intrhoz6