<s> from decimal import Decimal <EOL> import json <EOL> from django . core . serializers . json import DjangoJSONEncoder <EOL> from django . utils import six <EOL> try : <EOL> from importlib import import_module <EOL> except ImportError : <EOL> from django . utils . importlib import import_module <EOL> class JSONFieldDescriptor ( object ) : <EOL> def __init__ ( self , field ) : <EOL> self . field = field <EOL> def __get__ ( self , obj , objtype ) : <EOL> cache_field = '<STR_LIT>' % self . field <EOL> if not hasattr ( obj , cache_field ) : <EOL> try : <EOL> setattr ( <EOL> obj , <EOL> cache_field , <EOL> json . loads ( <EOL> getattr ( obj , self . field ) , <EOL> parse_float = Decimal , <EOL> ) <EOL> ) <EOL> except ( TypeError , ValueError ) : <EOL> setattr ( obj , cache_field , { } ) <EOL> return getattr ( obj , cache_field ) <EOL> def __set__ ( self , obj , value ) : <EOL> setattr ( obj , '<STR_LIT>' % self . field , value ) <EOL> setattr ( obj , self . field , json . dumps ( value , cls = DjangoJSONEncoder ) ) <EOL> def get_object ( path , fail_silently = False ) : <EOL> if not isinstance ( path , six . string_types ) : <EOL> return path <EOL> try : <EOL> return import_module ( path ) <EOL> except ImportError : <EOL> try : <EOL> dot = path . rindex ( '<STR_LIT:.>' ) <EOL> mod , fn = path [ : dot ] , path [ dot + <NUM_LIT:1> : ] <EOL> return getattr ( import_module ( mod ) , fn ) <EOL> except ( AttributeError , ImportError ) : <EOL> if not fail_silently : <EOL> raise </s>
