title: How to JSON serialize sets?
body: <p>I have a Python <code>set</code> that contains objects with <code>__hash__</code> and <code>__eq__</code> methods in order to make certain no duplicates are included in the collection.</p>
  
  <p>I need to json encode this result <code>set</code>, but passing even an empty <code>set</code> to the <code>json.dumps</code> method raises a <code>TypeError</code>.</p>
  
  <pre class="lang-none prettyprint-override"><code>  File "/usr/lib/python2.7/json/encoder.py", line 201, in encode
      chunks = self.iterencode(o, _one_shot=True)
    File "/usr/lib/python2.7/json/encoder.py", line 264, in iterencode
      return _iterencode(o, 0)
    File "/usr/lib/python2.7/json/encoder.py", line 178, in default
      raise TypeError(repr(o) + " is not JSON serializable")
  TypeError: set([]) is not JSON serializable
  </code></pre>
  
  <p>I know I can create an extension to the <code>json.JSONEncoder</code> class that has a custom <code>default</code> method, but I'm not even sure where to begin in converting over the <code>set</code>.  Should I create a dictionary out of the <code>set</code> values within the default method, and then return the encoding on that?  Ideally, I'd like to make the default method able to handle all the datatypes that the original encoder chokes on (I'm using Mongo as a data source so dates seem to raise this error too)</p>
  
  <p>Any hint in the right direction would be appreciated.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Thanks for the answer!  Perhaps I should have been more precise.</p>
  
  <p>I utilized (and upvoted) the answers here to get around the limitations of the <code>set</code> being translated, but there are internal keys that are an issue as well.</p>
  
  <p>The objects in the <code>set</code> are complex objects that translate to <code>__dict__</code>, but they themselves can also contain values for their properties that could be ineligible for the basic types in the json encoder.</p>
  
  <p>There's a lot of different types coming into this <code>set</code>, and the hash basically calculates a unique id for the entity, but in the true spirit of NoSQL there's no telling exactly what the child object contains.</p>
  
  <p>One object might contain a date value for <code>starts</code>, whereas another may have some other schema that includes no keys containing "non-primitive" objects.</p>
  
  <p>That is why the only solution I could think of was to extend the <code>JSONEncoder</code> to replace the <code>default</code> method to turn on different cases - but I'm not sure how to go about this and the documentation is ambiguous.  In nested objects, does the value returned from <code>default</code> go by key, or is it just a generic include/discard that looks at the whole object?  How does that method accommodate nested values?  I've looked through previous questions and can't seem to find the best approach to case-specific encoding (which unfortunately seems like what I'm going to need to do here).</p>