"""Artist JSON Encoder. Handles JSON encoding of an Artist model instance, specifically for the purposes of constructing microservice API responses. """ import json from artist.models.artist import Artist class ArtistJSONEncoder(json.JSONEncoder): """Handles default JSON encoding of an Artist model instance.""" def default(self, obj): """Extract properties of an artist object for JSON serialization. Args: obj (Artist): artist object to serialize. Returns: dict: dictionary of artist data to render as JSON. """ if isinstance(obj, Artist): return { 'id': obj.artist_id, 'name': obj.name, 'artist_type': obj.artist_type} return json.JSONEncoder.default(self, obj)