<s> """<STR_LIT>""" <EOL> import json <EOL> json_tags = { } <EOL> TAG_PREFIX = '<STR_LIT:!>' <EOL> def register_tag ( cls ) : <EOL> """<STR_LIT>""" <EOL> json_tags [ TAG_PREFIX + getattr ( cls , '<STR_LIT>' ) ] = cls <EOL> return cls <EOL> class JSONTaggedEncoder ( json . JSONEncoder ) : <EOL> def default ( self , obj ) : <EOL> obj_tag = getattr ( obj , '<STR_LIT>' , None ) <EOL> if obj_tag is None : <EOL> return super ( JSONTaggedEncoder , self ) . default ( obj ) <EOL> obj_tag = TAG_PREFIX + obj_tag <EOL> obj = obj . encode_json_obj ( ) <EOL> return { obj_tag : obj } <EOL> class JSONTaggedDecoder ( json . JSONDecoder ) : <EOL> def decode ( self , s ) : <EOL> return self . decode_obj ( super ( JSONTaggedDecoder , self ) . decode ( s ) ) <EOL> @ classmethod <EOL> def decode_obj ( cls , obj ) : <EOL> if isinstance ( obj , dict ) : <EOL> obj = dict ( ( key , cls . decode_obj ( val ) ) for ( key , val ) in obj . items ( ) ) <EOL> elif isinstance ( obj , list ) : <EOL> obj = list ( cls . decode_obj ( val ) for val in obj ) <EOL> if not isinstance ( obj , dict ) or len ( obj ) != <NUM_LIT:1> : <EOL> return obj <EOL> obj_tag = next ( iter ( obj . keys ( ) ) ) <EOL> if not obj_tag . startswith ( '<STR_LIT:!>' ) : <EOL> return obj <EOL> if obj_tag not in json_tags : <EOL> raise ValueError ( '<STR_LIT>' , obj_tag ) <EOL> obj_cls = json_tags [ obj_tag ] <EOL> return obj_cls . decode_json_obj ( obj [ obj_tag ] ) <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] </s>
