<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> import six <EOL> from decimal import Decimal <EOL> from django . db import models <EOL> from django . conf import settings <EOL> from django . core . serializers . json import DjangoJSONEncoder <EOL> try : <EOL> import json <EOL> except ImportError : <EOL> from django . utils import simplejson as json <EOL> def dumps ( value ) : <EOL> return DjangoJSONEncoder ( ) . encode ( value ) <EOL> def loads ( txt ) : <EOL> value = json . loads ( <EOL> txt , <EOL> parse_float = Decimal , <EOL> encoding = settings . DEFAULT_CHARSET <EOL> ) <EOL> return value <EOL> class JSONDict ( dict ) : <EOL> """<STR_LIT>""" <EOL> def __repr__ ( self ) : <EOL> return dumps ( self ) <EOL> class JSONUnicode ( six . text_type ) : <EOL> """<STR_LIT>""" <EOL> def __repr__ ( self ) : <EOL> return dumps ( self ) <EOL> class JSONList ( list ) : <EOL> """<STR_LIT>""" <EOL> def __repr__ ( self ) : <EOL> return dumps ( self ) <EOL> class JSONField ( six . with_metaclass ( models . SubfieldBase , models . TextField ) ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> default = kwargs . get ( '<STR_LIT:default>' , None ) <EOL> if default is None : <EOL> kwargs [ '<STR_LIT:default>' ] = '<STR_LIT:{}>' <EOL> elif isinstance ( default , ( list , dict ) ) : <EOL> kwargs [ '<STR_LIT:default>' ] = dumps ( default ) <EOL> models . TextField . __init__ ( self , * args , ** kwargs ) <EOL> def to_python ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if value is None or value == '<STR_LIT>' : <EOL> return { } <EOL> elif isinstance ( value , six . string_types ) : <EOL> res = loads ( value ) <EOL> if isinstance ( res , dict ) : <EOL> return JSONDict ( ** res ) <EOL> elif isinstance ( res , six . string_types ) : <EOL> return JSONUnicode ( res ) <EOL> elif isinstance ( res , list ) : <EOL> return JSONList ( res ) <EOL> return res <EOL> else : <EOL> return value <EOL> def get_db_prep_save ( self , value , connection , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if value is None and self . null : <EOL> return None <EOL> if not isinstance ( value , six . string_types ) : <EOL> value = dumps ( value ) <EOL> return super ( JSONField , self ) . get_db_prep_save ( value , connection = connection ) <EOL> def south_field_triple ( self ) : <EOL> """<STR_LIT>""" <EOL> from south . modelsinspector import introspector <EOL> field_class = "<STR_LIT>" <EOL> args , kwargs = introspector ( self ) <EOL> return ( field_class , args , kwargs ) <EOL> def deconstruct ( self ) : <EOL> name , path , args , kwargs = super ( JSONField , self ) . deconstruct ( ) <EOL> if self . default == '<STR_LIT:{}>' : <EOL> del kwargs [ '<STR_LIT:default>' ] <EOL> return name , path , args , kwargs </s>
