JsonSerializationMixin

class lsst.verify.jsonmixin.JsonSerializationMixin

Bases: object

Mixin that provides JSON serialization support to subclasses.

Subclasses must implement the json method. The method returns a dict that can be serialized to JSON. Use the jsonify_dict method to handle the conversion of iterables, numbers, strings, booleans and JsonSerializationMixin-compatible objects into a JSON-serialiable object.

Attributes Summary

json dict that can be serialized as semantic JSON, compatible with the SQUASH metric service.

Methods Summary

jsonify_dict(d) Recursively build JSON-renderable objects on all values in a dict.
write_json(filepath) Write JSON to a file.

Attributes Documentation

json

dict that can be serialized as semantic JSON, compatible with the SQUASH metric service.

Methods Documentation

static jsonify_dict(d)

Recursively build JSON-renderable objects on all values in a dict.

Parameters:
d : dict

Dictionary to convert into a JSON-serializable object. Values are recursively JSON-ified.

Returns:
json_dict : dict

Dictionary that can be serialized to JSON.

Examples

Subclasses can use this method to prepare output in their json-method implementation. For example:

def json(self):
    return JsonSerializationMixin.jsonify_dict({
        'value': self.value,
    })
write_json(filepath)

Write JSON to a file.

Parameters:
filepath : str

Destination file name for JSON output.