<s> from django . core . serializers import json as djangojson <EOL> from tastypie . serializers import Serializer <EOL> class GeoJSONSerializer ( Serializer ) : <EOL> """<STR_LIT>""" <EOL> formats = [ '<STR_LIT>' ] <EOL> content_types = { <EOL> '<STR_LIT>' : '<STR_LIT:application/json>' , <EOL> } <EOL> def to_geojson ( self , data , options = None ) : <EOL> """<STR_LIT>""" <EOL> def _build_feature ( obj ) : <EOL> f = { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { } <EOL> } <EOL> for key , value in obj . items ( ) : <EOL> if key == '<STR_LIT:location>' : <EOL> f [ '<STR_LIT>' ] = value <EOL> if key in [ '<STR_LIT:id>' , '<STR_LIT>' ] : <EOL> f [ key ] = value <EOL> elif key == '<STR_LIT>' : <EOL> pass <EOL> else : <EOL> f [ '<STR_LIT>' ] [ key ] = value <EOL> return f <EOL> def _build_feature_collection ( objs , meta ) : <EOL> fc = { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ ] <EOL> } <EOL> if ( meta ) : <EOL> fc [ "<STR_LIT>" ] = meta <EOL> for obj in objs : <EOL> fc [ '<STR_LIT>' ] . append ( _build_feature ( obj ) ) <EOL> return fc <EOL> options = options or { } <EOL> data = self . to_simple ( data , options ) <EOL> meta = data . get ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' in data : <EOL> data = _build_feature_collection ( data [ '<STR_LIT>' ] , meta ) <EOL> else : <EOL> data = _build_feature ( data ) <EOL> return djangojson . json . dumps ( data , cls = djangojson . DjangoJSONEncoder , <EOL> sort_keys = True , ensure_ascii = False ) </s>
