<s> import json <EOL> from ztag . transform import Encoder <EOL> class JSONEncoder ( Encoder ) : <EOL> def encode ( self , zobj ) : <EOL> obj = zobj . transformed or dict ( ) <EOL> if zobj . metadata is not None : <EOL> obj [ '<STR_LIT>' ] = zobj . metadata . local_metadata . to_dict ( ) <EOL> obj [ '<STR_LIT>' ] = zobj . metadata . global_metadata . to_dict ( ) <EOL> obj [ '<STR_LIT>' ] = list ( zobj . metadata . tags ) <EOL> return json . dumps ( obj , sort_keys = True ) <EOL> class LocalJSONEncoder ( Encoder ) : <EOL> def encode ( self , zobj ) : <EOL> obj = zobj . transformed or dict ( ) <EOL> if zobj . metadata is not None : <EOL> obj [ '<STR_LIT>' ] = zobj . metadata . local_metadata . to_dict ( ) <EOL> return json . dumps ( obj , sort_keys = True ) <EOL> class HexEncoder ( Encoder ) : <EOL> def encode ( self , obj ) : <EOL> return obj . encode ( "<STR_LIT>" ) <EOL> class IdentityEncoder ( Encoder ) : <EOL> def encode ( self , obj ) : <EOL> return obj <EOL> class _SequenceEncoder ( Encoder ) : <EOL> def __init__ ( self , encoders , * args , ** kwargs ) : <EOL> super ( _SequenceEncoder , self ) . __init__ ( self , * args , ** kwargs ) <EOL> self . encoders = encoders <EOL> def encode ( self , obj ) : <EOL> out = obj <EOL> for encoder in self . encoders : <EOL> out = encoder . encode ( out ) <EOL> return out <EOL> def encoder_sequence ( encoders ) : <EOL> return _SequenceEncoder ( encoders ) </s>
