Fix Python – When to use Serializer’s create() and ModelViewset’s perform_create()

I want to clarify the given documentation of Django-rest-framework regarding the creation of a model object. So far I have found that there are 3 approaches on how to handle such events.

The Serializer’s create() method. Here is the documentation
class CommentSerializer(serializers.Serializer):

def create(self, validated_data):
retur….

Fix Python – How to JSON serialize sets?

I have a Python set that contains objects with __hash__ and __eq__ methods in order to make certain no duplicates are included in the collection.
I need to json encode this result set, but passing even an empty set to the json.dumps method raises a TypeError.
File “/usr/lib/python2.7/json/encoder.py”, line 201, in encode
chunks = self.iteren….

Fix Python – Python – json without whitespaces

I just realized that json.dumps() adds spaces in the JSON object
e.g.
{‘duration’: ’02:55′, ‘name’: ‘flower’, ‘chg’: 0}

how can remove the spaces in order to make the JSON more compact and save bytes to be sent via HTTP?
such as:
{‘duration’:’02:55′,’name’:’flower’,’chg’:0}

….

Fix Python – Serializing class instance to JSON

I am trying to create a JSON string representation of a class instance and having difficulty. Let’s say the class is built like this:
class testclass:
value1 = “a”
value2 = “b”

A call to the json.dumps is made like this:
t = testclass()
json.dumps(t)

It is failing and telling me that the testclass is not JSON serializable.
TypeError: <__....

Fix Python – How to get string objects instead of Unicode from JSON?

I’m using Python 2 to parse JSON from ASCII encoded text files.
When loading these files with either json or simplejson, all my string values are cast to Unicode objects instead of string objects. The problem is, I have to use the data with some libraries that only accept string objects. I can’t change the libraries nor update them.
Is it possib….