Reputation: 32061
I'm using the code found here to convert a GAE model into JSON:
def to_dict(self):
return dict([(p, unicode(getattr(self, p))) for p in self.properties()])
It works just fine, but if a property does not have a value, it puts a default string of "None", and this is interpreted as a real value in my client device (Objective-C), even though it should be interpreted as a nil value.
How can I modify the code above while maintaining its brevity to skip over and not write properties to the dictionary that have None values?
Upvotes: 3
Views: 595
Reputation: 70552
def to_dict(self):
return dict((p, unicode(getattr(self, p))) for p in self.properties()
if getattr(self, p) is not None)
You don't need to create a list first (the surrounding []
), you can just use a generator expression to build the values on-the-fly.
It's not quite brief, but if your model structure ever gets a bit more complex, you may want to look at this recursive variant:
# Define 'simple' types
SIMPLE_TYPES = (int, long, float, bool, dict, basestring, list)
def to_dict(model):
output = {}
for key, prop in model.properties().iteritems():
value = getattr(model, key)
if isinstance(value, SIMPLE_TYPES) and value is not None:
output[key] = value
elif isinstance(value, datetime.date):
# Convert date/datetime to ms-since-epoch ("new Date()").
ms = time.mktime(value.utctimetuple())
ms += getattr(value, 'microseconds', 0) / 1000
output[key] = int(ms)
elif isinstance(value, db.GeoPt):
output[key] = {'lat': value.lat, 'lon': value.lon}
elif isinstance(value, db.Model):
# Recurse
output[key] = to_dict(value)
else:
raise ValueError('cannot encode ' + repr(prop))
return output
This could be easily extended with other non-simple types by adding to the elif
branches.
Upvotes: 7