<s> """<STR_LIT>""" <EOL> import datetime , json <EOL> from django . db import models <EOL> from django . db . models import signals <EOL> from django . conf import settings <EOL> from django . core . serializers . json import DjangoJSONEncoder <EOL> class JSONField ( models . TextField ) : <EOL> """<STR_LIT>""" <EOL> __metaclass__ = models . SubfieldBase <EOL> def __init__ ( self , json_type = None , deserialization_params = None , ** kwargs ) : <EOL> self . json_type = json_type <EOL> self . deserialization_params = deserialization_params <EOL> super ( JSONField , self ) . __init__ ( ** kwargs ) <EOL> def to_python ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if self . json_type : <EOL> if isinstance ( value , self . json_type ) : <EOL> return value <EOL> if isinstance ( value , dict ) or isinstance ( value , list ) : <EOL> return value <EOL> if value == "<STR_LIT>" or value == None : <EOL> return None <EOL> try : <EOL> parsed_value = json . loads ( value ) <EOL> except : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if self . json_type and parsed_value : <EOL> parsed_value = self . json_type . fromJSONDict ( parsed_value , ** self . deserialization_params ) <EOL> return parsed_value <EOL> def get_prep_value ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( value , basestring ) : <EOL> return value <EOL> if value == None : <EOL> return None <EOL> if self . json_type and isinstance ( value , self . json_type ) : <EOL> the_dict = value . toJSONDict ( ) <EOL> else : <EOL> the_dict = value <EOL> return json . dumps ( the_dict , cls = DjangoJSONEncoder ) <EOL> def value_to_string ( self , obj ) : <EOL> value = self . _get_val_from_obj ( obj ) <EOL> return self . get_db_prep_value ( value ) </s>
