<s> from . import interface , exc , meta <EOL> def validate ( obj ) : <EOL> """<STR_LIT>""" <EOL> members = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> interface . validate ( ISerialization , obj , members ) <EOL> class ISerialization ( interface . Interface ) : <EOL> """<STR_LIT>""" <EOL> def get_headers ( ) : <EOL> """<STR_LIT>""" <EOL> def deserialize ( ) : <EOL> """<STR_LIT>""" <EOL> def serialize ( ) : <EOL> """<STR_LIT>""" <EOL> class SerializationHandler ( meta . MetaMixin ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ** kw ) : <EOL> super ( SerializationHandler , self ) . __init__ ( ** kw ) <EOL> def get_headers ( self ) : <EOL> return { } <EOL> def deserialize ( self , serialized_string ) : <EOL> raise NotImplementedError <EOL> def serialize ( self , dict_obj ) : <EOL> raise NotImplementedError <EOL> class JsonSerializationHandler ( SerializationHandler ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ** kw ) : <EOL> try : <EOL> import json <EOL> except ImportError as e : <EOL> import simplejson as json <EOL> self . backend = json <EOL> super ( JsonSerializationHandler , self ) . __init__ ( ** kw ) <EOL> def deserialize ( self , serialized_string ) : <EOL> try : <EOL> if type ( serialized_string ) == bytes : <EOL> serialized_string = serialized_string . decode ( '<STR_LIT:utf-8>' ) <EOL> return self . backend . loads ( serialized_string ) <EOL> except ValueError as e : <EOL> return dict ( error = e . args [ <NUM_LIT:0> ] ) <EOL> def serialize ( self , dict_obj ) : <EOL> return self . backend . dumps ( dict_obj ) <EOL> def get_headers ( self ) : <EOL> headers = { <EOL> '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' , <EOL> } <EOL> return headers </s>
