<s> import json <EOL> from django import forms <EOL> from django . utils import six <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> __all__ = [ '<STR_LIT>' ] <EOL> class InvalidJSONInput ( six . text_type ) : <EOL> pass <EOL> class JSONField ( forms . CharField ) : <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" ) , <EOL> } <EOL> def __init__ ( self , ** kwargs ) : <EOL> kwargs . setdefault ( '<STR_LIT>' , forms . Textarea ) <EOL> super ( JSONField , self ) . __init__ ( ** kwargs ) <EOL> def to_python ( self , value ) : <EOL> if value in self . empty_values : <EOL> return None <EOL> try : <EOL> return json . loads ( value ) <EOL> except ValueError : <EOL> raise forms . ValidationError ( <EOL> self . error_messages [ '<STR_LIT>' ] , <EOL> code = '<STR_LIT>' , <EOL> params = { '<STR_LIT:value>' : value } , <EOL> ) <EOL> def bound_data ( self , data , initial ) : <EOL> if self . disabled : <EOL> return initial <EOL> try : <EOL> return json . loads ( data ) <EOL> except ValueError : <EOL> return InvalidJSONInput ( data ) <EOL> def prepare_value ( self , value ) : <EOL> if isinstance ( value , InvalidJSONInput ) : <EOL> return value <EOL> return json . dumps ( value ) </s>
