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()dmust 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 fromd['data'], which must contain every key thatto_dict()writes for that type, without defaults for missing keys.'composites_version'is only used in the error message of another format version.
- composites.json_io.from_json(s)#
Create an object from a JSON string created by
to_json()Returns
from_dict(json.loads(s)), seefrom_dict(), which raises aValueErrorfor invalid content.- Parameters:
- sstr or bytes
The JSON string.
- Returns:
- obj
Laminate,Lamina,MatLamina,LaminationParametersorGradABD The object.
- obj
- composites.json_io.load_json(fname)#
Load an object from a JSON file created by
save_json()When
fnamehas areadmethod, 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 withfrom_json().- Parameters:
- fnamestr, path-like or file object
Name of the file, or a file object opened for reading.
- Returns:
- obj
Laminate,Lamina,MatLamina,LaminationParametersorGradABD The object.
- obj
- composites.json_io.save_json(obj, fname, indent=2)#
Save an object to a JSON file
The string of
to_json()is written tofname. Whenfnamehas awritemethod, 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:
- obj
Laminate,Lamina,MatLamina,LaminationParametersorGradABD 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,
Nonefor the most compact form. Default2.
- obj
See also
- composites.json_io.to_dict(obj)#
Convert an object into a dictionary of JSON-compatible types
The type of
objis identified withisinstance(), checkingLaminatefirst, thenLamina,MatLamina,LaminationParametersandGradABD.'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 valuesnan,infand-infare stored as the strings"NaN","Infinity"and"-Infinity".- Parameters:
- obj
Laminate,Lamina,MatLamina,LaminationParametersorGradABD The object to be converted.
- obj
- Returns:
- ddict
Dictionary with the keys
'type','format_version','composites_version'and'data', containing onlydict,list,str,int,floatandNone.
- Raises:
- TypeError
If
objis not an instance of one of the classes above.
- composites.json_io.to_json(obj, **kwargs)#
Convert an object into a JSON string
Returns
json.dumps(to_dict(obj), allow_nan=False, **kwargs), seeto_dict(), which raises aTypeErrorfor an object of another type.- Parameters:
- obj
Laminate,Lamina,MatLamina,LaminationParametersorGradABD The object to be converted.
- kwargsdict, optional
Passed to
json.dumps(), e.g.indent=2.
- obj
- Returns:
- sstr
The JSON string.