Skip to main content
Remove dots to make copying easier
Source Link
Akaisteph7
  • 6.9k
  • 2
  • 39
  • 53

You can create a custom encoder that returns a list when it encounters a set. Here's an example:

>>> import json >>> class SetEncoder(json.JSONEncoder): ... def default(self, obj): ...  if isinstance(obj, set): ...  return list(obj) ...  return json.JSONEncoder.default(self, obj) ...  >>>data_str = json.dumps(set([1,2,3,4,5]), cls=SetEncoder) print(data_str) # Output: '[1, 2, 3, 4, 5]' 

You can detect other types this way too. If you need to retain that the list was actually a set, you could use a custom encoding. Something like return {'type':'set', 'list':list(obj)} might work.

To illustratedillustrate nested types, consider serializing this:

>>> class Something(object): ... pass >>> json.dumps(set([1,2,3,4,5,Something()]), cls=SetEncoder) 

This raises the following error:

TypeError: <__main__.Something object at 0x1691c50> is not JSON serializable 

This indicates that the encoder will take the list result returned and recursively call the serializer on its children. To add a custom serializer for multiple types, you can do this:

>>> class SetEncoder(json.JSONEncoder): ... def default(self, obj): ...  if isinstance(obj, set): ...  return list(obj) ...  if isinstance(obj, Something): ...  return 'CustomSomethingRepresentation' ...  return json.JSONEncoder.default(self, obj) ... >>>data_str = json.dumps(set([1,2,3,4,5,Something()]), cls=SetEncoder) print(data_str) # Output: '[1, 2, 3, 4, 5, "CustomSomethingRepresentation"]' 

You can create a custom encoder that returns a list when it encounters a set. Here's an example:

>>> import json >>> class SetEncoder(json.JSONEncoder): ... def default(self, obj): ... if isinstance(obj, set): ... return list(obj) ... return json.JSONEncoder.default(self, obj) ...  >>> json.dumps(set([1,2,3,4,5]), cls=SetEncoder) '[1, 2, 3, 4, 5]' 

You can detect other types this way too. If you need to retain that the list was actually a set, you could use a custom encoding. Something like return {'type':'set', 'list':list(obj)} might work.

To illustrated nested types, consider serializing this:

>>> class Something(object): ... pass >>> json.dumps(set([1,2,3,4,5,Something()]), cls=SetEncoder) 

This raises the following error:

TypeError: <__main__.Something object at 0x1691c50> is not JSON serializable 

This indicates that the encoder will take the list result returned and recursively call the serializer on its children. To add a custom serializer for multiple types, you can do this:

>>> class SetEncoder(json.JSONEncoder): ... def default(self, obj): ... if isinstance(obj, set): ... return list(obj) ... if isinstance(obj, Something): ... return 'CustomSomethingRepresentation' ... return json.JSONEncoder.default(self, obj) ... >>> json.dumps(set([1,2,3,4,5,Something()]), cls=SetEncoder) '[1, 2, 3, 4, 5, "CustomSomethingRepresentation"]' 

You can create a custom encoder that returns a list when it encounters a set. Here's an example:

import json class SetEncoder(json.JSONEncoder): def default(self, obj):   if isinstance(obj, set):   return list(obj)   return json.JSONEncoder.default(self, obj) data_str = json.dumps(set([1,2,3,4,5]), cls=SetEncoder) print(data_str) # Output: '[1, 2, 3, 4, 5]' 

You can detect other types this way too. If you need to retain that the list was actually a set, you could use a custom encoding. Something like return {'type':'set', 'list':list(obj)} might work.

To illustrate nested types, consider serializing this:

class Something(object): pass json.dumps(set([1,2,3,4,5,Something()]), cls=SetEncoder) 

This raises the following error:

TypeError: <__main__.Something object at 0x1691c50> is not JSON serializable 

This indicates that the encoder will take the list result returned and recursively call the serializer on its children. To add a custom serializer for multiple types, you can do this:

