<s> import simplejson as json <EOL> from devil . datamapper import DataMapper <EOL> from devil import errors <EOL> class JsonMapper ( DataMapper ) : <EOL> content_type = '<STR_LIT:application/json>' <EOL> def __init__ ( self , use_decimal = False ) : <EOL> """<STR_LIT>""" <EOL> self . use_decimal = use_decimal <EOL> def _format_data ( self , data , charset ) : <EOL> if data is None or data == '<STR_LIT>' : <EOL> return u'<STR_LIT>' <EOL> else : <EOL> params = { <EOL> '<STR_LIT>' : <NUM_LIT:4> , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : charset , <EOL> } <EOL> self . _maybe_add_use_decimal ( params ) <EOL> return json . dumps ( data , ** params ) <EOL> def _parse_data ( self , data , charset ) : <EOL> params = { } <EOL> self . _maybe_add_use_decimal ( params ) <EOL> try : <EOL> return json . loads ( data , charset , ** params ) <EOL> except ValueError , exc : <EOL> raise errors . BadRequest ( '<STR_LIT>' % ( str ( exc ) , ) ) <EOL> def _maybe_add_use_decimal ( self , params ) : <EOL> """<STR_LIT>""" <EOL> if self . use_decimal : <EOL> params [ '<STR_LIT>' ] = True </s>
