Saving and loading in JSON (composites.json_io)#

The objects MatLamina, Lamina, Laminate, LaminationParameters and GradABD are saved to and loaded from JSON with:

from composites import laminated_plate, save_json, load_json

lam = laminated_plate([0, 45, -45, 90], plyt=1e-3, laminaprop=laminaprop)
save_json(lam, 'laminate.json')
lam2 = load_json('laminate.json')

The JSON is plain text, independent of the Python and composites versions, and it is safe to load from untrusted sources, unlike pickle. It can also be exchanged with JavaScript when running in the browser through Pyodide.

All public attributes are stored, including the derived ones, such as the ABD matrix and the constitutive terms of each ply, such that the loaded object is identical to the saved one. A MatLamina shared by several plies of a Laminate is stored once and is still shared after loading. The transverse shear distribution used by Laminate.calc_transverse_shear_stress() is not stored and is recomputed on demand. Attributes of Python subclasses that are not public attributes of the base class are not stored, and the loaded object has the type of the base class.

The values nan, inf and -inf, e.g. Abarbar44 of a ply with a singular transverse shear matrix, are stored as the strings "NaN", "Infinity" and "-Infinity", such that the output is strict JSON, readable by JSON.parse in JavaScript.

The format of the saved data is:

{
  "type": "Laminate",
  "format_version": 2,
  "composites_version": "0.10.0",
  "data": {...}
}
composites.json_io.from_dict(d)#

Create an object from a dictionary created by to_dict()

d must be a dictionary with exactly the keys 'type', 'format_version', 'composites_version' and 'data', with an integer 'format_version' equal to the format version of this module (2), and with a 'type' equal to 'Laminate', 'Lamina', 'MatLamina', 'LaminationParameters' or 'GradABD'. Files of other format versions are not read. The object is created from d['data'], which must contain every key that to_dict() writes for that type, without defaults for missing keys. 'composites_version' is only used in the error message of another format version.

Parameters:
ddict

Dictionary as returned by to_dict().

Returns:
objLaminate, Lamina, MatLamina, LaminationParameters or GradABD

The object.

Raises:
ValueError

If d is not valid, has unknown or missing keys, at any level, or was created with another format version, older or newer.

See also

to_dict, from_json
composites.json_io.from_json(s)#

Create an object from a JSON string created by to_json()

Returns from_dict(json.loads(s)), see from_dict(), which raises a ValueError for invalid content.

Parameters:
sstr or bytes

The JSON string.

Returns:
objLaminate, Lamina, MatLamina, LaminationParameters or GradABD

The object.

composites.json_io.load_json(fname)#

Load an object from a JSON file created by save_json()

When fname has a read method, the content is read with it and the file object is not closed; otherwise the file is opened with the encoding UTF-8. The content is converted with from_json().

Parameters:
fnamestr, path-like or file object

Name of the file, or a file object opened for reading.

Returns:
objLaminate, Lamina, MatLamina, LaminationParameters or GradABD

The object.

composites.json_io.save_json(obj, fname, indent=2)#

Save an object to a JSON file

The string of to_json() is written to fname. When fname has a write method, the string is written with it and the file object is not closed; otherwise the file is opened with the encoding UTF-8, overwriting an existing file, and closed after writing.

Parameters:
objLaminate, Lamina, MatLamina, LaminationParameters or GradABD

The object to be saved.

fnamestr, path-like or file object

Name of the file, or a file object opened in text mode for writing.

indentint or None, optional

Indentation of the JSON output, None for the most compact form. Default 2.

See also

load_json
composites.json_io.to_dict(obj)#

Convert an object into a dictionary of JSON-compatible types

The type of obj is identified with isinstance(), checking Laminate first, then Lamina, MatLamina, LaminationParameters and GradABD. 'type' is the name of the class found, 'format_version' the format version of this module (2), 'composites_version' the version of the installed composites and 'data' the stored attributes, see the module documentation. The values nan, inf and -inf are stored as the strings "NaN", "Infinity" and "-Infinity".

Parameters:
objLaminate, Lamina, MatLamina, LaminationParameters or GradABD

The object to be converted.

Returns:
ddict

Dictionary with the keys 'type', 'format_version', 'composites_version' and 'data', containing only dict, list, str, int, float and None.

Raises:
TypeError

If obj is not an instance of one of the classes above.

See also

from_dict, to_json
composites.json_io.to_json(obj, **kwargs)#

Convert an object into a JSON string

Returns json.dumps(to_dict(obj), allow_nan=False, **kwargs), see to_dict(), which raises a TypeError for an object of another type.

Parameters:
objLaminate, Lamina, MatLamina, LaminationParameters or GradABD

The object to be converted.

kwargsdict, optional

Passed to json.dumps(), e.g. indent=2.

Returns:
sstr

The JSON string.