class SetEncoder(json.JSONEncoder): def default(self, obj):   if isinstance(obj, set):   return list(obj)   if isinstance(obj, Something):   return 'CustomSomethingRepresentation'   return json.JSONEncoder.default(self, obj) data_str = json.dumps(set([1,2,3,4,5,Something()]), cls=SetEncoder) print(data_str) # Output: '[1, 2, 3, 4, 5, "CustomSomethingRepresentation"]' 
Null edit.
Source Link
Raymond Hettinger
  • 228.8k
  • 67
  • 405
  • 504

You can create a custom encoder that returns a list when it encounters a set. Here's an example:

>>> import json >>> class SetEncoder(json.JSONEncoder): ... def default(self, obj): ... if isinstance(obj, set): ... return list(obj) ... return json.JSONEncoder.default(self, obj) ... >>> json.dumps(set([1,2,3,4,5]), cls=SetEncoder) '[1, 2, 3, 4, 5]' 

You can detect other types this way too. If you need to retain that the list was actually a set, you could use a custom encoding. Something like return {'type':'set', 'list':list(obj)} might work.

To illustrated nested types, consider serializing this:

>>> class Something(object): ... pass >>> json.dumps(set([1,2,3,4,5,Something()]), cls=SetEncoder) 

This throwsraises the following error:

TypeError: <__main__.Something object at 0x1691c50> is not JSON serializable 

This indicates that the encoder will take the list result returned and recursively call the serializer on its children. To add a custom serializer for multiple types, you can do this:

>>> class SetEncoder(json.JSONEncoder): ... def default(self, obj): ... if isinstance(obj, set): ... return list(obj) ... if isinstance(obj, Something): ... return 'CustomSomethingRepresentation' ... return json.JSONEncoder.default(self, obj) ... >>> json.dumps(set([1,2,3,4,5,Something()]), cls=SetEncoder) '[1, 2, 3, 4, 5, "CustomSomethingRepresentation"]' 

You can create a custom encoder that returns a list when it encounters a set. Here's an example:

>>> import json >>> class SetEncoder(json.JSONEncoder): ... def default(self, obj): ... if isinstance(obj, set): ... return list(obj) ... return json.JSONEncoder.default(self, obj) ... >>> json.dumps(set([1,2,3,4,5]), cls=SetEncoder) '[1, 2, 3, 4, 5]' 

You can detect other types this way too. If you need to retain that the list was actually a set, you could use a custom encoding. Something like return {'type':'set', 'list':list(obj)} might work.

To illustrated nested types, consider serializing this:

>>> class Something(object): ... pass >>> json.dumps(set([1,2,3,4,5,Something()]), cls=SetEncoder) 

This throws the following error:

TypeError: <__main__.Something object at 0x1691c50> is not JSON serializable 

This indicates that the encoder will take the list result returned and recursively call the serializer on its children. To add a custom serializer for multiple types, you can do this:

>>> class SetEncoder(json.JSONEncoder): ... def default(self, obj): ... if isinstance(obj, set): ... return list(obj) ... if isinstance(obj, Something): ... return 'CustomSomethingRepresentation' ... return json.JSONEncoder.default(self, obj) ... >>> json.dumps(set([1,2,3,4,5,Something()]), cls=SetEncoder) '[1, 2, 3, 4, 5, "CustomSomethingRepresentation"]' 

You can create a custom encoder that returns a list when it encounters a set. Here's an example:

>>> import json >>> class SetEncoder(json.JSONEncoder): ... def default(self, obj): ... if isinstance(obj, set): ... return list(obj) ... return json.JSONEncoder.default(self, obj) ... >>> json.dumps(set([1,2,3,4,5]), cls=SetEncoder) '[1, 2, 3, 4, 5]' 

You can detect other types this way too. If you need to retain that the list was actually a set, you could use a custom encoding. Something like return {'type':'set', 'list':list(obj)} might work.

To illustrated nested types, consider serializing this:

>>> class Something(object): ... pass >>> json.dumps(set([1,2,3,4,5,Something()]), cls=SetEncoder) 

This raises the following error:

TypeError: <__main__.Something object at 0x1691c50> is not JSON serializable 

