details: http://www.bx.psu.edu/hg/galaxy/rev/2302d3914843 changeset: 1635:2302d3914843 user: James Taylor <james@jamestaylor.org> date: Tue Nov 18 15:47:13 2008 -0500 description: Always write keys in order when creating json strings. Also removed a bunch of unused stuff from JSONType -- if you don't need it... 1 file(s) affected in this change: lib/galaxy/model/custom_types.py diffs (89 lines): diff -r ad26fd305881 -r 2302d3914843 lib/galaxy/model/custom_types.py --- a/lib/galaxy/model/custom_types.py Tue Nov 18 11:23:09 2008 -0500 +++ b/lib/galaxy/model/custom_types.py Tue Nov 18 15:47:13 2008 -0500 @@ -6,6 +6,10 @@ from galaxy.util.bunch import Bunch from galaxy.util.aliaspickler import AliasPickleModule +# Default JSON encoder and decoder +json_encoder = simplejson.JSONEncoder( sort_keys=True ) +json_decoder = simplejson.JSONDecoder( ) + class JSONType( TypeDecorator ): """ Defines a JSONType for SQLAlchemy. Takes a primitive as input and @@ -13,63 +17,42 @@ """ impl = Binary - def __init__( self, jsonifyer=None, mutable=True ): - self.jsonifyer = jsonifyer or simplejson - self.mutable = mutable - super( JSONType, self).__init__() - def process_bind_param( self, value, dialect ): if value is None: return None - return self.jsonifyer.dumps( value ) + return json_encoder.encode( value ) def process_result_value( self, value, dialect ): if value is None: return None - return self.jsonifyer.loads( str( value ) ) + return json_decoder.decode( str( value ) ) def copy_value( self, value ): - if self.mutable: - return self.jsonifyer.loads( self.jsonifyer.dumps(value) ) - else: - return value + return json_decoder.decode( json_encoder.encode( value ) ) def compare_values( self, x, y ): - if self.mutable: - return self.jsonifyer.dumps( x ) == self.jsonifyer.dumps( y ) - else: - return x is y + return json_encoder.encode( x ) == json_encoder.encode( y ) - def is_mutable( self ): - return self.mutable - -MetadataPickler = AliasPickleModule( { +metadata_pickler = AliasPickleModule( { ( "cookbook.patterns", "Bunch" ) : ( "galaxy.util.bunch" , "Bunch" ) } ) class MetadataType( JSONType ): """ - Mixture between JSONType and PickleType...can read in either, and - writes JSON. + Backward compatible metadata type. Can read pickles or JSON, but always + writes in JSON. """ - - def __init__( self, pickler=MetadataPickler, jsonifyer=None, mutable=True ): - self.jsonifyer = jsonifyer or simplejson - self.pickler = pickler or pickle - self.mutable = mutable - super( MetadataType, self).__init__() - def process_result_value( self, value, dialect ): if value is None: return None - buf = value ret = None try: - ret = self.pickler.loads( str(buf) ) - if ret: ret = dict( ret.__dict__ ) + ret = metadata_pickler.loads( str( value ) ) + if ret: + ret = dict( ret.__dict__ ) except: try: - ret = self.jsonifyer.loads( str(buf) ) + ret = json_decoder.decode( str( value ) ) except: ret = None return ret