This indicates that the encoder will take the list result returned and recursively call the serializer on its children. To add a custom serializer for multiple types, you can do this:

>>> class SetEncoder(json.JSONEncoder): ... def default(self, obj): ... if isinstance(obj, set): ... return list(obj) ... if isinstance(obj, Something): ... return 'CustomSomethingRepresentation' ... return json.JSONEncoder.default(self, obj) ... >>> json.dumps(set([1,2,3,4,5,Something()]), cls=SetEncoder) '[1, 2, 3, 4, 5, "CustomSomethingRepresentation"]' 
added 942 characters in body
Source Link
jterrace
  • 67.5k
  • 24
  • 164
  • 208

You can create a custom encoder that returns a list when it encounters a set. Here's an example:

>>> import json >>> class SetEncoder(json.JSONEncoder): ... def default(self, obj): ... if isinstance(obj, set): ... return list(obj) ... return json.JSONEncoder.default(self, obj) ... >>> json.dumps(set([1,2,3,4,5]), cls=SetEncoder) '[1, 2, 3, 4, 5]' 

You can detect other types this way too. If you need to retain that the list was actually a set, you could use a custom encoding. Something like return {'type':'set', 'list':list(obj)} might work.

To illustrated nested types, consider serializing this:

>>> class Something(object): ... pass >>> json.dumps(set([1,2,3,4,5,Something()]), cls=SetEncoder) 

This throws the following error:

TypeError: <__main__.Something object at 0x1691c50> is not JSON serializable 

This indicates that the encoder will take the list result returned and recursively call the serializer on its children. To add a custom serializer for multiple types, you can do this:

>>> class SetEncoder(json.JSONEncoder): ... def default(self, obj): ... if isinstance(obj, set): ... return list(obj) ... if isinstance(obj, Something): ... return 'CustomSomethingRepresentation' ... return json.JSONEncoder.default(self, obj) ... >>> json.dumps(set([1,2,3,4,5,Something()]), cls=SetEncoder) '[1, 2, 3, 4, 5, "CustomSomethingRepresentation"]' 

You can create a custom encoder that returns a list when it encounters a set. Here's an example:

>>> import json >>> class SetEncoder(json.JSONEncoder): ... def default(self, obj): ... if isinstance(obj, set): ... return list(obj) ... return json.JSONEncoder.default(self, obj) ... >>> json.dumps(set([1,2,3,4,5]), cls=SetEncoder) '[1, 2, 3, 4, 5]' 

You can detect other types this way too. If you need to retain that the list was actually a set, you could use a custom encoding. Something like return {'type':'set', 'list':list(obj)} might work.

You can create a custom encoder that returns a list when it encounters a set. Here's an example:

>>> import json >>> class SetEncoder(json.JSONEncoder): ... def default(self, obj): ... if isinstance(obj, set): ... return list(obj) ... return json.JSONEncoder.default(self, obj) ... >>> json.dumps(set([1,2,3,4,5]), cls=SetEncoder) '[1, 2, 3, 4, 5]' 

You can detect other types this way too. If you need to retain that the list was actually a set, you could use a custom encoding. Something like return {'type':'set', 'list':list(obj)} might work.

To illustrated nested types, consider serializing this:

>>> class Something(object): ... pass >>> json.dumps(set([1,2,3,4,5,Something()]), cls=SetEncoder) 

This throws the following error:

TypeError: <__main__.Something object at 0x1691c50> is not JSON serializable 

This indicates that the encoder will take the list result returned and recursively call the serializer on its children. To add a custom serializer for multiple types, you can do this:

>>> class SetEncoder(json.JSONEncoder): ... def default(self, obj): ... if isinstance(obj, set): ... return list(obj) ... if isinstance(obj, Something): ... return 'CustomSomethingRepresentation' ... return json.JSONEncoder.default(self, obj) ... >>> json.dumps(set([1,2,3,4,5,Something()]), cls=SetEncoder) '[1, 2, 3, 4, 5, "CustomSomethingRepresentation"]' 
Source Link
jterrace
  • 67.5k
  • 24
  • 164
  • 208
Loading